Search code examples
javascriptreactjspdfnodemailerhtml2pdf

HTML2PDF with Nodemailer


Trying to send a generated PDF from eKoopman's HTML2PDF.js to send as an email attachment-- but can't get the resulting PDF to display. Have tried a lot of the solutions on Stack Overflow and elsewhere, but many tend to be outdated.

HTML2PDF:

html2pdf().set(opt).from(iac).toPdf().output('datauristring')
    .then(function(pdf) {
        emailDoc(pdf)
    })

Attempts at the relevant parts of the mail options from emailDoc():

attachments: [{
   filename: 'Name.pdf',
   content: Buffer.from(pdf).toString('base64')
}]

attachments: [{
   filename: 'Name.pdf',
   content: Buffer.from(pdf).toString('base64'),
   contentType: 'application/pdf'
}]

attachments: [{
   filename: 'Name.pdf',
   content: new Buffer(pdf, 'base64'),
   contentType: 'application/pdf'
}]

attachments: [{
   filename: 'Name.pdf',
   content: pdf,
   encoding: 'base64'
}]

Doesn't necessarily have to be the base64 method, just any combination that will make this work!


Solution

  • You may try to save your pdf first, then upload it using path description like,

      attachments: [{
        filename: 'file.pdf',
        path: '/path/to/file',
        contentType: 'application/pdf'
      }],
    

    Hope it helps!