我正在使用nodejs pdfkit创建一个pdf文档,并将其保存在一个文件中。
我想写一个文件内的PDF,并确保PDF已完成writting之前退出我的功能。
下面是我的代码:
export default async function generate_pdf(params, filePath) {
let doc = new PDFDocument({ size: "A4", margin: 50 });
// Filling my pdf document...
fillDoc(doc);
// Finish the document and save it in a file
doc.end();
// Here a fs.WriteStream is created with automatic close.
const writeStream = doc.pipe(fs.createWriteStream(filePath));
// Here I would like to make sure that writeStream has finished before ending the function.
// something like await waitForClose(writeStream);
// before returning
return filePath;
}
字符串
我知道根据WriteStream文档,有一个事件'close',它被发出。
所以我可以这样做:
writeStream.on('close', () => {
// my code
});
型
但这并不能解决我的需求,因为我可能会同时创建多个文件,我希望有一种方法可以确保所有文件在退出函数之前都已完成写入。
如果你能帮忙的话,提前谢谢你:)
1条答案
按热度按时间n3h0vuf21#
我相信你会这样做:
字符串