where to add custom scripts in nodejs code, how to like custom scripts to template

77 views
Skip to first unread message

mani...@gmail.com

unread,
Mar 10, 2017, 2:43:52 PM3/10/17
to jsreport
Hi, 

I am able to render the pdf but not able to send an email after render. I dont know where to put the custom scripts code in my nodejs code. I am calling scripts in the jsreport render, but nothing is happening. Currently my code is as follow.

var http = require('http');
var jsreport = require('jsreport');

var fs = require('fs');
var contents = fs.readFileSync('template.html').toString();
console.log(contents);
console.log('*********************');
var scripts = fs.readFileSync('scripts.js').toString();
console.log(scripts);

http.createServer(function (req, res) {

    jsreport.render({
            "template": {
                "content": contents,
                "engine": "handlebars",
                "phantom": {
                    "orientation": "potrait",
                    "width": "350px"
                },
                "recipe": "phantom-pdf"
            },
            "data": {
                "items": [{
                    "journeyCount": 168,
                    "journeyDate": "03-09-2017"
                }, {
                    "journeyCount": 267,
                    "journeyDate": "03-08-2017"
                }, {
                    "journeyCount": 252,
                    "journeyDate": "03-07-2017"
                }, {
                    "journeyCount": 303,
                    "journeyDate": "03-06-2017"
                }, {
                    "journeyCount": 58,
                    "journeyDate": "03-05-2017"
                }, {
                    "journeyCount": 143,
                    "journeyDate": "03-04-2017"
                }, {
                    "journeyCount": 352,
                    "journeyDate": "03-03-2017"
                }]
            },
             "scripts":"scripts"

        })
        .then(function (out) {
            out.stream.pipe(res);
        })
        .catch(function (e) {
            res.end(e.message);
        });

}).listen(1337, '127.0.0.1');

and scripts code is as follows: 

//send the pdf report by mail
function afterRender(req, res, done) {
  var SendGrid = require('sendgrid');
  var sendgrid = new SendGrid('', '');
  console.log('***********************RUNNING SCRIPTS *******************');
  sendgrid.send({ to: '',  from: '', subject: 'test',
          html: 'This is your report',
          files: [ {filename: 'Report.pdf', content: new Buffer(res.content) }]
  }, function(success, message) {          
          done(success);
  });
}

Boris Matos Morillo

unread,
Mar 11, 2017, 11:21:21 AM3/11/17
to jsreport
here is a basic example https://github.com/bjrmatos/jsreport-send-mail using your code as base. just add you sendgrid API KEY and change the emails, also if you are using the browser to start the report rendering http://localhost:1337 you will have 2 reports because the browser will do two requests by default, you will need to adjust your http.createServer function to handle that case, but anyway that is something that is not jsreport related.

Manin Shah

unread,
Mar 14, 2017, 10:05:40 AM3/14/17
to jsreport
Thanks a lot!! this is exactly what i needed. 
Reply all
Reply to author
Forward
0 new messages