Hopding / pdf-lib

Create and modify PDF documents in any JavaScript environment
https://pdf-lib.js.org
MIT License
6.89k stars 656 forks source link

Flatten is removing RadioGroups and Checkboxes #1549

Open wasims1 opened 11 months ago

wasims1 commented 11 months ago

What were you trying to do?

First thank you so much for the great library! We're using it to process pdf. We draw text, set radio groups and checkboxes. Everything is working great. The only issue is when using form.flatten() , then checkboxes and radio groups do NOT show up in the flattened pdf.

Here's a summary psuedo-code:

    const form = pdfDoc.getForm();
    const pages = pdfDoc.getPages();
    const fields = form.getFields();
    const font = await pdfDoc.embedFont(StandardFonts.TimesRoman);
...
    drawText....

    radioGroup.select(value);
...
    form.flatten()
    await pdfDoc.save();

I tried everything:

Any ideas on why checkboxes and radiogroups are disappearing when form.flatten() is called?

How did you attempt to do it?

see above

What actually happened?

see above

What did you expect to happen?

see above

How can we reproduce the issue?

Flattening a pdf

Version

1.17.1

What environment are you running pdf-lib in?

Node

Checklist

Additional Notes

No response

AkshayPathak commented 9 months ago

Did you ever come across a solution for this?

wasims1 commented 9 months ago

I did not... if you come across any please share...

AkshayPathak commented 9 months ago

I just converted my checkbox to a text field and added an "X". Not sure if this work around will apply to your project.

redondi88 commented 7 months ago

I came across the same issue, and it fixed it with the following:

form.getCheckBox('check18').check();
form.getCheckBox('check18').defaultUpdateAppearances()

Hope this helps someone else

peterbartos commented 7 months ago

I came across the same issue, and it fixed it with the following:

form.getCheckBox('check18').check();
form.getCheckBox('check18').defaultUpdateAppearances()

Hope this helps someone else

That unfortunately changes the check style from x to tick, so not ideal

satyajitnayk commented 6 months ago

I have came across a fix by updating at least one of the check box/radio box border from thin to medium as shown in attached image.

image

form_to_flatten.pdf form_to_flatten_wide_border.pdf

ammulder commented 5 months ago

I'm seeing the same issue with a checkbox with a custom appearance (circular and etc.). When I flatten the form all the checkboxes appear unchecked. If I manually call updateAppearance or defaultUpdateAppearance on one of the checked ones, it does get a check mark (which is not the desired appearance) and also a slight rectangular border around the whole thing. So that doesn't work for me (in particular the border); I'd rather draw my own content over top of the widget.

FWIW I tried older versions back to when form flattening was introduced and this seems to have been an issue all along.

ammulder commented 5 months ago

I think the problem is in PDFForm.ts#findWidgetAppearanceRef where it only handles checkboxes and radio buttons if the normal appearance is a dictionary instead of a reference... in my file I think it's a reference. Therefore I think it skips the logic to look for the checked or unchecked appearance corresponding to the current field setting, and just supplies whatever the unmodified default appearance of the field is (for me, always unchecked).

I could try to patch this, but to be honest I can't get pdf-lib to build for me -- all the dependencies seem dated and it seems to run into a variety of TypeScript errors on the build step... if somebody could walk me through setting up an environment to get yarn install && yarn build to complete successfully, I could work on a fix.

satyajitnayk commented 5 months ago

The border of check boxes are transparent as shown in the image below. Just unchecked transparent border & now it works with flatten too. image