Skip to content

Commit 6a04db5

Browse files
committed
fix: handle malformed PDFs and checkbox appearance states during flatten
- Use lookupMaybe() instead of lookup() for FT attribute to gracefully handle malformed PDFs with missing or invalid field types Hopding/pdf-lib#1519 - Use widget's appearance state (/AS) when available instead of relying solely on field value (/V) to find correct appearance in /AP/N dict Hopding/pdf-lib#1574 Coded by an LLM.
1 parent f4e8687 commit 6a04db5

2 files changed

Lines changed: 14 additions & 3 deletions

File tree

src/api/form/PDFForm.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -791,8 +791,17 @@ export default class PDFForm {
791791
refOrDict = lookedUp;
792792
}
793793
if (refOrDict instanceof PDFDict) {
794-
const value = field.acroField.getValue();
795-
const ref = refOrDict.get(value) ?? refOrDict.get(PDFName.of('Off'));
794+
// Use the widget's appearance state (/AS) if available, otherwise
795+
// fall back to the field value (/V). The /AS value is the key in
796+
// the /AP/N dict that specifies which appearance to show.
797+
// This fixes #1574 where checkmarks disappeared because /V and /AS
798+
// might have different values (e.g., /V=/Yes but /AS=/1)
799+
const appearanceState = widget.getAppearanceState();
800+
const fieldValue = field.acroField.getValue();
801+
const ref =
802+
(appearanceState && refOrDict.get(appearanceState)) ??
803+
refOrDict.get(fieldValue) ??
804+
refOrDict.get(PDFName.of('Off'));
796805

797806
if (ref instanceof PDFRef) {
798807
refOrDict = ref;

src/core/acroform/utils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ const isNonTerminalAcroField = (dict: PDFDict): boolean => {
7777

7878
const createPDFAcroTerminal = (dict: PDFDict, ref: PDFRef): PDFAcroTerminal => {
7979
const ftNameOrRef = getInheritableAttribute(dict, PDFName.of('FT'));
80-
const type = dict.context.lookup(ftNameOrRef, PDFName);
80+
// Use lookupMaybe to handle malformed PDFs where FT is missing or invalid
81+
// This fixes crashes with certain government forms (#1519)
82+
const type = dict.context.lookupMaybe(ftNameOrRef, PDFName);
8183

8284
if (type === PDFName.of('Btn')) return createPDFAcroButton(dict, ref);
8385
if (type === PDFName.of('Ch')) return createPDFAcroChoice(dict, ref);

0 commit comments

Comments
 (0)