PyPDFForm
PyPDFForm copied to clipboard
Error when dealing with integer / radio button
The error appears both when using PdfWrapper and FromWrapper, but with a different behavior.
Here is the test pdf file: https://www.cdc.gov/infectioncontrol/pdf/icar/IPC-demo-LTC-508.pdf
Loading the file shows this in the console:
Ignoring wrong pointing object 14 0 (offset 0)
Ignoring wrong pointing object 89 0 (offset 0)
EDIT: this could be the issue: https://github.com/py-pdf/pypdf/issues/2326
Calling the schema method:
pdf_form_schema = PdfWrapper("IPC-demo-LTC-508.pdf").schema
shows that the field "S1 GF 7" is of type "integer" (a radio button), with a maximum value of 2.
Problem 1: why is the maximum 2 when there are clearly 4 possible options? Starting with 0 = "Accute", 1 = "Long-term" , 2 = "Outpatient", but setting 3 = "Other" does nothing.
Problem 2: using FormWrapper setting the field "S1 GF 7" to any integer fails with the following message:
Traceback (most recent call last):
File "~/.local/share/virtualenvs/test_auto_fill-9T0hmLRz/lib/python3.9/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 600, in _run_script
exec(code, module.__dict__)
File "~/Documents/Projets/test_auto_fill/test.py", line 7, in <module>
filled = FormWrapper("IPC-demo-LTC-508.pdf").fill(
File "~/.local/share/virtualenvs/test_auto_fill-9T0hmLRz/lib/python3.9/site-packages/PyPDFForm/wrapper.py", line 60, in fill
self.stream = simple_fill(
File "~/.local/share/virtualenvs/test_auto_fill-9T0hmLRz/lib/python3.9/site-packages/PyPDFForm/filler.py", line 192, in simple_fill
simple_update_radio_value(annot)
File "~/.local/share/virtualenvs/test_auto_fill-9T0hmLRz/lib/python3.9/site-packages/PyPDFForm/patterns.py", line 109, in simple_update_radio_value
for each in annot[AP][D]: # noqa
File "~/.local/share/virtualenvs/test_auto_fill-9T0hmLRz/lib/python3.9/site-packages/pypdf/generic/_data_structures.py", line 409, in __getitem__
return dict.__getitem__(self, key).get_object()
KeyError: '/D'
Problem 3: using FormWrapper: setting the field "S1 GF 7" to a string, like '0', always checks the option "Other".
Problem 4: using PdfWrapper: setting the field "S1 GF 7" to a string does nothing (soft fail?); setting the field "S1 GF 7" to an integer between 0 and 2 checks the appropriate field. I have not found a way to check the "Other" field option. I was expecting the same behavior using string as FormWrapper.
Environment:
Python 3.9.6 pip 23.3.2
altair==5.3.0 attrs==23.2.0 blinker==1.8.2 cachetools==5.3.3 certifi==2024.2.2 cffi==1.16.0 chardet==5.2.0 charset-normalizer==3.3.2 click==8.1.7 cryptography==42.0.7 gitdb==4.0.11 GitPython==3.1.43 idna==3.7 Jinja2==3.1.4 jsonschema==4.22.0 jsonschema-specifications==2023.12.1 markdown-it-py==3.0.0 MarkupSafe==2.1.5 mdurl==0.1.2 numpy==1.26.4 packaging==24.0 pandas==2.2.2 pillow==10.3.0 protobuf==4.25.3 pyarrow==16.0.0 pycparser==2.22 pydeck==0.9.0 Pygments==2.18.0 pypdf==4.2.0 PyPDFForm==1.4.24 python-dateutil==2.9.0.post0 pytz==2024.1 referencing==0.35.1 reportlab==4.2.0 requests==2.31.0 rich==13.7.1 rpds-py==0.18.1 six==1.16.0 smmap==5.0.1 streamlit==1.34.0 tenacity==8.3.0 toml==0.10.2 toolz==0.12.1 tornado==6.4 typing_extensions==4.11.0 tzdata==2024.1 urllib3==2.2.1