-
-
Notifications
You must be signed in to change notification settings - Fork 821
Make pdf-lib more graceful like other pdf software #1407
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
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 |
|
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();
} |
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