How to save jsreport rendering for a file using nodeJs?

I am new to node.js and jsreport, but what I'm trying to do is create a pdf file in memory using node.js and then save it to disk. I need this to stand, as it will work 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 works, the pdf output does not open in Adobe Reader, so I assume that the output of the file is not a valid PDF.

this requires npm install jsreport

+6
source share
1 answer

From what I am collecting on the jsreport website (although I could not verify since none of the examples on their website work for me), it looks like out (PDF) data is not displayed, but an object that contains, among other things, a stream.

This makes me think this might work:

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

Source: https://habr.com/ru/post/984665/


All Articles