2

我正在使用 child_process 运行 wkhtmltopdf 以从 html 文档构建 PDF。我想等到 wkhtmltopdf 完成将文档处理成 PDF 后再继续。我认为从 stdout 读取以捕获 wkhtmltopdf 发送其完成信号时的最佳方法是执行此操作,但以下代码在 res.send() 处报告 stdout 为空。当 stdout 为我提供数据时,如何设置要触发的事件?

代码:

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();
    });
};
4

2 回答 2

4

您遇到了 wkhtmltopdf 问题。它不会将状态信息写入 STDOUT,而是将其写入 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
于 2012-06-20T00:55:46.743 回答
0

我刚刚通过 Heroku 上的 Node.js 完成了这项工作,并想发布,因为我必须克服几个小障碍。

// Spin up a new child_process to handle wkhtmltopdf.
var spawn = require('child_process').spawn;

// stdin/stdout, but see below for writing to tmp storage.
wkhtmltopdf = spawn('./path/to/wkhtmltopdf', ['-', '-']);

// Capture stdout (the generated PDF contents) and append it to the response.
wkhtmltopdf.stdout.on('data', function (data) {
    res.write(data);
});

// On process exit, determine proper response depending on the code.
wkhtmltopdf.on('close', function (code) {
    if (code === 0) {
        res.end();
    } else {
        res.status(500).send('Super helpful explanation.');
    }
});

res.header('Content-Type', 'application/octet-stream');
res.header('Content-Disposition', 'attachment; filename=some_file.pdf');
res.header('Expires', '0');
res.header('Cache-Control', 'must-revalidate, post-check=0, pre-check=0');

// Write some markup to wkhtmltopdf and then end the process. The .on 
// event above will be triggered and the response will get sent to the user.
wkhtmltopdf.stdin.write(some_markup);
wkhtmltopdf.stdin.end();

在 Heroku 的 Cedar-14 堆栈上,我无法让 wkhtmltopdf 写入标准输出。服务器总是以Unable to write to destination. 诀窍是写入./.tmp然后将写入的文件流式传输回用户 - 很简单:

wkhtmltopdf = spawn('./path/to/wkhtmltopdf', ['-', './.tmp/some_file.pdf']);

wkhtmltopdf.on('close', function (code) {
    if (code === 0) {

        // Stream the file.
        fs.readFile('./.tmp/some_file.pdf', function(err, data) {

            res.header('Content-Type', 'application/octet-stream');
            res.header('Content-Disposition', 'attachment; filename=' + filename);
            res.header('Expires', '0');
            res.header('Cache-Control', 'must-revalidate, post-check=0, pre-check=0');

            res.send(data);
        });
    } else {
        res.status(500).send('Super helpful explanation.');
    }
});

res.header('Content-Type', 'application/octet-stream');
res.header('Content-Disposition', 'attachment; filename=' + filename);
res.header('Expires', '0');
res.header('Cache-Control', 'must-revalidate, post-check=0, pre-check=0');
于 2015-02-23T23:45:22.750 回答