Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Node.js:PDFkit写入响应_Javascript_Node.js_Pdf Generation - Fatal编程技术网

Javascript Node.js:PDFkit写入响应

Javascript Node.js:PDFkit写入响应,javascript,node.js,pdf-generation,Javascript,Node.js,Pdf Generation,我正在使用PDFKit(Node.js的PDF生成库),我想将PDF作为响应发送给客户端 # Write the PDF file to disk doc.write('output.pdf'); 上面的代码将PDF文件写入磁盘,但我希望它作为响应发送。我该怎么做?假设res是您的服务器响应对象,只需执行以下操作: doc.output(function(string) { res.end(string); }); 这将发送PDF的字符串表示,而不是将其写入文件。上面的代码是PDFKit

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

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

上面的代码将PDF文件写入磁盘,但我希望它作为响应发送。我该怎么做?

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

doc.output(function(string) {
  res.end(string);
});
这将发送PDF的字符串表示,而不是将其写入文件。上面的代码是PDFKit文档中编译的CoffeeScript

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

请记住设置二进制模式:
res.end(字符串,'binary')
否则PDF输出将损坏。仅供参考此方法已被弃用。