py-pdf / pypdf

A pure-python PDF library capable of splitting, merging, cropping, and transforming the pages of PDF files
https://pypdf.readthedocs.io/en/latest/
Other
8.07k stars 1.39k forks source link

update_page_form_field_values prevents barcode (pdf417) from displaying correctly #1306

Closed BenjaminFarris closed 1 year ago

BenjaminFarris commented 2 years ago

Here's a description of the issue https://stackoverflow.com/questions/73533672/pypdf2-doesnt-display-barcode-pdf417-after-using-update-page-form-field-value/73535421#73535421

pdf417 barcode isn't displayed after using update_page_form_field_values()

Environment

Which environment were you using when you encountered the problem?

$ python -m platform
Windows-10-10.0.19043-SP0

$ python -c "import PyPDF2;print(PyPDF2.__version__)"
2.10.3

Code + PDF

This is a minimal, complete example that shows the issue:

from PyPDF2 import PdfReader, PdfWriter

reader = PdfReader("i-130.pdf", strict=True)
pager = reader.pages[0]
field_dict = {"Pt2Line4b_GivenName[0]": "Mark"}

writer = PdfWriter(strict=True)
writer.add_page(pager)
writer.update_page_form_field_values(pager, fields=field_dict)

with open("newfile.pdf", "wb") as fh:
    writer.write(fh)

The PDF: https://www.uscis.gov/sites/default/files/document/forms/i-130.pdf - you can add it to the tests

The issue

It works, just doesn't work correctly

pubpub-zz commented 1 year ago

Currently , when you are using update_page_form_field_values() you are setting the flag NeedAppearances to true which is asking the viewer to regenerate the appearances of the fields when opening the file : this mean that the "field" of the code is regenerated.

I've started a PR to prepare a fix to not set the NeedAppearances flag but generate the fields Appearance