gettalong / hexapdf

Versatile PDF creation and manipulation for Ruby
https://hexapdf.gettalong.org
Other
1.21k stars 69 forks source link

Form flatten removes field values from some PDFs #304

Closed omohokcoj closed 3 months ago

omohokcoj commented 3 months ago

https://github.com/docusealco/docuseal/issues/244

https://github.com/docusealco/docuseal/files/14804201/RR_model_tenancy_1.2_shared_house.pdf

pdf = HexaPDF::Document.new(io: File.open('RR_model_tenancy_1.2_shared_house (6).pdf'))
pdf.acro_form.flatten
pdf.write('result.pdf')

When open the flatten result.pdf form fields are removed from the PDF but text values of the fields are missing on the page.

gettalong commented 3 months ago

If you flatten the form, appearances are created by default if they do not exist. However, in case of that PDF file and the text fields on page 3, appearances do exist but they don't show anything.

You can make this work by forcing the creation of appearance streams even if they already exist by calling pdf.acro_form.create_appearances(force: true) before flattening.

Another thing to note is that the file has the pdf.acro_form[:NeedAppearances] entry set to true which indicates that the library/viewer should generate all the appearances. So you could either check that entry and only conditionally recreate all appearances. Or just recreate all appearances by default.