Sending pdf on email us not working

290 views
Skip to first unread message

sham...@gmail.com

unread,
Mar 6, 2017, 12:48:06 AM3/6/17
to jsreport
I am referring to https://jsreport.net/blog/sending-reports-periodically-in-email

And here is the sending mail code
JS reprot

Sharmishtha Chandane
Mon 3/6/2017 11:14 AM
Inbox; Sent Items
To:
Sharmishtha Chandane;
https://jsreport.net/blog/sending-reports-periodically-in-email


function toJSON(data) {
return JSON.stringify(data);
}

function beforeRender(done) {
require('request')({ url: 'http://127.0.0.1:5488/api/report/getTemplate', json:true },function(err, response, body) {
request.template.data = body;
done();
});
}
function afterRender( done) {
var SendGrid = require('sendgrid');
var sendgrid = new SendGrid('', '');

sendgrid.send({ to: 'sharmishth...@arrkgroup.com', from: 'sharmishth...@arrkgroup.com', subject: 'Sending PDF template',
html: 'This is your report',
files: [ {filename: 'Report.pdf', content: new Buffer(response.content) }]
}, function(success, message) {
done(success);
});
}

And log:
JS report log


2017-03-06T05:16:57.493Z - info: Starting rendering request 9 (user: null) requestId=9
2017-03-06T05:16:57.494Z - info: Rendering template {shortid:S1tR2CfYg, recipe:phantom-pdf,engine:handlebars} requestId=9
2017-03-06T05:16:57.494Z - debug: Data item not defined for this template. requestId=9
2017-03-06T05:16:57.495Z - debug: Executing script rJpI_05Fe requestId=9
2017-03-06T05:16:57.788Z - debug: Resources not defined for this template. requestId=9
2017-03-06T05:16:57.788Z - debug: Rendering engine handlebars requestId=9
2017-03-06T05:16:58.057Z - debug: Compiled template not found in the cache, compiling timestamp=Mon Mar 06 2017 10:46:58 GMT+0530 (India Standard Time), requestId=9
2017-03-06T05:16:58.059Z - debug: Replaced images [] requestId=9
2017-03-06T05:16:58.059Z - debug: Executing recipe phantom-pdf requestId=9
2017-03-06T05:17:02.700Z - debug: Converting in dedicated phantomjs 1.9.8 timestamp=Mon Mar 06 2017 10:46:58 GMT+0530 (India Standard Time), requestId=9
2017-03-06T05:17:02.703Z - debug: Request file:///C%3A%5CUsers%5CSHARMI~1%5CAppData%5CLocal%5CTemp%5Cjsreport-temp%5C1f0928c0-022c-11e7-9bac-1f6887691137html.html timestamp=Mon Mar 06 2017 10:46:58 GMT+0530 (India Standard Time), requestId=9
2017-03-06T05:17:02.705Z - debug: Request http://code.jquery.com/jquery-1.11.0.min.js timestamp=Mon Mar 06 2017 10:46:58 GMT+0530 (India Standard Time), requestId=9
2017-03-06T05:17:02.706Z - debug: Request http://code.highcharts.com/highcharts.js timestamp=Mon Mar 06 2017 10:46:58 GMT+0530 (India Standard Time), requestId=9
2017-03-06T05:17:02.708Z - debug: phantom-pdf recipe finished with 1 pages generated requestId=9
2017-03-06T05:17:02.710Z - debug: Skipping storing report. requestId=9
2017-03-06T05:17:02.715Z - info: Rendering request 9 finished in 5223 ms requestId=9
2017-03-06T05:18:36.323Z - debug: OData query on templates
2017-03-06T05:18:36.332Z - debug: OData query on settings
2017-03-06T05:24:54.114Z - debug: OData query on templates
2017-03-06T05:24:54.133Z - debug: OData query on settings


What I am doing here?

Jan Blaha

unread,
Mar 6, 2017, 1:15:08 AM3/6/17
to jsreport, sham...@gmail.com
And you receive no email, right?

Please try to add some console.log and check what is returned from sendgrid. You should see the output if you run the template with debug button.

sendgrid.send({ to: 'sharmishtha.chandane@arrkgroup.com',  from: 'sharmishtha.chandane@arrkgroup.com', subject: 'Sending PDF template',

          html: 'This is your report',
          files: [ {filename: 'Report.pdf', content: new Buffer(response.content) }]
  }, function(success, message) {  
          console.log(success)
          console.log(message)       
          done(success);
  });

sham...@gmail.com

unread,
Mar 6, 2017, 2:32:44 AM3/6/17
to jsreport
Thanks for quick reply.
I have given console.log inside beforeRender and afterRender. Still it's not printing anything in debug. Is it not going inside?

Jan Blaha

unread,
Mar 6, 2017, 2:41:40 AM3/6/17
to jsreport, sham...@gmail.com
Do you have the script assigned to the template? - open template and add script in the left menu

The console.log should definitely be displayed in the debug. At least for couple of last versions....

sham...@gmail.com

unread,
Mar 6, 2017, 5:31:53 AM3/6/17
to jsreport
Yah now I am able to see tha logs. Thanks. Sendgrid implementation has change with new version. So currently looking into that.

Jan Blaha

unread,
Mar 6, 2017, 5:35:02 AM3/6/17
to jsreport, sham...@gmail.com
Btw you don't have to stick with sendgrid, you can use nodemailer or what you prefer.

But please post here the updated sendgrid code if you make it working.

sham...@gmail.com

unread,
Mar 6, 2017, 8:14:47 AM3/6/17
to jsreport
Yah, I am also thinking of using nodemailer instead. Else will attached the updated code here.
Can I access this created report directly without hitting Run button available on jsreport UI?

Jan Blaha

unread,
Mar 6, 2017, 8:35:26 AM3/6/17
to jsreport, sham...@gmail.com
I don't understand this question

sham...@gmail.com

unread,
Mar 6, 2017, 12:31:03 PM3/6/17
to jsreport
Sorry to create confusion. My requirement is from mobile app I am going to send some parameters and form a sql query and from that pdf report should generate and send on perticular email. This has to happen on single click on mobile button. How I can achieve this? How can I call the jsreport?

Boris Matos Morillo

unread,
Mar 6, 2017, 12:54:19 PM3/6/17
to jsreport, sham...@gmail.com
Hi! jsreport has a REST API which you can call directly from your mobile app, or maybe from the API that is serving your mobile app.

you can find the relevant documentation here: https://jsreport.net/learn/api

Boris Matos Morillo

unread,
Mar 6, 2017, 12:56:27 PM3/6/17
to jsreport, sham...@gmail.com
You can use the node.js client https://jsreport.net/learn/nodejs-client too if you are going to call jsreport from a node.js app

sham...@gmail.com

unread,
Mar 6, 2017, 1:16:31 PM3/6/17
to jsreport
Ok thank. Will look into it.

sham...@gmail.com

unread,
Mar 8, 2017, 6:08:28 AM3/8/17
to jsreport

HI,
I am facing thin issue. When I post the following request the I am getting following error in response.

http://127.0.0.1:5488/api/report
{
"template": {"shortid":"S1tR2CfYg"},
"data": { },
"options": { "saveResult" : "true" }
}

