Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: Invalid stream: endstream not found #285

Open
flowsn4ke opened this issue Jun 27, 2022 · 1 comment · May be fixed by #286
Open

Error: Invalid stream: endstream not found #285

flowsn4ke opened this issue Jun 27, 2022 · 1 comment · May be fixed by #286

Comments

@flowsn4ke
Copy link

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.

@rkusa rkusa linked a pull request Sep 5, 2022 that will close this issue
@rkusa
Copy link
Owner

rkusa commented Sep 5, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants