Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am using PDFKit and NodeJS to dynamically generate PDF documents, and I would like to attach said document to an email. At this moment I'm using Mandril-API via NPM.

I can generate the PDF without issue and display it in the browser via:

doc.pipe( res );

I can send an email without issue, but I have failed miserably at getting the proper PDF content. I am fairly certain that I am 99% of the way there - but I'm missing something. I have done a ton of reading and testing using Google/StackOverflow etc but I'm stuck.

I am getting content that when I do a base64 decode I get:

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

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

--_av-Ti-H6i8tBBHL4BgoXnyC2Q Content-Type: application/pdf Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="mytestPDF.pdf"

PDF1370obj/Predictor15/Colors1/BitsPerComponent8/Columns100e ndobj60obj/Type/XObject/Subtype/Image/BitsPerComponent8/Widt h100/Height19/Filter/FlateDecode/DecodeParms70R/ColorSpace/I ndexed/DeviceRGB25580R/Length1751streamxdSSNEhGIRTRkWbY/nHaO MJln7t+vv89ylF111PlYNB9Nm6e9DENsd9FxLFUbOjrgt+ErRgWtj9vPCTBH oohMHl9oZ7IdpC/hxusjTHFFMcxhwIxPlbNorOB+bH8exrrA1DUnzKzq/UXI xT456nxtB59fQNiIrBT2apETJZieZvltpeThrObiZ4ydtY0koKJ2Epb940A1 iXyehONQVXiZr8jRP/NJ3bmjHA0sygAou4Q=

Although I've messed around for hours on this, my best hunch is that I have line break/new line errors in my PDF content. The way I'm getting my PDF content is by creating an array called buffers, then: doc.on('data', buffers.push.bind(buffers));

I'm supposing that I need to be adding /n or /r etc...but I've been working with NodeJS and AngularJS for a month or so now and I know almost EVERYTHING I do wrong is because I'm over-complicating the matter...so I turn to you folks and hope that there is a simple method to attach the content from the new PDFDocument I create with PDFKit to an email using NodeJS.

Thank you in advance...please forgive my rambling, but I started this about 8 hours ago (it's now 3:25am my time). :)

like image 703
user2348688 Avatar asked Sep 17 '14 10:09

user2348688


1 Answers

I recently had the same problem with posting an email pdf attachment to mandrill through node.js but 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);
        });
    });

I hope this helps. Ping if you need additional help =)

like image 106
mraxus Avatar answered Sep 18 '22 20:09

mraxus