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

KeyError '/Opt' in get_fields #2838

Open
alesdotio opened this issue Sep 8, 2024 · 4 comments
Open

KeyError '/Opt' in get_fields #2838

alesdotio opened this issue Sep 8, 2024 · 4 comments
Labels
key-error Could be a bug, but also a robustness issue workflow-forms From a users perspective, forms is the affected feature/workflow

Comments

@alesdotio
Copy link

Replace this: What happened? What were you trying to achieve?

Trying to get the fields out of a PDF.

Environment

Which environment were you using when you encountered the problem?

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

$ python -c "import pypdf;print(pypdf._debug_versions)"
pypdf==4.3.1, crypt_provider=('pycryptodome', '3.20.0'), PIL=10.4.0

Code + PDF

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

reader = PdfReader(pdf)
fields = reader.get_fields()

Share here the PDF file(s) that cause the issue. The smaller they are, the
better. Let us know if we may add them to our tests!

Unfortunately, I cannot share the PDF.

Traceback

This is the complete traceback I see:

    fields = reader.get_fields()
../../.venv/lib/python3.12/site-packages/pypdf/_doc_common.py:533: in get_fields
    self._build_field(field, retval, fileobj, field_attributes, stack)
../../.venv/lib/python3.12/site-packages/pypdf/_doc_common.py:570: in _build_field
    retval[key][NameObject("/_States_")] = obj[NameObject(FA.Opt)]
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = {'/DA': '/AgencyFB 8 Tf 0 g', '/FT': '/Ch', '/Ff': 917504, '/Kids': [IndirectObject(2453, 0, 4403691248), IndirectObject(2235, 0, 4403691248)], '/T': 'Designation', '/V': 'Town'}
key = '/Opt'

    def __getitem__(self, key: Any) -> PdfObject:
>       return dict.__getitem__(self, key).get_object()
E       KeyError: '/Opt'

../../.venv/lib/python3.12/site-packages/pypdf/generic/_data_structures.py:417: KeyError

Changing this line in _doc_common.py:570 fixes the problem for my case, but I have no idea what the consequences are:

        if obj.get(FA.FT, "") == "/Ch" and obj.get(NameObject(FA.Opt)):
            retval[key][NameObject("/_States_")] = obj[NameObject(FA.Opt)]
@stefan6419846 stefan6419846 added workflow-forms From a users perspective, forms is the affected feature/workflow key-error Could be a bug, but also a robustness issue labels Sep 8, 2024
@pubpub-zz
Copy link
Collaborator

@alesdotio
Without data we can not complete an anamlysis.
I see 2 options to try to go forward:
a) take an empty copy of the form, check if the error is still present and provide us with
b) send privately a copy to @stefan6419846 : we promise to not disclose the data

@pubpub-zz
Copy link
Collaborator

@alesdotio
Is this approach OK for you ? else we will have to close this as analysis can not be done

@alesdotio
Copy link
Author

@alesdotio Is this approach OK for you ? else we will have to close this as analysis can not be done

Yeah, that's fair. I will try to reproduce it on another file and reopen the issue if I manage.

My guess is that this is a choice field without any choices defined?

@pubpub-zz
Copy link
Collaborator

@alesdotio any update ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
key-error Could be a bug, but also a robustness issue workflow-forms From a users perspective, forms is the affected feature/workflow
Projects
None yet
Development

No branches or pull requests

3 participants