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

Ritesh Jagga picture Ritesh Jagga · Oct 22, 2015 · Viewed 7.6k times · Source

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

var pdfDoc = printer.createPdfKitDocument(docDefinition);
pdfDoc.pipe(fs.createWriteStream('pdfs/absolute.pdf'));
pdfDoc.end();

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.

Answer

mithril_knight picture mithril_knight · Oct 22, 2015

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

var pdfDoc = printer.createPdfKitDocument(docDefinition); 
pdfDoc.end();

and then:

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