Start jsreport from another app



  • 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?


  • administrators

    hi! since you did not mention anywhere if your custom app is node.js based or not, or if you had read the docs about using jsreport in custom app i will start linking to the relevant docs for using jsreport with custom node.js app: https://jsreport.net/learn/adapting-jsreport

    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

    if you are just trying to use jsreport from your app you don't need to involve the node node_modules/.bin/jsreport start at all, that command is just to quickly getting started or when you want to treat jsreport as a standalone app. if you want to integrate jsreport into your existing server see the docs i linked before.

    for the record i think that the Invoice report times out for you because you are not activating all the necessary extensions, when you call jsreport.use you are manually activating extensions, jsreport has a lot of functionality activated by default and since you are activating things manually then you can not expect that everything will work as it is supposed in a normal default installation. i don't know why are you calling jsreport.use in the first place, i don't remember that we are recommending using jsreport.use or use node_modules/.bin/jsreport start to integrate jsreport into custom app, for integrations we recommend doing this https://jsreport.net/learn/adapting-jsreport

    There seems to be a lot of people complaining about this, but no real solution.

    can you show me where did you see the complaints with no solution? we always try to guide the best we can and provide steps for user to resolve its confusion. we are not perfect but i don't remember seeing a lot of people complaining about this, if that is the case please show me some links about that so we can improve the situation in the future.

    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 vscode debugger with jsreport is another history, we have no docs about debugging jsreport but as you found changing tasks strategy to in-process is the trick. the rest of your problems are still caused by manually activating extensions, etc. some info and explanation about debugging here, i will try to put some notes about nodejs debugging with jsreport in official docs.

    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?

    the example and the solution for the rest of your problems are here -> https://jsreport.net/learn/adapting-jsreport if you have some problems understanding the information there just paste here what part you don't understand.

    as a final note please note that when we provide free help support the least that we expect is that user had read the docs, for your case i think all your problems would be avoided if you read that link in the first place, which is very visible in node.js learn section of jsreport website.



  • Thanks for a very quick, very detailed and complete answer - it is greatly appreciated. I do apologise for not stating that my app is a nodejs app. Also I did state "with no real solution" on the issue of "timeout". This was unfair of me. I should have been more clear in that I did find a number of complaints about the timeout and although there were responses, none of them worked for me - or maybe I simply did not spot my problem in the responses I read.

    As for progress on my side. I did read the articles you ask me as I worked through your response and ended up creating a simple test directly out of the documentation. The test I did was to create a project exactly from the doc as follows:

    var http = require('http');
    var jsreport = require('jsreport');
    
    http.createServer(function (req, res) {
      jsreport.render({ template: { content: 'Hello world', engine: 'jsrender', recipe: 'phantom-pdf' } }).then(function(out) {
        out.stream.pipe(res);
      }).catch(function(e) {
        res.end(e.message);
      });
    
    }).listen(1337, '127.0.0.1');
    

    Sure enough - I got the timeout. But then I tried starting the server from the CLI in stead of vscode debugger and it worked. Only after I tried this I finished reading your post and found that you have actually have mentioned that exact problem with vscode debugger.

    Once again - thanks for a very helpful response to my question.


Log in to reply
 

Looks like your connection to jsreport forum was lost, please wait while we try to reconnect.