最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I save a jsreport render to file with nodeJs? - Stack Overflow

programmeradmin2浏览0评论

I am new to node.js and jsreport, but what I am attempting to do is create a pdf in memory using node.js and then saving it to disk. I need this to be stand-along as it will be running as an AWS Lambda function.

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

Although this runs the output pdf will not open in Adobe Reader so I assume the file output is not a valid PDF.

this requires npm install jsreport

I am new to node.js and jsreport, but what I am attempting to do is create a pdf in memory using node.js and then saving it to disk. I need this to be stand-along as it will be running as an AWS Lambda function.

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

Although this runs the output pdf will not open in Adobe Reader so I assume the file output is not a valid PDF.

this requires npm install jsreport

Share Improve this question edited Apr 3, 2015 at 18:38 Ryan Fisch asked Apr 3, 2015 at 18:36 Ryan FischRyan Fisch 2,6645 gold badges39 silver badges57 bronze badges 1
  • fyi .writeFile is async. – Daniel A. White Commented Apr 3, 2015 at 18:38
Add a ment  | 

1 Answer 1

Reset to default 6

From what I gather from the jsreport website (although I haven't been able to verify, as none of the examples on their website work for me), it looks like out isn't rendered (PDF) data, but an object that contains—amongst other things—a stream.

Which leads me to believe that this might work:

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

评论列表(0)

  1. 暂无评论