-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Closed
Labels
PdfWriterThe PdfWriter component is affectedThe PdfWriter component is affectedworkflow-formsFrom a users perspective, forms is the affected feature/workflowFrom a users perspective, forms is the affected feature/workflow
Description
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)
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
Labels
PdfWriterThe PdfWriter component is affectedThe PdfWriter component is affectedworkflow-formsFrom a users perspective, forms is the affected feature/workflowFrom a users perspective, forms is the affected feature/workflow