Unable to flatten PDF if widget doesn't have P reference
When running form.flatten() on some PDFs, like from this this example, I get this error:
Error: Failed to find page undefined for element favorite.superhero
at _loop_1 (/node_modules/pdf-lib/cjs/api/form/PDFForm.js:463:27)
at PDFForm.flatten (/node_modules/pdf-lib/cjs/api/form/PDFForm.js:492:17)
Here is an example of PDF that will produce this error and where form isn't on 1st page. (Originally posted here)
I've created a draft pull request with a possible solution. The approach was to find the widget reference and then find the page that has that widget reference. Ideally, I would have preferred to update P() to do all this work, so that we don't have to call another method for fallback, however, the document is required to get the pages and I'm not aware if there's a way to access the document without passing it in. Perhaps @Hopding has other ideas or opinions about the apis?
However, when testing the example document, I am getting a different error, possibly unrelated, something about AcroField. @Hopding / @chislin do you have some time to take a look?
I've created a draft pull request with a possible solution. The approach was to find the widget reference and then find the page that has that widget reference. Ideally, I would have preferred to update
P()to do all this work, so that we don't have to call another method for fallback, however, the document is required to get the pages and I'm not aware if there's a way to access the document without passing it in. Perhaps @Hopding has other ideas or opinions about the apis?However, when testing the example document, I am getting a different error, possibly unrelated, something about
AcroField. @Hopding / @chislin do you have some time to take a look?
Looks good to me, hope @Hopding can take a look at this. Thank you!
Is there any way I can Pass P reference for flattening ?
Dug into the error @btecu ran into on his branch and found the following:
- This code:
...produces the following error:const formPdfBytes = fs.readFileSync('/Users/user/Desktop/form_on_3rd.pdf'); const pdfDoc = await PDFDocument.load(formPdfBytes); const form = pdfDoc.getForm(); form.flatten(); form.acroForm.getFields();Error: Expected instance of PDFName, but got instance of undefined at new UnexpectedObjectTypeError (/Users/user/github/pdf-lib/scratchpad/build/src/core/errors.js:38:24) at PDFContext.lookup (/Users/user/github/pdf-lib/scratchpad/build/src/core/PDFContext.js:93:15) at createPDFAcroTerminal (/Users/user/github/pdf-lib/scratchpad/build/src/core/acroform/utils.js:69:29) at Object.exports.createPDFAcroField (/Users/user/github/pdf-lib/scratchpad/build/src/core/acroform/utils.js:38:12) at PDFAcroForm.getFields (/Users/user/github/pdf-lib/scratchpad/build/src/core/acroform/PDFAcroForm.js:25:36) at /Users/user/github/pdf-lib/scratchpad/build/scratchpad/index.js:18:31 at step (/Users/user/github/pdf-lib/node_modules/tslib/tslib.js:139:27) at Object.next (/Users/user/github/pdf-lib/node_modules/tslib/tslib.js:120:57) at fulfilled (/Users/user/github/pdf-lib/node_modules/tslib/tslib.js:110:62) - An error is thrown whenever
form.acroForm.getFields()is called afterform.flatten()is invoked.
The cause of the error seems to be twofold:
-
The
PDFForm.flatten()method attempts to remove fields from the form after they have been flattened: https://github.com/Hopding/pdf-lib/blob/2b4ee6b097b7231935eac610f0e43a1befa7cac6/src/api/form/PDFForm.ts#L594This is good. The problem is that it only attempts to remove the field from the root-level
Fieldsarray. However, theFieldsarray is actually just the root of a tree structure, made up of terminal and non-terminal nodes. The terminal nodes are the actual fields and are always children of non-terminal nodes. Non-terminal nodes can themselves contain more non-terminals (thereby increasing the depth of the tree) or terminals.So removing fields from the root-level non-terminal (the
Fieldsentry ofPDFAcroForm.dict) is sufficient only for simple cases where the form does not contain a tree structure. More complicated forms (like the example in the README) that contain tree structures will produce this error when attempting to flatten them. Note that a form must contain a tree structure if there is at least one field whose fully qualified name contains a period (e.g.favorite.superhero).The solution to this issue is to use the
PDFAcroField.getParent()method to find the flattened field's non-terminal parent node. Then just remove the field from its parent'sKidsarray. If I recall correctly, fields will returnundefinedfromgetParentif they are direct descendents of the form'sFieldsarray. See the following for an example of how to account for this: https://github.com/Hopding/pdf-lib/blob/2b4ee6b097b7231935eac610f0e43a1befa7cac6/src/api/form/PDFForm.ts#L789 -
Once the above problem is addressed, a second issue will be introduced. The following logic is executed to convert raw
PDFDictinstances into the appropriate non-terminal/terminal wrapper class: https://github.com/Hopding/pdf-lib/blob/2b4ee6b097b7231935eac610f0e43a1befa7cac6/src/core/acroform/utils.ts#L44-L46It uses the following logic to determine if a node is terminal or non-terminal (not as straightforward as you might hope): https://github.com/Hopding/pdf-lib/blob/2b4ee6b097b7231935eac610f0e43a1befa7cac6/src/core/acroform/utils.ts#L51-L65
What will happen is that non-terminal nodes will have all of their fields removed from their
Kidsarray. This will then result in the above logic classifying them as terminal nodes. So, in essence, removing all kids from a non-terminal results in it becoming a terminal. This is unfortunate, but I don't think there's really a way to avoid it. The reason this is an actual problem is that the former non-terminal field will be passed to https://github.com/Hopding/pdf-lib/blob/2b4ee6b097b7231935eac610f0e43a1befa7cac6/src/core/acroform/utils.ts#L79-L81 to create an appropriate wrapper. However, because it used to be a non-terminal, it will not have aFTproperty. So the line that attempts to look this up will throw an error.I see two ways to solve this problem: (1) Don't assert that the result of an
FTlookup will be aPDFName, thereby allowing former non-terminals to fallthrough to the genericPDFAcroTerminalreturn. Or (2) when the final entry in a non-terminal'sKidsarray is deleted, remove the non-terminal itself from the tree, thereby avoiding the situation entirely. It would be best to do both of these things.
@Hopding thanks for the detailed explanation. The pull request should be ready for review now.