pdfjs icon indicating copy to clipboard operation
pdfjs copied to clipboard

Error: Invalid stream: endstream not found

Open flowsn4ke opened this issue 2 years ago • 1 comments

Hi!

After some research it seams this is a known issue due to pdf generators not following pdf specifications close enough. Unfortunately we still have to deal with it... Any chance we could bake in a fix in the lib or find a workaround?

The issue is also that this error makes our application crash, it's not caught even if inside a try / catch block for some reason.

You can find a pdf that allows to reproduce the issue here.

flowsn4ke avatar Jun 27 '22 15:06 flowsn4ke

Hi, thanks for the report and for providing an actual example PDF!

The issue is also that this error makes our application crash, it's not caught even if inside a try / catch block for some reason.

I agree, this must be avoided. It looks like I am able to successfully catch the error in the following snippet:

const fs = require("fs");
const pdf = require("./");

async function main() {
  try {
    const doc = new pdf.Document({
      font: require("./font/Helvetica"),
      padding: 0,
    });
    doc.pipe(fs.createWriteStream("output.pdf"));

    const src = await fs.promises.readFile(
      "891a951c3d0bbf66d6fb851500629cf78a73ab88_Facture_FSECU83196.pdf"
    );
    const ext = new pdf.ExternalDocument(src);
    doc.addPagesOf(ext);

    doc.text("Test", { borderBottomWidth: 1, borderBottomColor: 0x000000 });

    await doc.end();
  } catch (err) {
    console.log("Caught error:");
    console.error(err);
  }
}

main();

It prints out:

Caught error:
Error: Invalid stream: `endstream` not found

Are you making sure to either use the try/catch inside of an async function (and don't have any missing awaits), or properly add .catch() handlers to your promises?

To the error itself.

Any chance we could bake in a fix in the lib or find a workaround?

I am generally open to that, though depends a bit on how involved the workaround is.

In this case, I've created a potential workaround #286. However, while the error is gone, the PDF in question just produces white pages. Looks like there is something else wrong, but tracking that down is unfortunately beyond the time I am willing to invest in it, sorry.

The question now is, would the workaround work for other cases, or does it always produce white pages. Because if it's the latter, I'd rather keep the error.

rkusa avatar Sep 05 '22 16:09 rkusa