pdfs with flattened forms can't be printed in Adobe Reader or Acrobat
What were you trying to do?
Print a pdf flattened with pdf-lib.
How did you attempt to do it?
Open the file in Adobe Reader or Acrobat, and click cmd/ctl + P.
What actually happened?
Two successive errors popped up:
- The document could not be printed.
- There were no pages selected to print.
What did you expect to happen?
The pages to print.
How can we reproduce the issue?
Download the flattened form here by clicking the button: https://jsfiddle.net/Hopding/skevywdz/2/
Open the file in either Adobe Reader or Adobe Acrobat and print it.
Version
1.17.1
What environment are you running pdf-lib in?
Browser, Node
Checklist
- [X] My report includes a Short, Self Contained, Correct (Compilable) Example.
- [X] I have attached all PDFs, images, and other files needed to run my SSCCE.
Additional Notes
Tested in Acrobat DC Pro 2022.002.20191 on Mac OS 13.0.1
This is also happening for me. A few notes from testing that I have.
1.) This does NOT occur with pdfs that only have text input fields with their own unique names 2.) This does occur on a pdf with only text input fields that share a common name
Therefore, It seems to be related to any pdf field that is flattened with child widgets.
Additionally an Acrobat preflight shows "Incorrect value type for array element at index N"

I have yet to find a workaround.
Do you have any updates on this? (We're experiencing the same issue).
WORKAROUND:
Replace:
pdfForm.flatten();
with:
pdfForm.getFields().forEach((formField) => formField.enableReadOnly());
Additionally, the resulting PDF is smaller. We are not sure how/why.
WORKAROUND:
Replace:
pdfForm.flatten();with:
pdfForm.getFields().forEach((formField) => formField.enableReadOnly());Additionally, the resulting PDF is smaller. We are not sure how/why.
Yep, that is the exact same workaround I used.
This PR fixed the issue for me: https://github.com/Hopding/pdf-lib/pull/1268
Confirming this issue with a precise mechanism and inspection code.
Trigger conditions
The bug fires when all three are true:
- The source PDF is loaded (not created from scratch with pdf-lib) — typically an Acrobat-authored AcroForm template, or any AcroForm that's been round-tripped through a viewer that pre-renders widget appearances (Apple PDFKit, Foxit, iText, etc.).
- At least one checkbox widget has pre-existing
/AP/Nappearance entries on it. - The caller does
cb.check() + cb.defaultUpdateAppearances() + form.flatten() + doc.save()(the pattern recommended in #1574 / #1267 to make flatten render the checkmark).
Adobe Reader rejects the output with "There was a problem reading this document (18)".
A from-scratch document (PDFDocument.create() + form.createCheckBox(...)) does not trigger this — even with the same checkbox sequence — so this is specific to the load+flatten interaction with template-supplied appearances.
Mechanism
After save(), the page's /Resources /XObject dict contains entries that are bare PDFDicts with /Yes and /Off keys instead of Form XObject streams. These are the checkbox /AP/N appearance-state maps that flatten() placed in the page XObject slot rather than resolving to the active appearance stream (/Yes, since the box is checked).
mupdf flags it cleanly:
syntax error: no XObject subtype specified
syntax error: no XObject subtype specified
syntax error: no XObject subtype specified
syntax error: no XObject subtype specified
qpdf and poppler tolerate it (qpdf --check reports "no syntax or stream encoding errors found"); Adobe Reader and any mupdf-based renderer reject it.
Inspection code (works on any pdf-lib flatten output)
import { PDFDocument, PDFName, PDFDict } from 'pdf-lib';
const doc = await PDFDocument.load(savedBytes);
for (const p of doc.getPages()) {
const xobj = p.node.Resources()?.lookup(PDFName.of('XObject'));
if (!(xobj instanceof PDFDict)) continue;
for (const key of xobj.keys()) {
const o = xobj.lookup(key);
if (o?.getContents) continue; // proper Form XObject stream → ok
const subKeys = Array.from(o?.keys?.() || []).map(k => k.toString());
if (subKeys.includes('/Yes')) {
console.log(`malformed: ${key.toString()} subKeys=[${subKeys.join(',')}]`);
}
}
}
On a flattened output that exhibits the bug, this prints something like:
malformed: /FlatWidget-2163799337 subKeys=[/Yes,/Off]
malformed: /FlatWidget-4824990222 subKeys=[/Yes,/Off]
malformed: /FlatWidget-7592840450 subKeys=[/Yes,/Off]
malformed: /FlatWidget-578830786 subKeys=[/Yes,/Off]
(One entry per checkbox that was check()-ed.)
Suspected root cause
In the flatten code path that creates FlatWidget-* Form XObjects from widget appearances, when a checkbox has a pre-existing /AP/N dict, pdf-lib appears to take widget.AP.N (the {Yes, Off} map) as the candidate Form XObject and put it in the page resources, instead of taking widget.AP.N[widget.AS] (the active appearance stream). The map dict has no /Type or /Subtype because it's an appearance-state map, not a Form XObject.
Working workarounds
- The
enableReadOnlyworkaround already proposed in this issue works — confirmed end-to-end against the Adobe Reader error 18 reproducer. Trade-off: fields remain as widgets so for clean UX we additionally strip/Sigwidget annotations from page/Annotsto remove signature-field click-to-sign boxes. - Post-process pdf-lib's output through mupdf (
PDFDocument.openDocument(bytes, 'application/pdf').saveToBuffer('garbage=2,clean=yes,sanitize=yes')) — strips/resolves the malformed entries cleanly and produces an Adobe-Reader-readable PDF.
Version
Reproduced on [email protected], Node 18 and Node 25.
Relationship to PR #1268 / #1267
PR #1268 (against #1267) is a related but distinct fix. Its 4-line diff in PDFForm.ts changes which widget ref gets removed from the page's /Annots array during flatten — addressing the orphan-annotation-ref symptom of #1267 ("page annotations pointing to null references"). That code path is upstream of the one this issue is about.
Our bug is in the part of flatten() that copies the active widget appearance into the page's /XObject resource dict, where pdf-lib emits the /AP/N map ({/Yes, /Off}) instead of /AP/N[<active-state>]. PR #1268 doesn't touch that code path, so even merged it wouldn't fix the trigger here.
Both bugs cause Adobe Reader to reject the output, but they're independent and need separate fixes.
Notes for a fix
If anyone wants to take this on, the fix is likely in the flatten code path that emits FlatWidget XObjects: when reading the appearance for the active state, dereference /AP/N[<active-state-name>] instead of placing /AP/N itself in the page XObject slot. Happy to test a candidate PR against the real-world trigger I have on file.