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

Cannot update a form field with weird/special attributes. #2512

Closed
zacharysyoung opened this issue Mar 10, 2024 · 0 comments · Fixed by #2570
Closed

Cannot update a form field with weird/special attributes. #2512

zacharysyoung opened this issue Mar 10, 2024 · 0 comments · Fixed by #2570
Labels
PdfWriter The PdfWriter component is affected workflow-forms From a users perspective, forms is the affected feature/workflow

Comments

@zacharysyoung
Copy link

I tried to update a simple PDF form, but one field has some odd attributes, i.e., "/Ff": 0, "/Kids": [IndirectObject(10, 0, 4353225744)], that caused this error, KeyError: '/AP'.

Environment

Which environment were you using when you encountered the problem?

$ python -m platform
macOS-14.2.1-arm64-arm-64bit

$ python -c "import pypdf;print(pypdf._debug_versions)"
pypdf==4.1.0, crypt_provider=('local_crypt_fallback', '0.0.0'), PIL=none

Code + PDF

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

from pypdf import PdfReader, PdfWriter

reader = PdfReader("Simple-form.pdf")
writer = PdfWriter()
writer.append(reader)

writer.update_page_form_field_values(
    writer.pages[0],
    {"Last_name": "Bar"},
    auto_regenerate=False,
)

with open(f"output.pdf", "wb") as output_stream:
    writer.write(output_stream)

Simple-form.pdf

Traceback

This is the complete traceback I see:

Traceback (most recent call last):
  File "main.py", line 9, in <module>
    writer.update_page_form_field_values(
  File ".venv/lib/python3.12/site-packages/pypdf/_writer.py", line 955, in update_page_form_field_values
    value if value in k[AA.AP]["/N"] else "/Off"
                      ~^^^^^^^
  File ".venv/lib/python3.12/site-packages/pypdf/generic/_data_structures.py", line 319, in __getitem__
    return dict.__getitem__(self, key).get_object()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
KeyError: '/AP'

This seems related to #2234, exact traceback at least.

I ended up deleting the field and recreating it, and the /Ff and /Kids dict keys were absent, and the script above worked.

@stefan6419846 stefan6419846 added PdfWriter The PdfWriter component is affected workflow-forms From a users perspective, forms is the affected feature/workflow labels Mar 10, 2024
pubpub-zz added a commit to pubpub-zz/pypdf that referenced this issue Apr 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PdfWriter The PdfWriter component is affected workflow-forms From a users perspective, forms is the affected feature/workflow
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants