Skip to content

Could not find page for PDFRef Error during flatten #1281

@makarov-roman

Description

@makarov-roman

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.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions