Skip to content

Commit

Permalink
Wait until stream finshed to write the pdf (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
elpic authored and diegomura committed Jul 13, 2017
1 parent 61d41d0 commit a517d11
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions packages/react-pdf-node/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,21 @@ export default {
PDFRenderer.updateContainer(element, node, null);

const output = await pdf(container).toBuffer();
output.pipe(fs.createWriteStream(filePath));
const stream = fs.createWriteStream(filePath);
output.pipe(stream);

if (callback) {
callback(output, filePath);
}
await new Promise((resolve, reject) => {
stream.on('finish', () => {
if (callback) {
callback(output, filePath);
}
resolve(output);

console.log(`📝 PDF successfuly exported on ${path.resolve(filePath)}`);
console.log(
`📝 PDF successfuly exported on ${path.resolve(filePath)}`,
);
});
stream.on('error', reject);
});
},
};

0 comments on commit a517d11

Please sign in to comment.