Skip to content

Cannot catch error in asBuffer() #312

@7freaks-otte

Description

@7freaks-otte

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:

this.end()

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions