I am taking a webpage, using jquery to load the page on document.ready, css to format the page, node.js http to retrieve the page from the server, html-pdf to convert the html to a pdf file. From this point, I am sending the file as an email attachment. The ultimate goal is to create an invoice. The issue is that the css and js do not appear to be processing when it is in external files. I would prefer to keep in external files but need jquery to populate the page on document.ready and the css to properly format the page. Also, I added cheerio to the mix to see if that would make sure the processing occurred correctly and it did not help. The code is outlined below:
email sending code:
var transporter = nodemailer.createTransport({
service: 'gmail',
host: this.hostname,
auth: {
user: this.smtpusername,
pass: this.smtppassword
}
});
var mailOptions = {
from: fromaddress, // sender address
to: toaddresslist, // list of receivers
subject: subject, // Subject line
html: text
};
if (attachments){
mailOptions.attachments = [
{ // binary buffer as an attachment
filename: 'invoice.pdf',
content: attachments,
contentType: 'application/pdf',
encoding: 'base64'
}
];
}
html conversion code:
let body = [];
var options = {
host: 'localhost',
path: '/invoice/' + invoiceid,
port: '3000'
};
http.request(options, function(response){
response.on('data', function (chunk) {
body.push(chunk);
});
response.on('end', function () {
let buffer = '';
buffer = Buffer.concat(body).toString();
let $ = cheerio.load(buffer);
let newdocument = $.html();
console.log(newdocument);
pdf.create(newdocument, {
directory: "tmp",
format: "letter",
orientation: "portrait",
zoomFactor: ".5", // default is 1
type: "pdf",
quality: "75"
}).toBuffer(function(err, newbuffer){
if (err){
reject(err);
}
if (Buffer.isBuffer(newbuffer)){
resolve(newbuffer);
} else {
reject(new Error('The pdf file could not be generated at this time. Please try again later.'));
}
});
});
})
.end();
When I check the email, I am getting the pdf, but is does not appear populated with the data I expect from the api call and the formatting is way off. I tried to move the css code inside the document for testing purposes, and though the css was still off, it was clear it was able to see the css in this scenario.
1) How can I get html-pdf package to recognize and process the external css and js files?
2) What type of html formatting does the html-pdf work with, div or tables?