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

Could not find page for PDFRef Error during flatten #1281

Open
2 tasks done
makarov-roman opened this issue Jul 20, 2022 · 2 comments
Open
2 tasks done

Could not find page for PDFRef Error during flatten #1281

makarov-roman opened this issue Jul 20, 2022 · 2 comments

Comments

@makarov-roman
Copy link

makarov-roman commented Jul 20, 2022

What were you trying to do?

flatten document.

How did you attempt to do it?

example to reproduce below

What actually happened?

crash on flatten call

Error: Could not find page for PDFRef 220 0 R
    at PDFForm.findWidgetPage (/home/roman/Projects/monorepo/packages/lambdas/pdfService/node_modules/pdf-lib/cjs/api/form/PDFForm.js:603:23)
    at PDFForm.flatten (/home/roman/Projects/monorepo/packages/lambdas/pdfService/node_modules/pdf-lib/cjs/api/form/PDFForm.js:458:33)

What did you expect to happen?

not having a crash

How can we reproduce the issue?

const { PDFCheckBox, PDFDocument, PDFTextField } = require('pdf-lib');
const fs = require('fs');

const fill = async () => {
  try {
    const Body = fs.readFileSync(
      'internshipApplicationForFurtherEducation.pdf',
    );

    const pdfDoc = await PDFDocument.load(Body);
    const form = pdfDoc.getForm();
    form.flatten();

    const result = await pdfDoc.save({ useObjectStreams: true });
    fs.writeFileSync('result.pdf', result);
  } catch (e) {
    console.error(e);
    throw e;
  }
};
fill();

file

Version

1.17.1

What environment are you running pdf-lib in?

Node

Checklist

  • My report includes a Short, Self Contained, Correct (Compilable) Example.
  • I have attached all PDFs, images, and other files needed to run my SSCCE.

Additional Notes

In our project I have around 30 different pdfs and only 2 has such issue. Not sure if it's corrupted metadata or something else. Before it was working with pdftk.

@makarov-roman makarov-roman changed the title Could not find page for PDFRef Error why during flatten Could not find page for PDFRef Error during flatten Jul 20, 2022
@makarov-roman
Copy link
Author

I've made minor patch to ignore this error inside flatten and findWidgetPage functions and result doc look fine. So my assumption is that some editor left broken meta-data, thus pdflib "see" widget, but cannot find it on page. Ideally we should check it during load and clean such broken widgets/refs.
I'll publish the workaround tomorrow.

@makarov-roman
Copy link
Author

here is my workaround
you can test it via @visaright/pdf-lib npm package. (you can use package with caution, cause it will be removed after the issue is properly fixed in the mainstream)

emilsedgh added a commit to emilsedgh/pdf-lib that referenced this issue Feb 26, 2023
that couldn't be found on any pages. Up until now we've been throwing
an exception when dealing with such fields. But it appears that other
PDF software are more resilient to this and gracefully ignore them.

This commit ensures we'll do the same.

Fixes Hopding#967,Hopding#1281,Hopding#1349
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant