【问题标题】:How to get pdf data in pdfkit without saving it to file如何在 pdfkit 中获取 pdf 数据而不将其保存到文件中
【发布时间】:2014-05-21 07:57:39
【问题描述】:

我在 nodejs 中使用 pdfkit 来创建 pdf。现在,要从 pdfDocument 中获取数据,我首先使用“fs”将其写入文件,然后从该文件中读取。

我希望能够直接使用来自 pdfDocument 对象的数据并将其作为响应发送。我该怎么做?

【问题讨论】:

  • 哪个pdfkit? Ruby 一还是 nodejs 一?你已经标记了他们两个。

标签: node.js pdfkit node-pdfkit


【解决方案1】:

每个 pdfDocument 都是一个流。您基本上可以像这样通过管道将其传递给响应:

require('http').createserver(function (request, response) {
    var pdfdocument = require('pdfkit'),
        pdfdocument = new pdfdocument();

    pdfdocument.text('wassup');
    pdfdocument.pipe(response);

    pdfdocument.end()
}).listen(1999);

【讨论】:

  • 我希望能够将 pdfDocument 作为响应对象发布。但我无法将 pdfDocument 传输到流中。我该怎么做?
  • 无法将 pdfDocument 传输到流中是什么意思?上面的代码有什么问题?我尝试过这个。它有效。
  • @FaridNouriNeshat 谢谢。我想问一下上面是否可以与任何其他http处理程序一起使用?您的解决方案需要一个端口,我已经有一个 hapi nodejs 服务器,我正在尝试使用 lambda 函数进行复制。需要将响应作为 pdf 下载传递回前端网站。
  • 是的,只要您有一个响应对象或可写流,否则您可以将其缓冲并作为结果返回,但我不建议这样做,因为它会更慢。
猜你喜欢
  • 2012-10-27
  • 2013-11-29
  • 1970-01-01
  • 2013-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多