@hodaraadam Alright, I figured out a solution that seems to be working really well. I have a private directory in the top level of my app with an html file called ‘case-report.html’. That file is an html page that I’m using to generate a PDF via webshot. It is styled with styles in that file. I actually just copied all the css from my main app into a style tag in the head section of that html page. I then created a route with the following code:
I guess I should also mention that I had to install "webshot": "0.16.0" via meteorhacks:npm, dfischer:phantomjs, and meteorhacks:ssr
Router.route('/cases/:_id/report/generatePDF', {
name: 'generatePDF',
where: 'server',
action: function() {
var webshot = Meteor.npmRequire('webshot');
var fs = Npm.require('fs');
var Future = Npm.require('fibers/future');
var fut = new Future();
var fileName = "case-report.pdf";
var curCase = Cases.findOne(this.params._id);
// Render the template on the server
SSR.compileTemplate('caseReport', Assets.getText('case-report.html'));
var html = SSR.render("caseReport", curCase);
// Setup Webshot options
var options = {
"paperSize": {
"format": "Letter",
"orientation": "portrait",
"margin": "1cm"
},
siteType: 'html'
};
webshot(html, fileName, options, function(err) {
fs.readFile(fileName, function (err, data) {
if (err) {
return console.log(err);
}
fs.unlinkSync(fileName);
fut.return(data);
});
});
this.response.writeHead(200, {'Content-Type': 'application/pdf',"Content-Disposition": "attachment; filename=generated.pdf"});
this.response.end(fut.wait());
}
});
I then have a link on the page that I want to generate the PDF from:
<a href="{{pathFor 'generatePDF'}}" class="waves-effect waves-light btn download">Download Case</a>
Because I am doing it this way, I can actually have another route with the exact same code as the case-report.html file and show users a preview of what the PDF will look like.