Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js - Wait until file is completely written

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();
    });
};
like image 815
Owen Pierce Avatar asked May 01 '26 22:05

Owen Pierce


1 Answers

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
like image 171
Jordan Running Avatar answered May 04 '26 10:05

Jordan Running