Make pdf-lib more graceful like other pdf software#1407
Open
emilsedgh wants to merge 2 commits intoHopding:masterfrom
Open
Make pdf-lib more graceful like other pdf software#1407emilsedgh wants to merge 2 commits intoHopding:masterfrom
emilsedgh wants to merge 2 commits intoHopding:masterfrom
Conversation
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
|
@emilsedgh please check my comment here |
|
Hey @Hopding. It would be great if you could review this PR. It is an important fix & requested by many. |
|
cf #1281 too |
|
Is this planning to be merged? I'm currently using this alternative fork @visaright/pdf-lib only because of this issue. |
|
Can we get this in please? @Hopding |
|
Bump. @Hopding |
Bathlamos
added a commit
to Bathlamos/pdf-lib
that referenced
this pull request
Oct 17, 2025
|
Bump. @Hopding |
|
@anodynos what does your comment have to do with this? @cantoo/pdf-lib also has this problem |
|
Here is my workaround, maybe it will be helpful const form = pdfDoc.getForm();
if (form && form.getFields().length > 0) {
const pages = pdfDoc.getPages();
const formFields = form.getFields();
const firstPageRef = pages[0].ref;
for (const field of formFields) {
const widgets = field.acroField.getWidgets();
for (const widget of widgets) {
const widgetPageRef = widget.P();
// If widget has no page reference, assign it to first page
if (!widgetPageRef) {
try {
widget.setP(firstPageRef);
} catch {}
}
}
}
form.flatten();
} |
|
@Hopding this is needed |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Some broken fields may include annotations in their Kids array 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 #967,#1281,#1349
What?
PDF Fields can have
Kidsin them, eachKidbeing aWidgetAnnotation. In some rare cases (probably due to bad pdf generators) there are cases that there is aPDFFieldwith a an annotation in itKidsbut that annotation doesn't exist on any page. Therefore we don't know on which page it must be rendered.Why?
PDFForm.findPageForAnnotationReis responsible for finding the page for a given annotation. Up until now it'd throw an exception if it couldn't find a page.This means when trying to flatten such PDF's, pdf-lib would throw an error. So basically you have a pdf file that can be opened up with Chrome, Firefox/pdfjs, Acrobat Reader and even pdf-lib. But trying to flatten it using pdf-lib would cause an exception (as seen in #967,#1281,#1349)
How?
This patch makes pdf-lib's Flatten gracefully ignore such cases and render the PDF like other pdf readers.
Testing?
Checklist