1
const doc = new PDFDocument(); 
// do stuff here
const writeStream = fs.createWriteStream('output.pdf')
doc.pipe(writeStream);
doc.end();

above code generating pdf file but cannot open pdf, it is showing an error that file has damaged.

1 Answers1

4

You probably have an async issue where you're trying to access/return the doc before it's done writing to disk.

Fix with a handler :

writeStream.on('finish', () => { .. your code here .. });
AlienWebguy
  • 76,997
  • 17
  • 122
  • 145