【问题标题】:Node.js: PDFkit write to responseNode.js:PDFkit 写入响应
【发布时间】: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


    【解决方案1】:

    假设res 是您的服务器响应对象,只需这样做:

    doc.output(function(string) {
      res.end(string);
    });
    

    这将发送 PDF 的字符串表示形式,而不是将其写入文件。上面的代码是 PDFKit 文档中编译好的 CoffeeScript。

    doc.output (string) -> 
      console.log string
    

    【讨论】:

    • 记得设置二进制模式:res.end(string,'binary')否则PDF输出会损坏。
    • 仅供参考,此方法已被弃用。
    猜你喜欢
    • 2014-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-10
    • 2012-08-08
    • 2011-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多