I'm using child_process to run wkhtmltopdf to build a PDF from an html document. I want to wait until wkhtmltopdf is finished processing the document into a PDF before I proceed. I think that reading from stdout to capture when wkhtmltopdf sends its done signal is the best way to do this, but the following code reports stdout to be empty at res.send(). How do I set up an event to fire when stdout gives me data?
Code:
var buildPdf = function(error){
var pdfLetter;
var child = exec('wkhtmltopdf temp.html compensation.pdf', function(error, stdout, stderr) {
if (error)
res.send({
err:error.message
});
else
res.send({
output : stdout.toString()
});
// sendEmail();
});
};
You've encountered a wkhtmltopdf gotcha. It doesn't write status information to STDOUT, it writes it to STDERR.
$ node -e \
"require('child_process').exec( \
'wkhtmltopdf http://stackoverflow.com/ foo.pdf', \
function(err, stdout, stderr) { process.stdout.write( stderr ); } );"
Loading pages (1/6)
content-type missing in HTTP POST, defaulting to application/octet-stream
Counting pages (2/6)
Resolving links (4/6)
Loading headers and footers (5/6)
Printing pages (6/6)
Done
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With