Node.js中的pdf空文件

时间:2018-09-26 05:47:57

标签: node.js

我是Node.js的新手。我正在生成pdf文件,但是在生成pdf文件之后缺少内容。在cmd上以加密形式显示一些数据。 这是代码:

var fs = require('fs');
const jsreport = require('jsreport-core')()
jsreport.init().then(() => {
var something =  jsreport.render({
    template: {
        content: ' This is content ..LBABABABAB',
        engine: 'handlebars',
        recipe: 'chrome-pdf',
        binary : true

    },
    data: {
        foo: "world"

    }
    }).then((resp) => {
    // prints pdf with headline Hello world
    //console.log();

    var data = resp.content.toString();
    console.log(data);
    // Write Data to File


    fs.writeFile("D:/test.pdf", data, function(err) 
         {
        if(err) {
            return console.log(err);
        }
        console.log("The file was saved!");
    });

});

1 个答案:

答案 0 :(得分:0)

 if we pass the resp.content buffer directly to the writeFile it will work.. 
 like      
 fs.writeFile("D:/test.pdf", resp.content, function(err)  


var fs = require('fs');
const jsreport = require('jsreport-core')()
jsreport.init().then(() => {
var something =  jsreport.render({
template: {
    content: ' This is content ..LBABABABAB',
    engine: 'handlebars',
    recipe: 'chrome-pdf',
    binary : true

},
data: {
    foo: "world"

}
}).then((resp) => {
// prints pdf with headline Hello world
//console.log();

var data = resp.content.toString();
console.log(data);
// Write Data to File


fs.writeFile("D:/test.pdf", resp.content, function(err) 
     {
    if(err) {
        return console.log(err);
    }
    console.log("The file was saved!");
});

});