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

Radio buttons. - KeyError: "2 Not An Option, Options are ['0', '1']" #63

Open
NexPlex opened this issue Oct 22, 2024 · 1 comment
Open

Comments

@NexPlex
Copy link

NexPlex commented Oct 22, 2024

Hi I'm getting this error on some Radio buttons.

File "C:\sites\backend\env\lib\site-packages\fillpdf\fillpdfs.py", line 251, in write_fillable_pdf
raise KeyError(f"{data_dict[key]} Not An Option, Options are {options}")
KeyError: "2 Not An Option, Options are ['0', '1']"

In the PDF, the values are shown as 1 and 2

When I inspect for each in annotation['/Kids']: keys2 = each['/AP']['/N'].keys(). I can't find where these choices are saved.

Any suggestion how to work around this?

image

image

@NexPlex
Copy link
Author

NexPlex commented Oct 23, 2024

here is my work around.

I updated
def write_fillable_pdf(input_pdf_path, output_pdf_path, data_dict, flatten=False):

after for each, I added this check.

if annotation[ANNOT_FORM_options] or (
each['/Parent'] and each['/Parent']['/Opt']):

Like this. I will post a fork.

                                                                                                       key_value = data_dict[key]
                                    for each in annotation['/Kids']:
                                        if not annotation[ANNOT_FORM_options]:
                                            continue
                                        keys2 = each['/AP']['/N'].keys()
                                        if '/Off' in keys2:
                                            keys2.remove('/Off')
                                        if ['/Off'] in keys:
                                            keys2.remove('/Off')
                                        export = keys2[0]
                                        if '/' in export:
                                            options.append(export[1:])
                                        else:
                                            options.append(export)
                                        form_options = None
                                        if annotation[ANNOT_FORM_options] or (
                                                each['/Parent'] and each['/Parent']['/Opt']):
                                            if annotation[ANNOT_FORM_options]:
                                                form_options = [(x.strip('()')) for x in annotation[ANNOT_FORM_options]]

                                            if each['/Parent'] and each['/Parent']['/Opt']:
                                                # custom choices =  /AP/N choices
                                                form_options = [(x.strip('()')) for x in each['/Parent']['/Opt']]
                                            if form_options:
                                                options = form_options
                                        if (is_int_convertible(data_dict[key]) and key_value == data_dict[key]):
                                            try:
                                                key_value = str(form_options.index(data_dict[key]))
                                            except ValueError:
                                                pass
                                                # print('index not found')
                                        if f'/{key_value}' == export:
                                            val_str = pdfrw.objects.pdfname.BasePdfName(f'/{key_value}')
                                        else:
                                            val_str = pdfrw.objects.pdfname.BasePdfName(f'/Off')
                                        if set(keys).intersection(set(temp_dict.values())):
                                            each.update(pdfrw.PdfDict(AS=val_str))
                                    # if data_dict[key] not in options:
                                    #     if key_value != "None"  and data_dict[key] != "":
                                    #         print(KeyError(f"{data_dict[key]} Not An Option for key '{key}, "
                                    #                        f"on page '{pg_count}' loop '{pg_loop}' Options are {options}"))
                                    # else:
                                    if set(keys).intersection(key_value):
                                        annotation.update(
                                            pdfrw.PdfDict(V=pdfrw.objects.pdfname.BasePdfName(f'/{key_value}')))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant