Skip to content

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

@zacharysyoung

Description

@zacharysyoung

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    PdfWriterThe PdfWriter component is affectedworkflow-formsFrom a users perspective, forms is the affected feature/workflow

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions