I need to do some housekeeping after pdf file is saved:
docx2pdf(doc, function (pdf) {
console.log("saving file");
var pdfFile = fs.createWriteStream("./test2.pdf");
pdf.on('finish',function(){
console.log("Finished");
process.exit();
}).pipe(pdfFile);
pdf.end();
console.log("After save");
}, true, true);
But code inside second nested function is never launched. pdf is object from pdfkit library. How can I fix it?
UPD: this approach doesn't work also:
docx2pdf(doc, function (pdf) {
console.log("saving file");
var pdfFile = fs.createWriteStream("./test2.pdf");
pdfFile.on('finish',function(){
console.log("Finished");
process.exit();
});
pdf.pipe(pdfFile);
//pdf.end();
console.log("After save");
}, true, true);
You need to catch the event finish
on the pipe object, not on the pdf object:
console.log("saving file");
pdf
.pipe( fs.createWriteStream("./test2.pdf") )
.on( 'finish', function(){
console.log("Finished");
});
pdf.end();
console.log("After save");
If pdf
is a proper NodeJS stream, you can simply pipe it into the write stream and be done. If you want to quit your program after the file is saved, you could do this on 'close'.
Notice: there is no "after saving" state (because you end the process). That console.log("After save")
line in your code is called before the file is saved!
docx2pdf(doc, function (pdf) {
console.log("saving file");
var pdfFile = fs.createWriteStream("./test2.pdf");
pdf.pipe(pdfFile);
pdfFile.on('close',function(){
console.log("Finished");
process.exit();
});
}, true, true);
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