如何使用nodeJs将jsreport渲染保存到文件?

Rya*_*sch 7 javascript node.js jsreport

我是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");
});
Run Code Online (Sandbox Code Playgroud)

虽然运行输出pdf将无法在Adobe Reader中打开,因此我假设文件输出不是有效的PDF.

这需要npm install jsreport

rob*_*lep 6

从我从jsreport网站收集的内容(虽然我无法验证,因为他们网站上没有任何示例适用于我),它看起来像是out不呈现(PDF)数据,而是包含以下内容的对象:除其他外 - 一个流.

这让我相信这可行:

require("jsreport").render("<h1>Hi there!</h1>").then(function(out) {
  out.result.pipe(fs.createWriteStream('c:\\helloworld.pdf'));
});
Run Code Online (Sandbox Code Playgroud)