Use external JavaScript library in jsreport

602 views
Skip to first unread message

sharad....@gmail.com

unread,
Jun 6, 2016, 5:16:16 AM6/6/16
to jsreport
I have a javascript file named "projectUtilities.js". This file contains various functions such as sorting data, grouping data, some calculations and ,etc. I would like to include my library "projectUtilities.js" in jsreport so that I need not have to write same code for each reports. I am using handlebar for templating. Is there any way to register my library "projectUtilities.js" for use in jsReport.

Jan Blaha

unread,
Jun 7, 2016, 4:39:07 AM6/7/16
to jsreport
Sorry for the late response.

- If you want to link this script into the template html, probably the easiest and the cleanest is to use jsreport-static-resources. That allows you to link this script in the head section of html.

- If you want to use the functions from the script in handlebars helpers, I would recommend to use require to load it as node module, this is described here

- You can also use custom script to automate the whole process - http://jsreport.net/learn/scripts

Untested hints, remember to allow fs module in config
var fs = require('fs')
var utilsContent = fs.readFileSync('__rootDirectory', 'projectUtilities.js')


function beforeRender(done) {
  request
.template.helpers += '\n' + utilsContent;
 
done()
}


Reply all
Reply to author
Forward
0 new messages