1

UPDATE:

Seems like an issue of wkhtmltopdf not exiting properly


I'm doing the following in node:

console.log("before");
fs.writeFile(html_filename, html, function (err) {
  if (err) {res.writeHead(400); res.end("" + err); return;}

  console.log("wrote html fine; now converting");
  exec('wkhtmltopdf ' + html_filename + ' ' + pdf_filename, function (err, stdout, stderr) {
    if (err) {res.writeHead(400); res.end("" + err); return;}

    console.log("converted; now reading");
    fs.readFile(pdf_filename, function (err, data) {
      if (err) {res.writeHead(400); res.end("" + err); return;}

      console.log("read fine; now serving");
      res.writeHead(200, {"content-type" : "application/pdf"});
      res.end(data);
    });
  });
});

which works fine, except everytime this is executed, the node program hangs and when I cmd + tab I see a "exec" process. When I tab to this process, the node program proceeds.

Any ideas why?

Max
  • 1,399
  • 13
  • 28

2 Answers2

4

As an alternative to wkhtmltopdf I would use Phantom.js: http://phantomjs.org/

You can create a simple rasterize script:

var page = require('webpage').create(),
address, output, size;

if (phantom.args.length < 2 || phantom.args.length > 3) {
    console.log('Usage: rasterize.js URL filename');
    phantom.exit();
} else {
    address = phantom.args[0];
    output = phantom.args[1];
    page.viewportSize = { width: 600, height: 600 };
    page.open(address, function (status) {
        if (status !== 'success') {
            console.log('Unable to load the address!');
        } else {
            window.setTimeout(function () {
                page.render(output);
                phantom.exit();
            }, 200);
        }
    });
}

And then call:

phantomjs rasterize.js http://path/to/webpage output.pdf
rogchap
  • 933
  • 6
  • 8
  • thanks, but since I'm not pdf'ing a static html, but a dynamic one based on a http request, I don't think this solution can help me better than wkhtmltopdf. – Max Aug 03 '12 at 00:05
  • 1
    phantomjs works with all urls including dynamic ones based on a http request. phantomjs is a pure headless webkit & javascript api, it is more active in the community than wkhtmltopdf and has many uses. But hey, it's up to you.... – rogchap Aug 09 '12 at 14:01
0

Um, this looks like a osx bug...

Max
  • 1,399
  • 13
  • 28