how to render data in nodejs using jsreport and store file in pdf



  • here my code:
    jsreport.render({
    template: {

                                        // content: fs.readFileSync('../test.html'),
                                        content: "Hello world {{#sayLoudly this.name}}",
                                        helpers: "function sayLoudly(str) { return str.toUpperCase(); }",
                                        engine: "jsrender",
                                        // recipe: "html"
                                        recipe: "phantom-pdf",
                                        data: { name: "jsreport" }
                                    },
    
                                }).then(function(out){
                                    console.log("*********************************",out)
                                    // out.stream.pipe(fs.createWriteStream('../reports.pdf'));
                                    var read=fs.createWriteStream('report.pdf')
                                    out.stream.pipe(read);
                                });
    

    i got an error like this:
    Unhandled rejection Error: Error during rendering report: Data entry not found (
    jsreport)
    at E:\homaso\node_modules\jsreport-data\lib\data.js:87:15
    at tryCatcher (E:\homaso\node_modules\listener-collection\node_modules\blueb
    ird\js\release\util.js:16:23)
    at Promise._settlePromiseFromHandler (E:\homaso\node_modules\listener-collec
    tion\node_modules\bluebird\js\release\promise.js:510:31)
    at Promise._settlePromise (E:\homaso\node_modules\listener-collection\node_m
    odules\bluebird\js\release\promise.js:567:18)
    at Promise.settlePromise0 (E:\homaso\node_modules\listener-collection\node
    modules\bluebird\js\release\promise.js:612:10)
    at Promise.settlePromises (E:\homaso\node_modules\listener-collection\node
    modules\bluebird\js\release\promise.js:691:18)
    at Async._drainQueue (E:\homaso\node_modules\listener-collection\node_module
    s\bluebird\js\release\async.js:133:16)
    at Async._drainQueues (E:\homaso\node_modules\listener-collection\node_modul
    es\bluebird\js\release\async.js:143:10)
    at Immediate.Async.drainQueues (E:\homaso\node_modules\listener-collection\n
    ode_modules\bluebird\js\release\async.js:17:14)
    at runCallback (timers.js:672:20)
    at tryOnImmediate (timers.js:645:5)
    at processImmediate [as _immediateCallback] (timers.js:617:5)



  • Please take a look to documentation, for the basic example
    https://jsreport.net/learn/adapting-jsreport#rendering

    jsreport.init().then(function () {     
       jsreport.render({
           template: {
               content: '<h1>Hello {{:foo}}</h1>',
               engine: 'jsrender',
               recipe: 'phantom-pdf'
            },
            data: {
                foo: "world"
            }
        }).then(function(resp) {
         // write report buffer to a file 
         fs.writeFileSync('report.pdf', resp.content)    
       });
    }).catch(function(e) {
      console.log(e)
    })
    


  • thank u jan_blaha...
    i can render pdf file but i want to render data in dynamically in html page but i got an error

    Unhandled rejection TypeError: Error during rendering report: string.match is no
    t a function
    at module.exports (E:\homaso\node_modules\async-replace\async-replace.js:30:

    1. at tryCatcher (E:\homaso\node_modules\jsreport-assets\node_modules\bluebird
      js\release\util.js:16:23)
      at ret (eval at makeNodePromisifiedEval (E:\homaso\node_modules\jsreport-ass
      ets\node_modules\bluebird\js\release\promisify.js:184:12), <anonymous>:13:39)
      at evaluateAssets (E:\homaso\node_modules\jsreport-assets\lib\assets.js:66:1
    2. at E:\homaso\node_modules\jsreport-assets\lib\assets.js:267:14
      at tryCatcher (E:\homaso\node_modules\listener-collection\node_modules\blueb
      ird\js\release\util.js:16:23)
      at Promise._settlePromiseFromHandler (E:\homaso\node_modules\listener-collec
      tion\node_modules\bluebird\js\release\promise.js:510:31)
      at Promise._settlePromise (E:\homaso\node_modules\listener-collection\node_m
      odules\bluebird\js\release\promise.js:567:18)
      at Promise.settlePromise0 (E:\homaso\node_modules\listener-collection\node
      modules\bluebird\js\release\promise.js:612:10)
      at Promise.settlePromises (E:\homaso\node_modules\listener-collection\node
      modules\bluebird\js\release\promise.js:691:18)
      at Async._drainQueue (E:\homaso\node_modules\listener-collection\node_module
      s\bluebird\js\release\async.js:133:16)
      at Async._drainQueues (E:\homaso\node_modules\listener-collection\node_modul
      es\bluebird\js\release\async.js:143:10)
      at Immediate.Async.drainQueues (E:\homaso\node_modules\listener-collection\n
      ode_modules\bluebird\js\release\async.js:17:14)
      at runCallback (timers.js:672:20)
      at tryOnImmediate (timers.js:645:5)
      at processImmediate [as _immediateCallback] (timers.js:617:5)

    my code:
    var html = fs.readFileSync('./test.html')
    jsreport.render({
    template: {
    content:html
    engine: 'jsrender',
    recipe: 'phantom-pdf'
    },
    data: {
    foo: "world"
    }
    }).then(function (resp) {
    // write report buffer to a file
    console.log("*********************" + resp)

                                    var read = fs.createWriteStream('report.pdf')
                                    resp.stream.pipe(read);
                                });
    

    can i do like this way


  • administrators

    }).then(function (resp) {
    // write report buffer to a file
    console.log("*********************" + resp)
    
                                    var read = fs.createWriteStream('report.pdf')
                                    resp.stream.pipe(read);
                                });
    

    can i do like this way

    yes, your code is fine.

    About the error Unhandled rejection TypeError: Error during rendering report: string.match is no t a function, you are getting the error because it seems like you are using handlebars syntax in your html content but you are trying to render it with the jsrender engine, so try to keep the syntax in your template and the engine input value in the same way, either using handlebars or jsrender.



  • thank u sooo... for u are supporting


Log in to reply
 

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