【发布时间】:2013-09-18 19:04:36
【问题描述】:
我正在使用 PDFKit(Node.js 的 PDF 生成库,pdfkit.org),我想将 PDF 作为响应发送给客户。
# Write the PDF file to disk
doc.write('output.pdf');
上面的代码将 PDF 文件写入磁盘,但我希望它作为响应发送。我该怎么做?
【问题讨论】:
标签: javascript node.js pdf-generation
我正在使用 PDFKit(Node.js 的 PDF 生成库,pdfkit.org),我想将 PDF 作为响应发送给客户。
# Write the PDF file to disk
doc.write('output.pdf');
上面的代码将 PDF 文件写入磁盘,但我希望它作为响应发送。我该怎么做?
【问题讨论】:
标签: javascript node.js pdf-generation
假设res 是您的服务器响应对象,只需这样做:
doc.output(function(string) {
res.end(string);
});
这将发送 PDF 的字符串表示形式,而不是将其写入文件。上面的代码是 PDFKit 文档中编译好的 CoffeeScript。
doc.output (string) ->
console.log string
【讨论】:
res.end(string,'binary')否则PDF输出会损坏。