{
"message": "Error during rendering report: Cannot set property 'message' of null",
"stack": "TypeError: Cannot set property 'message' of null\n at merge (D:\\nodejs\\node_modules\\jsreport\\node_modules\\jsreport-scripts\\lib\\scripts.js:195:22)\n at merge (D:\\nodejs\\node_modules\\jsreport\\node_modules\\jsreport-scripts\\lib\\scripts.js:197:13)\n at merge (D:\\nodejs\\node_modules\\jsreport\\node_modules\\jsreport-scripts\\lib\\scripts.js:197:13)\n at D:\\nodejs\\node_modules\\jsreport\\node_modules\\jsreport-scripts\\lib\\scripts.js:205:7\n at tryCatcher (D:\\nodejs\\node_modules\\jsreport\\node_modules\\jsreport-scripts\\node_modules\\bluebird\\js\\release\\util.js:16:23)\n at Promise._settlePromiseFromHandler (D:\\nodejs\\node_modules\\jsreport\\node_modules\\jsreport-scripts\\node_modules\\bluebird\\js\\release\\promise.js:510:31)\n at Promise._settlePromise (D:\\nodejs\\node_modules\\jsreport\\node_modules\\jsreport-scripts\\node_modules\\bluebird\\js\\release\\promise.js:567:18)\n at Promise._settlePromise0 (D:\\nodejs\\node_modules\\jsreport\\node_modules\\jsreport-scripts\\node_modules\\bluebird\\js\\release\\promise.js:612:10)\n at Promise._settlePromises (D:\\nodejs\\node_modules\\jsreport\\node_modules\\jsreport-scripts\\node_modules\\bluebird\\js\\release\\promise.js:691:18)\n at Promise._fulfill (D:\\nodejs\\node_modules\\jsreport\\node_modules\\jsreport-scripts\\node_modules\\bluebird\\js\\release\\promise.js:636:18)\n at D:\\nodejs\\node_modules\\jsreport\\node_modules\\jsreport-scripts\\node_modules\\bluebird\\js\\release\\nodeback.js:42:21\n at ChildProcess.<anonymous> (D:\\nodejs\\node_modules\\jsreport\\node_modules\\jsreport-core\\node_modules\\script-manager\\lib\\manager-processes.js:41:20)\n at emitTwo (events.js:87:13)\n at ChildProcess.emit (events.js:172:7)\n at internal/child_process.js:696:12\n at nextTickCallbackWith0Args (node.js:420:9)\n at process._tickCallback (node.js:349:13)"
}

What's missing here?

sham...@gmail.com

unread,
Mar 8, 2017, 12:13:58 PM3/8/17
to jsreport
While calling jsreport api, getting error.

Boris Matos Morillo

unread,
Mar 8, 2017, 1:07:32 PM3/8/17
to jsreport, sham...@gmail.com
Hi! are you sure that you don't have an error in any of your scripts? if possible, can you upload your example in https://playground.jsreport.net/ and copy us the link, so we can take a quick look and test

sham...@gmail.com

unread,
Mar 8, 2017, 10:17:15 PM3/8/17
to jsreport
I have added my code there. But I don't think it's giving that error due to something wrong in my code. I have hit http://localhost/api/jsreport & in request passed the template name. This I was doing on postman. There only I am getting error. Can you check doing same at you end? Hitting jsreport and what you get in rsponse when u pass pdf template.

Thanks

sham...@gmail.com

unread,
Mar 9, 2017, 7:00:10 AM3/9/17
to jsreport
Hi,
Did you get a chance to go through my code? And also the error which I am getting. Any help will be much appreciated.
Thanks

sham...@gmail.com

unread,
Mar 9, 2017, 7:02:32 AM3/9/17
to jsreport

Boris Matos Morillo

unread,
Mar 9, 2017, 10:48:01 AM3/9/17
to jsreport, sham...@gmail.com
Hi, sorry but i think you forgot to send me the link of the playground, it should be something like this picture.
Captura de pantalla 2017-03-09 a las 10.45.16 a.m..png

sham...@gmail.com

unread,
Mar 9, 2017, 10:03:10 PM3/9/17
to jsreport
Here is a link
https://playground.jsreport.net/studio/workspace/ByQTUHC9l/2
But still why /api/report getting error.
As when I run template with data, it shows me pdf generated with pie chart. That api should return me template right?
Let me know your findings.

Thanks

sham...@gmail.com

unread,
Mar 10, 2017, 6:54:43 AM3/10/17
to jsreport
Hi,
Do you able to see my code?

Thanks

Boris Matos Morillo

unread,
Mar 10, 2017, 9:40:58 AM3/10/17
to jsreport, sham...@gmail.com

Hi, the code in playground has some errors and the scripts have been not assigned to the template, i've updated your example here: https://playground.jsreport.net/studio/workspace/ByQTUHC9l/10 

now, i have some questions, you have a script "renderPDF", why are you calling "http://127.0.0.1:5488/api/report" in "beforeRender"? i thought that you are trying to call jsreport API from another application, if that is your intention you should send a request to "http://127.0.0.1:5488/api/report" in that application not in jsreport's script.

