Rendering with Dynamic data in single executable file



  • Is there any way to render the HTML template with dynamic json data without storing it in .JSON File for single executable JSReport File?



  • You could use jsreport custom script to prepare data programmatically and load it from environment variable for example.
    https://jsreport.net/learn/scripts



  • I tried it, but not working. My html file looks like,

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    </head>
    <body>
        <script>
            function beforeRender(req, res, done) {
                // merge in some values for later use in engine
                // and preserve other values which are already in
                req.data = Object.assign({}, req.data, {foo: "foo"})
                done()
            }
        </script>
        <%= foo %>
    </body>
    </html>
    

    The error page is,

    arpa@ubuntu:~/Documents/Cape/jsreport$ ./jsreport render  --template.engine=ejs  --template.recipe=chrome-pdf  --template.content=./../static/index1.html  --out=./../output/pallet.pdf
    starting rendering process..
    rendering has finished with errors:
    A critical error occurred while trying to execute the render command (2). Error while executing templating engine. ejs:18
        16|         }
        17|     </script>
     >> 18|     <%= foo %>
        19| </body>
        20| </html>
    
    foo is not defined (1). 
    caused by error (2):
    -> stack
    Error:
        at onCriticalError (/snapshot/jsreport/node_modules/jsreport-cli/lib/commands/render.js:302:19)
        at /snapshot/jsreport/node_modules/jsreport-cli/lib/commands/render.js:298:12
    caused by error (1):
    -> meta = {"cause":{},"isOperational":true,"weak":true,"logged":true}
    -> stack
    ReferenceError: ejs:18
        16|         }
        17|     </script>
     >> 18|     <%= foo %>
        19| </body>
        20| </html>
    
    foo is not defined
        at eval (eval at compile (/home/arpa/Documents/Cape/jsreport/node_modules/ejs/lib/ejs.js:633:12), <anonymous>:9:26)
        at returnedFn (/home/arpa/Documents/Cape/jsreport/node_modules/ejs/lib/ejs.js:668:17)
        at /home/arpa/Documents/Cape/jsreport/node_modules/jsreport-ejs/lib/ejsEngine.js:10:12
        at Object.base.apply (/snapshot/jsreport/node_modules/vm2/lib/contextify.js:469:32)
        at evaluate-template-engine.js:1:64
        at Script.runInContext (vm.js:133:20)
        at VM.run (/snapshot/jsreport/node_modules/vm2/lib/main.js:219:62)
        at run (/snapshot/jsreport/node_modules/jsreport-core/lib/render/safeSandbox.js:171:19)
        at module.exports (/snapshot/jsreport/node_modules/jsreport-core/lib/render/engineScript.js:268:5)
        at Object.execute (/snapshot/jsreport/node_modules/script-manager/lib/in-process.js:31:34)
        at Object.tryCatcher (/snapshot/jsreport/node_modules/bluebird/js/release/util.js:16:23)
        at Object.ret [as executeAsync] (eval at makeNodePromisifiedEval (/snapshot/jsreport/node_modules/bluebird/js/release/promisify.js:184:12), <anonymous>:11:39)
        at executeScript (/snapshot/jsreport/node_modules/jsreport-core/lib/util/executeScript.js:41:49)
        at Reporter.executeScript (/snapshot/jsreport/node_modules/jsreport-core/lib/reporter.js:343:12)
        at invokeRender (/snapshot/jsreport/node_modules/jsreport-core/lib/render/render.js:60:36)
        at /snapshot/jsreport/node_modules/jsreport-core/lib/render/render.js:150:11
        at async startRender (/snapshot/jsreport/node_modules/jsreport-cli/lib/commands/render.js:357:17)
        at async /snapshot/jsreport/node_modules/jsreport-cli/lib/commands/render.js:291:13
        at async Commander.executeCommand (/snapshot/jsreport/node_modules/jsreport-cli/lib/commander.js:242:22)
    
    

    I also tried with handlebar templates. It doesn't show error but there is no text [plain white page] in the generated pdf.


Log in to reply
 

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