我是新手,正在学习node.js和jsreport。我想要做的是使用node.js在内存中创建PDF文件,然后将其保存到磁盘上。由于它将作为AWS Lambda函数运行,所以需要它是独立的。
var fs = require('fs');
require("jsreport").render("<h1>Hi there!</h1>").then(function(out) {
//pipe pdf with "Hi there!"
fs.writeFile('C:\\helloworld.pdf', out, function (err) {
if (err) return console.log(err);
console.log('Hello World > helloworld.txt');
});
fs.close();
console.log("The End");
});
尽管这段代码可以运行,但生成的PDF文件无法在Adobe Reader中打开,因此我认为输出文件不是有效的PDF格式。
要实现这一功能,需要先安装npm包jsreport。
.writeFile
是异步的。 - Daniel A. White