I saw this question but my code seems to be slightly different and not working. Sending a PDF created dynamically as an attachment using PDFKit in a nodejs application
the main difference is that i am sending this email from the backend not the browser.
Also to note that the file generated is genreated fine so it's probably an issue with passing it on to the email.
const Email = require('email-templates');
const PdfKit = require('pdfkit');
const SVGToPDF = require('svg-to-pdfkit');
const fs = require('fs');
const service = new Email({
message: {
from: 'aaron.test@gmail.com'
},
send: true
transport: require('../config/smtp.json') // my smtp config. Works without attachment
});
function generatePdfAndSend() {
var doc = new PdfKit();
const stream = doc.pipe(fs.createWriteStream("output.pdf"));
doc.font('backend/fonts/MADE Canvas Regular PERSONAL USE.otf')
.fontSize(25)
.text('sample text', 100, 100);
fs.readFile('backend/images/Happy Easter 5.svg', 'utf8', (err, contents) => {
SVGToPDF(doc, contents, 100, 200, {});
doc.end();
stream.on('finish', function() {
sendEmail("templateName", "myEmail", doc);
});
});
}
function sendEmail(template, to, attachment) {
service.send({
template: template,
message: {
to: to,
attachments: [
{
filename: 'text1.pdf',
content: attachment,
contentType: 'application/pdf'
}
]
},
locals: vars
})
.then(console.log).catch(console.error);
}