난 그냥 Heroku가에 Node.js를 통해 작업이 점점 완료 나는 작은 장애물의 몇 가지를 극복했다 이후 게시 싶었어요. Heroku가의 삼나무-14 스택에
// 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();
, 나는 표준 출력에 쓸 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');
전혀이 경우 이유를 설명 할 수 있습니까? – Menztrual
@tehlulz 다른 프로세스/파일 설명자 (예 :'wkhtmltopdf foo.html - | gzip> foo.gz')로 출력물을 파이프하는 동안 진행 상황을 볼 수 있도록하는 것이 좋습니다. –
이 마법은 무엇입니까! : P – Menztrual