Skip to content
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

Fields deleted/cleared when calling update_page_form_field_values #1804

Closed
neversphere opened this issue Apr 19, 2023 · 1 comment · Fixed by #1806
Closed

Fields deleted/cleared when calling update_page_form_field_values #1804

neversphere opened this issue Apr 19, 2023 · 1 comment · Fixed by #1806

Comments

@neversphere
Copy link
Contributor

neversphere commented Apr 19, 2023

I'm trying to automatically populate/update fields in a PDF but when I call update_page_form_field_values some of the fields in the form are cleared. If I just read the pdf and write it without calling update_page_form_field_values the fields show up when the PDF is rendered. These fields are also cleared if I call writer.set_need_appearances_writer() without calling update_page_form_field_values.

Original PDF: https://drive.google.com/file/d/1QRlVzVmB63xwN-Ybm-sEhyozenCBBn6q/view?usp=sharing

PDF just read and written: https://drive.google.com/file/d/1fJhod2UnneXegunQocR_cPs5TCnekwRh/view?usp=sharing

PDF post update_page_form_field_valuess: https://drive.google.com/file/d/1PutoaB1YLJNfk9_kfuXfRmRQSb7Ka2hp/view?usp=sharing

The following fields are clear after the empty dict is used for the update_page_form_field_values:

21A
21E
32
33

These fields are also cleared if a populated dict is used to update other fields.


Current code to update:

import sys, getopt
from pypdf import PdfReader, PdfWriter

argv = sys.argv[1:]

reader = PdfReader(argv[0])
writer = PdfWriter()

page = reader.pages[0]
writer.add_page(page)

with open(argv[0]+'-no-change.pdf', "wb") as output_stream:
    writer.write(output_stream)

writer.update_page_form_field_values(
    writer.pages[0], {}
)

with open(argv[0]+'-empty-dict-change.pdf', "wb") as output_stream:
    writer.write(output_stream)
@neversphere neversphere changed the title Fields deleted when calling update_page_form_field_values Fields deleted/cleared when calling update_page_form_field_values Apr 21, 2023
@neversphere
Copy link
Contributor Author

This was a documentation error. Updated with #1806

MartinThoma pushed a commit that referenced this issue Apr 23, 2023
Update docs to use append rather than add_page so that the PDF structure is copied. This ensures the form structure is kept which is necessary for some PDFs.

See #1790
Closes #1792
Closes #1804
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant