I'm having a problem, when I have to pipe()
the created document to multiple targets, in my case to a HTTP response and to an email attachment using node-mailer
. After the first use in an attachment to an email, nothing gets piped to a response (when calling it from the client, the PDF has 0 bytes).
Response controller:
const doc = await createPdf(course.name, lastRecordDate, ctx);
// Send a notificiation email with attachment
if (query.hasOwnProperty('sendEmail') && query.sendEmail === 'true') {
await sendNotificationEmail(doc, course, ctx);
}
doc.pipe(res);
res.contentType('application/pdf');
Function to send an email:
async function sendNotificationEmail(doc: any, course: Course, ctx: Context) {
const attachment = {
filename: `${course.name}-certificate.pdf`,
contentType: 'application/pdf',
content: doc
};
return SMTPSendTemplateWithAttachments(
ctx,
['somememail@test.si'],
`${course.name}`,
'en-report-created',
{
firstName: ctx.user.firstName,
courseName: course.name
},
[attachment]
);
}
If I remove the function to send an email, the PDF gets normally piped to a response and I can download it from the client.
I tried to find a way to clone the stream (PDFKit's document is a stream as far as I know), but was unsuccessful.
Any solution would be really helpful.