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

时间:2015-04-03 18:36:41

标签: 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");
});

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

这需要npm install jsreport

1 个答案:

答案 0 :(得分:5)

我从jsreport网站收集的内容(虽然我无法验证,因为他们网站上没有任何示例适用于我),看起来out似乎并非如此渲染(PDF)数据,但包含其他内容的对象 - 流。

这让我相信这可能有用:

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