Node.js html-pdf conversion problem, file returned damaged

I am trying to create an application that will take an html file that is populated using a jquery request. I am making an http get request to get the html by passing the string to the pdf.create function and using the generated buffer to send the PDF file by email. This seems to process and send the file as an email attachment, however, when I try to open the file, I get an error message when the file is damaged.

code that convert html document to pdf: var options = { host: 'localhost', path: '/salesorder/' + orderid, port: '3000' }; http.request(options, function(response){ let buffer = ''; response.on('data', function (chunk) { buffer += chunk; }); response.on('end', function () { pdf.create(buffer, { directory: "tmp" }).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(); code that sends the email: 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 text: text }; if (attachments){ mailOptions.attachments = [ { // binary buffer as an attachment filename: 'invoice.pdf', content: new Buffer(attachments.toString(), 'base64'), contentType: 'application/pdf' } ]; } 

I am using the html-pdf node.js package to perform the conversion. What am I missing here? I take the html that I get from the http.request method and I see the code generated if I print it using console.log. Now I notice that I do not see fillable text fields and shortcuts, so this is not like jquery processing.

+7
jquery pdf pdf-generation
source share
1 answer

I have no experience with nodemailer, but it seems to me that you are missing encoding: 'base64' in your application. See the docs here .

+4
source share

All Articles