Nodejs pdfkit Attach Dynamically Generated PDF to Email (Mandrill-API)

I use PDFKit and NodeJS to dynamically create PDF documents, and I would like to attach the specified document to the email. I am currently using the Mandril-API through NPM.

I can generate a PDF without any problems and display it in a browser with:

doc.pipe (res);

I can send an email without a problem, but I was mistaken in getting the correct PDF content. I am absolutely sure that I am 99% from there, but I am missing something. I did a ton of reading and testing using Google / StackOverflow etc., but I'm stuck.

I get the content that when I do base64 decoding, I get:

% PDF-1.3% 7 0 obj </ Predictor 15

I managed to get my PDF attachment to have a valid size of 445 KB but this email content:

--_ av-Ti-H6i8tBBHL4BgoXnyC2Q Content-Type: application / pdf Content-Transfer-Encoding: base64 Content-Disposition: attachment; file name = "mytestPDF.pdf"

PDF1370obj / Predictor15 / tsveta1 / BitsPerComponent8 / Columns100e ndobj60obj / Type / XObject / Subtype / Image / BitsPerComponent8 / width in h100 / Height19 / Filter / FlateDecode / DecodeParms70R / ColorSpace / I ndexed / DeviceRGB25580R / Length1751streamxdSSNEhGIRTRkWbY / nHaO MJln7t + vv89ylF111PlYNB9Nm6e9DENsd9FxLFUbOjrgt + ErRgWtj9vPCTBH oohMHl9oZ7IdpC / hxusjTHFFMcxhwIxPlbNorOB + bH8exrrA1DUnzKzq / Uxi xT456nxtB59fQNiIrBT2apETJZieZvltpeThrObiZ4ydtY0koKJ2Epb940A1 iXyehONQJXjijjjjhi

Even though I have been fucking for hours on this, my best guess is that I have a line break / new line errors in my PDF content. The way I get my PDF content is to create an array called buffers, and then: doc.on ('data', buffers.push.bind (buffers));

I suppose I need to add / n or / r, etc., but I have been working with NodeJS and AngularJS for a month or so and I know almost EVERYTHING that I am doing wrong, I am complicating the question too much ... so I am contacting you guys and hope that there is an easy way to attach content from the new PDFDocument that I create using PDFKit to email using NodeJS.

Thank you in advance ... please forgive my incoherence, but I started this about 8 hours ago (now 3:25 a.m.). :)

+5
source share
1 answer

I recently had the same problem sending pdf email attachment to mandrill via node.js, but I managed to solve it.

Here is what I did:

generatePdf(inputData, function (err, doc) { if (err) return callback(err); var bufferChunks = []; doc.on('readable', function() { // Store buffer chunk to array bufferChunks.push(doc.read()); }); doc.on('end', function() { var pdfBuffer = Buffer.concat(bufferChunks), pdfBase64String = pdfBuffer.toString('base64'); // This string is perfectly ok to use as an attachment to the mandrillAPI sendMandrillEmailWithAttachment(pdfBase64String); }); }); 

Hope this helps. Ping if you need more help =)

+9
source

Source: https://habr.com/ru/post/1202752/


All Articles