wkhtmltopdf pauses and requires manual intervention

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?

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

Um, this looks like a osx bug...