Also, i have checked that your example (with the changes that i have made in https://playground.jsreport.net/studio/workspace/ByQTUHC9l/10) can be called from jsreport API.

Boris Matos Morillo

unread,
Mar 10, 2017, 9:42:35 AM3/10/17
to jsreport, sham...@gmail.com
the API would return you the generated report  (PDF)

sham...@gmail.com

unread,
Mar 14, 2017, 8:16:48 AM3/14/17
to jsreport
Thanks for your valuable reply. Cleared all my confusion. In case of fetching data from sqlite, can we form a query by getting the parameters from request inside jsreport script?

Boris Matos Morillo

unread,
Mar 14, 2017, 10:22:26 AM3/14/17
to jsreport, sham...@gmail.com
yes, that sounds possible, just read from the request object in the script, also you can centralize that query in your own API and in jsreport script just call that API. 

sham...@gmail.com

unread,
Mar 24, 2017, 2:13:14 AM3/24/17
to jsreport
Hi,
I have successfully able to send a link to generated pdf in email. This is working locally. Now I have to implement this on server. I can install jsreport on the server but how I can create template as it don't have UI.
How to deal with this? Where created template gets save? Can I copy jsreport folder to server? Will it do the job?

Boris Matos Morillo

unread,
Mar 24, 2017, 10:05:53 AM3/24/17
to jsreport, sham...@gmail.com
Hi, yes you can copy jsreport folder to the server. templates are saved in "data/templates" directory but you will need to copy the entire "data" folder or the directory containing your jsreport proyect.

sham...@gmail.com

unread,
Mar 31, 2017, 12:33:13 AM3/31/17
to jsreport
Hi, I am getting below error while running jsreport on out test server
js report

Sharmishtha Chandane
Fri 3/31/2017 9:14 AM
Inbox; Sent Items
To:
Sharmishtha Chandane;
2017-03-31T03:39:29.620Z - error: Error when processing render request Error during rendering report: spawn /home/developer/node_js/node_modules/jsreport/node_modules/jsreport-phantom-pdf/node_modules/phantom-html-to-pdf/node_modules/phantomjs/lib/phantom\phantomjs.exe ENOENT Error: spawn /home/developer/node_js/node_modules/jsreport/node_modules/jsreport-phantom-pdf/node_modules/phantom-html-to-pdf/node_modules/phantomjs/lib/phantom\phantomjs.exe ENOENT
at exports._errnoException (util.js:1034:11)
at Process.ChildProcess._handle.onexit (internal/child_process.js:193:32)
at onErrorNT (internal/child_process.js:367:16)
at _combinedTickCallback (internal/process/next_tick.js:80:11)
at process._tickCallback (internal/process/next_tick.js:104:9) requestId=3
2017-03-31T03:39:29.622Z - error: Error during processing request: http://10.0.1.146:5488/api/report details: Error during rendering report: spawn /home/developer/node_js/node_modules/jsreport/node_modules/jsreport-phantom-pdf/node_modules/phantom-html-to-pdf/node_modules/phantomjs/lib/phantom\phantomjs.exe ENOENT Error: spawn /home/developer/node_js/node_modules/jsreport/node_modules/jsreport-phantom-pdf/node_modules/phantom-html-to-pdf/node_modules/phantomjs/lib/phantom\phantomjs.exe ENOENT
at exports._errnoException (util.js:1034:11)
at Process.ChildProcess._handle.onexit (internal/child_process.js:193:32)
at onErrorNT (internal/child_process.js:367:16)
at _combinedTickCallback (internal/process/next_tick.js:80:11)
at process._tickCallback (internal/process/next_tick.js:104:9)

Boris Matos Morillo

unread,
Mar 31, 2017, 10:29:02 AM3/31/17
to jsreport, sham...@gmail.com
Hi, the error says that jsreport could not find the phantomjs executable at "/home/developer/node_js/node_modules/jsreport/node_modules/jsreport-phantom-pdf/node_modules/phantom-html-to-pdf/node_modules/phantomjs/lib/phantom\phantomjs.exe", to solve this and if your project directory is "/home/developer/node_js/" try to run "npm install" on your server again, this should install missing dependencies, if that doesn't work try to delete the "/home/developer/node_js/node_modules" folder and run "npm install" (assuming that you have a "package.json" file in "/home/developer/node_js") to force a fresh install
Reply all
Reply to author
Forward
0 new messages