I would like to start jsreport from another app. I have done the following:
npm install jsreport
node node_modules/.bin/jsreport start
Then I can open http://localhost:5488 and print the demo "Invoice" report. To start it from bin/www (my application), I found the following code:
var jsreport = require('jsreport')();
jsreport.use(require('jsreport-handlebars'));
jsreport.use(require('jsreport-phantom-pdf'));
jsreport.use(require('jsreport-fs-store')());
jsreport.init().then(function () {
// running
}).catch(function (e) {
// error during startup
console.error(e.stack)
process.exit(1)
})
With this in place, I can still open http://localhost:5488 and still see the demo reports, but I cannot run the Invoice report because it simply times out. There seems to be a lot of people complaining about this, but no real solution. The next thing I tried was to copy the code of node_modules/.bin/jsreport into a new file (bin/jsreport), changed the paths to work (require) and also hardcoded the arguments as [ "start" ]. I did this with the hope of being able to call this script from bin/www. Funny thing is that if I run this script from the command line (node bin/jsreport), it works as expected if I open http://localhost:5488 in my browser, but if I use vscode debugger it times out on generating the Invoice report. (the rest of the JSReport user interface works). The vscode lauch config is as follows:
{
"type": "node",
"request": "launch",
"name": "Launch JSReport",
"sourceMaps": false,
"program": "${workspaceRoot}/bin/jsreport"
}
So I found a different solution to start jsreport from my app.
var jsrcore = require("jsreport-core");
var phantom = require("jsreport-phantom-pdf");
var renderer = require("jsreport-jsrender");
var jsreportfs = require('jsreport-fs-store')();
var jsreport = jsrcore({ tasks: { strategy: 'in-process', allowedModules: '*' } });
// I also tried the following in stead of the in-process:
// var jsreport = jsrcore({ tasks: { allowedModules: '*' } });
Using this, using jsreport.render({template: {name: "Invoice"} ... }) does not time out any more, but I get the following error:
var options = {
template: {name: "Invoice"},
recipe: "html",
engine: "jsrender",
options: { timeout: 600000, "Content-Disposition": "attachment; filename=invoice.pdf" }
};
jsreport.render(options).then(function(out) {
out.stream.pipe(res);
}).catch(function(err) {
res.status(500).json({
success: false,
message: "An error occurred: " + err
});
});
An error occurred: Error: Error during rendering report: Unable to find specified template or user doesnt have permissions to read it: Invoice
Is there not a simple example somewhere that simply shows how to start jsreport from within an application that also tells you how to get the Invoice pdf?