【发布时间】:2021-09-02 07:02:23
【问题描述】:
我正在尝试将使用 node.js 中的 pdfkit 生成的 pdf 文件发送到客户端。
如果我的 pdf 包含任何图像,那么我的代码将给出以下错误。否则(没有任何图像),此代码将正常工作
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
这是我的代码
var doc = new PDFDocument({
bufferPages: true,
}),
svg = mySvg;
SVGtoPDF(doc, svg, 0, 0);
let buffers = [];
doc.on("data", buffers.push.bind(buffers));
doc.on("end", () => {
let pdfData = Buffer.concat(buffers);
ctx.res
.writeHead(200, {
"Content-Length": Buffer.byteLength(pdfData),
"Content-Type": "application/pdf",
"Content-disposition": "attachment;filename=file.pdf",
})
.end(pdfData);
});
doc.end();
我使用strapi 作为我的后端框架。 当我尝试将文件写入服务器时,它会正常工作。但我需要发送文件而不存储在服务器中。
【问题讨论】: