How to use pdf output of PDFMake in the attachment mail options of NodeMailer?

nodemailer attachment pdf
pdfmake save pdf
pdfmake html to pdf
pdfmake server side
nodejs pdf
pdfmake download method
open pdf buffer
nodejs pdf generator

Here is a one of PDFMake's code snippet to create pdf file:

var pdfDoc = printer.createPdfKitDocument(docDefinition);

and here is one of the attachment option in NodeMailer:

  // stream as an attachment
  filename: 'text4.txt',
  content: fs.createReadStream('file.txt')

I am able to create pdf and save it to a file in a directory and attach that saved file in the e-mail but I want to directly send the output of pdf to the attachment content of e-mail without saving the pdf output to a file.

I tried to understand nodejs pipe and stream features but couldn't understand them to meet my requirement. I think it should be possible through stream and pipe but don't know how to achieve.

I want to prevent saving of pdf output to file because there can be multiple users using the functionality of creating pdf file and sending e-mail. There can be a situation when a pdf file created by one user will be overwritten by another user.

you could instead of pipe, pass the entire pdfdoc to the attachment

var pdfDoc = printer.createPdfKitDocument(docDefinition); 

and then:

attachments: {
    // stream as an attachment
    filename: 'text4.pdf',
    content: pdfDoc

Saving Generated pdfmake PDF or attaching to email · Issue #989 , createWriteStream('pdfs/absolute.pdf')); pdfDoc.end(); and here is one of the attachment option in NodeMailer: { // stream as an attachment filename: 'text4.txt',​  I've seen how I can use pdfMake.createPdf(def).open() to open a pdf in a new tab. Is there a function that returns the pdf file so I can send it as an attachment? This comment has been minimized.

I was able to get it to work using a buffer, and ending the buffer AFTER the email was sent with the attachment from PDFMake. If you end the buffer before the email is sent it will not work:

const printer = new pdfMakePrinter(fontDescriptors);
const doc = printer.createPdfKitDocument(docDefinition);
let buffers = [];
doc.on('data', buffers.push.bind(buffers));
doc.on('end', () => {
    let pdfData = Buffer.concat(buffers);

    let mailOptions = {
        from: '"John Doe" <>', // sender address
        to: '', // list of receivers
        subject: 'stuff' // Subject line
        text: '',
        html: '', // html body
        attachments: [{
            filename: 'attachment.pdf',
            content: pdfData

    transporter.sendMail(mailOptions, (error, info) => {
        if (error) {
            return console.log(error);
        console.log('Message sent: %s', info.messageId);
        console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info));


//end buffer

Saving the pdf as an attachment · Issue #720 · bpampuch/pdfmake , I have struggled for the past 3 days to get the generated pdf either saved in my local filesystem or emailed as an attachment to the user. Create the PDF convert to base64, do an http POST to my PHP server and then send the file as an attachment. I'm assuming that you are doing everything in the client so you need to to use the Blob.js and a file system library of some sort.

Here's what I did. It works in a Meteor app.

Client Side:

var docDefinition = {
    content: [
        'First paragraph'

var data;
pdfMake.createPdf(docDefinition).getBase64(function(encodedString) {
    data = encodedString;"sendPDFEmail", data);

Server Side

sendPDFEmail: function(thepdf){
        to: "receiver",
        from: "sender",
        subject: "Test",
        html: "text",
        attachments: [
                filename: "test.pdf",
                type: "application/pdf",
                content: Buffer.from(thepdf, 'base64')

Using attachments – Nodemailer, I want to send the actual file so I package the file up as a bas64 buffer and send it to my server where I use nodemailer to actually send it off. Nodemailer is a module for Node.js applications to allow easy as cake email sending. The project got started back in 2010 when there was no sane option to send email messages, today it is the solution most Node.js users turn to by default. Nodemailer is licensed under MIT license.

var pdfMake = require('pdfmake/build/pdfmake.js');
var pdfFonts = require('pdfmake/build/vfs_fonts.js');
pdfMake.vfs = pdfFonts.pdfMake.vfs;
//var base64 = require('base64-js');

var docDefinition = function(json) {
  return {
    content: [
      'PARAGRAPH 1',
      'paragraph 2', {
        columns: [
          'first column is a simple text', {
            stack: [
              // second column consists of paragraphs
              'paragraph A',
              'paragraph B',
              'these paragraphs will be rendered one below another inside the column'
            fontSize: 15

const pdfDocGenerator = pdfMake.createPdf(docDefinition);
pdfDocGenerator.getBuffer((pdfBuffer) => {
    //var pdfbase64 = base64.fromByteArray(buffer);
    //console.log("pdfDocGenerator.getBuffer OK" + pdfbase64);
    fs.writeFileSync('foo.pdf', buffer);

Generating PDF from HTML with Node.js and Puppeteer , attachment mail options of NodeMailer? I want to prevent saving of pdf output to file because there can be multiple users using the functionality of creating pdf  To add one or more attachments to your email, add an " attachments " property in the message object, that contains an array of attachment objects. You can add an utf-8 string as an attachment, a stream, a local file, or even a file from an URL address. Nodemailer Attachments. <<- Upload Files with Express ..

User mithril_knight, Useful if you want to use binary attachments in a JSON formatted e-mail object. headers – custom headers for the attachment node. Same usage as with message  Scenario : using pdfMake in AngularJS to create a pdf and it is working fine except for this code block, Continue reading. html, pdf-generation, pdfmake. Leave a comment.

html-pdf, about Puppeteer is that you can use it to Generate First, we launch the browser (PDF Setting the waitUntil: 'networkidle0' option means that Puppeteer considers await page.type('#email', process.env. 0px} #print-button {display: none}' })  Include the following information with your issue: Nodemailer version v2.3.2 Node.js version v4.4.2 OS Windows 8.1 So, I received mail but with corrupted pdf file. Bellow you can see my scriptcode

Top network posts. 8 twitter api 1.1 url count alternative · 5 How to use pdf output of PDFMake in the attachment mail options of NodeMailer? View more network  You can use NodemailerApp to instantly preview emails sent from your dev environment, be it from SMTP or sendmail or even from the open web via built-in catchall service. You can also upload any email to an external SMTP or IMAP server.

  • Uncaught Error { filename: undefined, line: NaN, row: NaN, message: 'Converting circular structure to JSON', type: 'TypeError', stack: 'TypeError: Converting circular structure to JSON\n at JSON.stringify (<anonymous>)\n at mandrill (C:\\Users\\MITVJADEV011\\Desktop\\react\\manyuBackEnd\\node_modules\\node-mandrill\\mandrill.js:28:39)\n at Object.sendMail (C:\\Users\\MITVJADEV011\\Desktop\\react\\manyuBackEnd\\services\\auth.service.js:87:2)
  • hi there @Anil . please correct me if I'm wrong. are you using node-mailer with a mandrill transport/plugin? I haven't really tested that setup, I used this approach for out of the box node-mailer setup. do you mind to share a little more details about your situation?
  • var mandrill = require('node-mandrill')('mykey'); var pdfDoc = printer.createPdfKitDocument(docDefinition);pdfDoc.end(); mailData['fileName'] = 'WoorOrderReminder.pdf'; mailData['fileType'] = 'application/pdf' mailData['fileContent'] = pdfDoc; attachments: [{ "type": mailData.fileType, "name": mailData.fileName, "content": mailData.fileContent }] this is my code i got above error
  • @Anil ok my man, one thing first: this question and answer were about node-mailer module, you are using a totally different one node-mandrill. no worries, look at the readme of the module you are using, it says it uses the mandrill api with a minimal overhead, I looked a bit into the source code and is just a little wapper, has no utils for handling files from streams or disc locations. and as the module says, you should read the official mandrill api docs, which says that attachments must be base64 string content. personal note: if the api has an official module use it. is mandrill-api
  • @Anil and also please go to devRant, will be nice to have you over there :)
  • From Review: Hi, please don't answer just with source code. Try to provide a nice description about how your solution works. See: How do I write a good answer?. Thanks