pdfjs: Cannot catch error in asBuffer()

I’m currently merging around 1500 PDFs and tried to find the defect one, but I cannot catch errors produced by this.end() in asBuffer().

While errors get caught here:

try {
    doc.pipe(fs.createWriteStream(fullPdfPath));
    await doc.end();
} catch (err) { console.error(err); }

The node process quits with an unhandled error here:

try {
    const buf = await doc.asBuffer();
    fs.writeFileSync(fullPdfPath, buf, { encoding: 'binary' });
} catch (err) { console.error(err); }

I’m pretty sure the reason for the uncaught error is this line: https://github.com/rkusa/pdfjs/blob/3374d1ff1142d16e47a10dac2ba93a3f0f161a35/lib/document.js#L636

It should probably be:

if (shouldEnd) {
    this.end().catch(reject)
}

Interesting side fact: PDFs throwing errors like Invalid xref object at 54524 or Name must start with a leading slash, found: 0 are single-page PDFs previously extracted by pdfjs from other multi-page PDFs. Extracting worked, but merging again failed.

I could get rid of the Invalid xref object error by extracting with asBuffer() writeFileSync and encoding binary instead of pipe and stream but the one PDF with Name must start with a leading slash, found: 0 drives me crazy.

About this issue

  • Original URL
  • State: open
  • Created a year ago
  • Comments: 21 (7 by maintainers)

Commits related to this issue

Most upvoted comments

Well, … 🤦‍♂️ – apparently returning a promise to chain it was only a thing inside of a then()/catch(). I gave it some more time and added a unit test to be sure this time. Thanks for the feedback. Released as 2.5.2.

So maybe you are trying to add PDFs previously generated with pdfjs that are already broken?

In the example I sent you “Tax-Invoice-M590936.pdf” that file was not generated by pdfjs (at least not by me) - that file was uploaded by one of our clients and triggers the “Name must start with a leading slash, found: (” error when appended to a pdf using pdfjs, then that pdf is appended to another pdf.