Node.js 节点PDFKit管道到多个目标

Node.js 节点PDFKit管道到多个目标,node.js,pdf,nodemailer,node-streams,node-pdfkit,Node.js,Pdf,Nodemailer,Node Streams,Node Pdfkit,我遇到了一个问题,当我必须将创建的文档管道()发送到多个目标时,我使用node-mailer发送到HTTP响应和电子邮件附件。第一次在电子邮件附件中使用后,没有任何内容通过管道发送到响应(从客户端调用时,PDF有0个字节) 响应控制器: const doc = await createPdf(course.name, lastRecordDate, ctx); // Send a notificiation email with attachment if (query.hasOwnPr

我遇到了一个问题,当我必须将创建的文档
管道()
发送到多个目标时,我使用
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的文档是一个流),但没有成功


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

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

这是密码

// 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
这个解决方案并不是最好的,任何建议都是非常受欢迎的