将PDFKit管道节点连接到多个目标

时间:2019-07-18 13:48:34

标签: node.js pdf nodemailer node-streams node-pdfkit

我不得不将创建的文档pipe()指向多个目标时遇到问题,就我而言,它是使用node-mailer的HTTP响应和电子邮件附件。首次在电子邮件附件中使用后,没有任何内容通过管道传递给响应(从客户端调用时,PDF的字节数为0)。

响应控制器:

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');

发送电子邮件的功能:

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]
  );
}

如果我删除了发送电子邮件的功能,则PDF通常可以通过管道发送到响应,并且可以从客户端下载。

我试图找到一种方法来克隆流(据我所知,PDFKit的文档是流),但未成功。

任何解决方案都将非常有帮助。

1 个答案:

答案 0 :(得分:0)

我使用两个PassThrough流解决了这个问题,两个流通过管道PDFKit文档Stream,然后在data事件中将chunks写到两个单独的缓冲区。在end事件中,我通过电子邮件发送数据并创建了一个新的PassThrough流,并将其通过管道传递给响应。

这是代码。

// Both PassThrough streams are defined before in order to use them in the createPdf function
streamCopy1 = new PassThrough();
streamCopy2 = new PassThrough();

const buffer1 = [];
const buffer2 = [];

streamCopy1
  .on('data', (chunk) => {
    buffer1.push(chunk);
  })
  .on('end', () => {
    const bufferFinished = Buffer.concat(buffer1);
    if (query.hasOwnProperty('sendEmail') && query.sendEmail === 'true') {
      sendNotificationEmail(bufferFinished, course, ctx);
    }
  });

streamCopy2
  .on('data', (chunk) => {
    buffer2.push(chunk);
  })
  .on('end', () => {
    const bufferFinished = Buffer.concat(buffer2);
    const stream = new PassThrough();
    stream.push(bufferFinished);
    stream.end();
    stream.pipe(res);
    res.contentType('application/pdf');
  });

创建PDF的功能。

// function declaration
const doc = new pdfDocument();
doc.pipe(streamCopy1).pipe(streamCopy2);
// rest of the code

解决方案并不是最好的解决方案,非常欢迎任何建议。

相关问题