Node.js:PDFkit写入响应

时间:2022-06-02 21:08:24

I'm using PDFKit (A PDF generation library for Node.js, pdfkit.org) and I want to send a PDF as a response to a client.

我正在使用PDFKit(Node.js的PDF生成库,pdfkit.org),我想发送PDF作为对客户端的响应。

# Write the PDF file to disk
doc.write('output.pdf');

The code above writes the PDF file to the disk, but I want it to send as the response. How can I do that?

上面的代码将PDF文件写入磁盘,但我希望它作为响应发送。我怎样才能做到这一点?

1 个解决方案

#1


5  

Assuming res is your server response object, just do this:

假设res是您的服务器响应对象,请执行以下操作:

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

This will send a string representation of the PDF rather than writing it to file. The code above is the compiled CoffeeScript that was in the documentation for PDFKit.

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

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

#1


5  

Assuming res is your server response object, just do this:

假设res是您的服务器响应对象,请执行以下操作:

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

This will send a string representation of the PDF rather than writing it to file. The code above is the compiled CoffeeScript that was in the documentation for PDFKit.

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

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