I am trying to render a pdf on the server and then attach it to an email.
My code so far:
Meteor.methods({
'generate_invoice_pdf': function(id) {
// SETUP
// Grab required packages
var webshot = Meteor.npmRequire('webshot');
var fs = Npm.require('fs');
var Future = Npm.require('fibers/future');
var fut = new Future();
// TODO on server this file can't be created
// { [Error: ENOENT, open 'invoice.pdf'] errno: 34, code: 'ENOENT', path: 'invoice.pdf' }
console.log(process.cwd()); // /opt/myAppName/app/programs/server
var fileName = 'invoice.pdf';
// PREPARE DATA
var data = Invoices.findOne(id);
if (!data) {
throw new Meteor.Error('Invoice does not exist');
}
var template;
'card' in data ? template = 'invoice_credit_card' : template = 'invoice_on_account';
// GENERATE HTML STRING
var css = Assets.getText('invoice_style.css');
SSR.compileTemplate('layout', Assets.getText('pdf_layout.html'));
SSR.compileTemplate('invoice_comp_tmpl', Assets.getText(template + '.html'));
var html_string = SSR.render('layout', {
css: css,
template: "invoice_comp_tmpl",
data: data
});
// Setup Webshot options
var options = {
"paperSize": {
"format": "Letter",
"orientation": "portrait",
"margin": "1cm"
},
siteType: 'html'
};
// Commence 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);
});
});
// Convert to base64
var pdfData = fut.wait();
var b = new Buffer(pdfData).toString('base64');
return b;
}
});
On my local machine this works fine.
Do I have to create the file in the public folder?
How would I do that?
Or where can I let server code create new files?
why should it look for the file?
it does not existā¦
and why are there permission issues? canāt I just allow my app to create/edit files as it wishes? maybe via ssh and chmod?