jsreport / jsreport-core

The minimalist jsreport rendering core
GNU Lesser General Public License v3.0
86 stars 24 forks source link

Example with phantom-pdf produces a blank PDF #7

Closed metachris closed 8 years ago

metachris commented 8 years ago

jsreport-phantom-pdf always produces an empty PDF, even with the example from the README:

var jsreport = require('jsreport-core')()
var fs = require("fs");
var output_fn = "/vagrant/test.pdf";

jsreport.init().then(function () {
   return jsreport.render({
       template: {
           content: '<h1>Hello {{:foo}}</h1>',
           engine: 'jsrender',
           recipe: 'phantom-pdf'
        },
        data: {
            foo: "world"
        }
    }).then(function(resp) {
        const pdf = resp.content.toString();
        console.log(pdf);
        fs.writeFileSync(output_fn, pdf);
        console.log("Saved PDF content to " + output_fn);
   });
}).catch(function(e) {
    console.log(e)
})

Producing HTML output works fine with handlebars or jsrender, but phantom-pdf always produces the same PDF without any kind of text. Same result with phantomjs-prebuilt and customPhantomJS: true.

PhantomJs version of the standard include:

$ /home/vagrant/node_modules/jsreport-phantom-pdf/node_modules/phantom-html-to-pdf/node_modules/phantom-workers/node_modules/phantomjs/lib/phantom/bin/phantomjs --version
1.9.8

PhantomJs version of phantomjs-prebuilt:

$ /home/vagrant/node_modules/phantomjs-prebuilt/lib/phantom/bin/phantomjs --version
2.1.1

Node module versions:

pofider commented 8 years ago

Hi,

this has something to do with encoding problem when you toString buffer and pass it to writeFileSync...

The best answer I can give you right now is to pass the resp.content buffer directly to the writeFilteSync

fs.writeFileSync(output_fn, resp.content);
metachris commented 8 years ago

Thanks, this did the trick! Works 👍

JesusSanYan commented 3 years ago

Thanks, this did the trick! Works 👍

Hi mechris I have the same issue, can you show me how your example looks?