javascript - Generate PDF Report in Meteor js -
i close generate pdf report in meteor js many folks out there. code runs till end, ends having error
{ [error: enoent, open 'report.pdf'] errno: 34, code: 'enoent', path: 'report.pdf' }
below code snippet using generate pdf list.
layout.html
<head> <style type="text/css"> {{css}} </style> </head> <body> {{> template.dynamic template=template data=data}} </body>
report.html
<body> <div class="container"> {{#each voters}} <h4>{{name}}</h4><br> {{/each}} </div> </body>
server/method.js
var total = voters.find() meteor.methods({'generate_pdf': function() { console.log("--------inside pdf generation module----------"); var fs = npm.require('fs'), future = npm.require('fibers/future'), fut = new future(), filename = "report.pdf", voters = total, data = { voters: voters } ssr.compiletemplate('layout', assets.gettext('layout.html')); ssr.compiletemplate('report', assets.gettext('report.html')); var html_string = ssr.render('layout', { template: "report", data: data }); var options = { "papersize": { "format": "letter", "orientation": "portrait", "margin": "1cm" }, sitetype: 'html' }; console.log("------------commencing webshot-----------"); webshot(html_string, filename, options, function(err) { fs.readfile(filename, function (err, data) { if (err) { return console.log(err); } fs.unlinksync(filename); fut.return(data); }); }); console.log("------------waiting till pdf generated-----------"); let pdfdata = fut.wait(); let base64string = new buffer(pdfdata).tostring('base64'); console.log("------------return result-----------"); return base64string; } });
triggerpoint button : home.html
<template name="home"> <div class="container"> <div class="row"> <div class="col-sm-6 col-sm-offset-3"> <a href="#" class="generate-pdf">generate pdf</a> </div> </div> </div> </template>
home.js
template.home.events({ 'click .generate-pdf': function(e, tmpl) { e.preventdefault(); meteor.call('generate_pdf', function(err, res) { if (err) { console.error(err); } else if (res) { window.open("data:application/pdf;base64, " + res); } }) } });
error log on console :
=> meteor server restarted => client modified -- refreshing i20160509-16:17:04.639(5.5)? --------inside pdf generation module---------- i20160509-16:17:14.617(5.5)? ------------commencing webshot----------- i20160509-16:17:14.622(5.5)? ------------waiting till pdf generated----------- i20160509-16:17:14.712(5.5)? { [error: enoent, open 'report.pdf'] errno: 34, code: 'enoent', path: 'report.pdf' }
- as per understanding error thrown @ line fs.readfile(filename, function (err, data) { in file method.js
- while not able read file might not generated. can me out this, if case solved many in benefit ready boiler plate code here.
initially had below meteor packages create pdf using phantomjs , display on seperate tab: [for meteor version 1.3.2.4]
1. dfischer:phantomjs
2. meteorhacks:ssr
3. jaredmartin:future
4. meteorhacks:npm
5. bryanmorgan:webshot
after hours of debugging, found out had remove package bryanmorgan:webshot , manually add package entry "webshot":"0.15.4" in project/packages.json file. in short, had add npm's webshot meteor project.
then, had restart meteor , worked.
Comments
Post a Comment