如何在 Koa 中发送生成的文件

How in Koa send generated file

我需要制作一个带有用户内容的pdf文件并发回。我选择了 pdfmake,因为这样就可以制作表格了。 我使用 Koa.js;

router.post('/pdf', koaBody(), async ctx => {
      const doc = printer.createPdfKitDocument(myFunctionGeneratePDFBody(ctx.request.body));
      doc.pipe(ctx.res, { end: false });
      doc.end();
      ctx.res.writeHead(200, {
        'Content-Type': 'application/pdf',
        "Content-Disposition": "attachment; filename=document.pdf",
      });
      ctx.res.end();
    });

并得到一个错误

Error [ERR_STREAM_WRITE_AFTER_END]: write after end
        at write_ (_http_outgoing.js:572:17)
        at ServerResponse.write (_http_outgoing.js:567:10)
        at PDFDocument.ondata (_stream_readable.js:666:20)
        at PDFDocument.emit (events.js:182:13)
        at PDFDocument.EventEmitter.emit (domain.js:442:20)
        at PDFDocument.Readable.read (_stream_readable.js:486:10)
        at flow (_stream_readable.js:922:34)
        at resume_ (_stream_readable.js:904:3)
        at process._tickCallback (internal/process/next_tick.js:63:19)

但是保存在中间文件中并发送它的工作...

router.post('/pdf', koaBody(), async ctx => {
  await new Promise((resolve, reject) => {
    const doc = printer.createPdfKitDocument(generatePDF(ctx.request.body));
    doc.pipe(fs.createWriteStream(__dirname + '/document.pdf'));
    doc.end();
    doc.on('error', reject);
    doc.on('end', resolve);
  })
    .then(async () => {
      ctx.res.writeHead(200, {
        'Content-Type': 'application/pdf',
        'Content-Disposition': 'attachment; filename=document.pdf',
      });
      const stream = fs.createReadStream(__dirname + '/document.pdf');
      return new Promise((resolve, reject) => {
        stream.pipe(ctx.res, { end: false });
        stream.on('error', reject);
        stream.on('end', resolve);
      });
    });
  ctx.res.end();
});

避免使用 writeHead,参见 https://koajs.com/#response

这样做:

ctx.attachment('file.pdf');
ctx.type =
  'application/pdf';
const stream = fs.createReadStream(`${process.cwd()}/uploads/file.pdf`);
ctx.ok(stream); // from https://github.com/jeffijoe/koa-respond

我刚刚 运行 遇到了同样的问题,你的问题帮助了我,我已经弄清楚了问题所在:你马上要结束 response/stream,但实际上并没有完全写好了。问题是 doc.end() returns 马上,在写入完成之前。诀窍是让您的文档在完成时结束流(因此不再 end: false)并等待此事件,例如使用 Promise。

固定代码如下所示:

router.post('/pdf', koaBody(), async ctx => {
      const doc = printer.createPdfKitDocument(myFunctionGeneratePDFBody(ctx.request.body));
      doc.pipe(ctx.res);
      doc.end();
      ctx.res.writeHead(200, {
        'Content-Type': 'application/pdf',
        "Content-Disposition": "attachment; filename=document.pdf",
      });
      return new Promise(resolve => ctx.res.on('finish', resolve));
    });