node wkhtmltopdf create corrupted PDF in node webk

2020-07-24 04:16发布

问题:

Im having a lot of trouble in node webkit to convert html to PDF.
Im using wkhtmltopdf 0.12.1 and did the npm install wkhtmltopdf.
Here is my node code:

    var wkhtmltopdf = require('wkhtmltopdf');
    var fs = require("fs-plus");

   // URL
   wkhtmltopdf('http://google.com/', { pageSize: 'letter' })
      .pipe(fs.createWriteStream('out.pdf'));

It gave me an error. Later I found this https://github.com/devongovett/node-wkhtmltopdf/pull/9
I did the correcting and im not getting any error anymore but the PDF that I get in return is corrupted.

Any idea ?

回答1:

I have tried following the examples on the node-wkhtmltopdf page.

First example generates a corrupted pdf file.

// URL
wkhtmltopdf('http://google.com/', { pageSize: 'letter' })
  .pipe(fs.createWriteStream('out.pdf'));

Though, this example works fine on my end.

wkhtmltopdf('http://google.com/', {output: 'out.pdf'});

You can also provide html instead of a url.

That being said, I am unable to generate a pdf within a node-webkit application. There doesn't seem to be any error, but nothing happens either.

Testing on Windows 7 64 bits.

I'd be curious to know why nothing is happening in a node-webkit application.



回答2:

So, I have found a solution. I instead use https://github.com/ariya/phantomjs/blob/master/examples/rasterize.js with phantomJS and run this from node-webkit with phantomJS and it work like a charm.