Skip to content

UnboundLocalError for appearance_stream_obj when doing update_page_form_field_values with flatten in 6.2.0 #3633

@John-Sharp

Description

@John-Sharp

When upgrading to 6.2.0 I noticed there was an UnboundLocalError when processing a pdf with a /Sig field. It does produce a warning 'Signature forms not implemented yet' but I didn't expect it to crash

Environment

$ python -m platform
Linux-6.8.0-90-generic-x86_64-with-glibc2.39

$ python -c "import pypdf;print(pypdf._debug_versions)"
pypdf==6.6.2, crypt_provider=('local_crypt_fallback', '0.0.0'), PIL=12.1.0

Code + PDF

import io
import pypdf

writer = pypdf.PdfWriter(io.BytesIO(open("minimal_signature.pdf", "rb").read()))

for page in writer.pages:
    writer.update_page_form_field_values(
        page,
        # {"signature": "blah", "date": "xxx"},
        {"signature": "ddd",},
        auto_regenerate=False,
        flatten=True
    )


writer.write(open("op.pdf", "wb"))

minimal_signature.pdf
(this can be added to tests)

Traceback

This is the complete traceback I see:

Signature forms not implemented yet
Traceback (most recent call last):
  File "/home/johnsharp/scratch/pypdftest.py", line 7, in <module>
    writer.update_page_form_field_values(
  File "/home/johnsharp/code/pypdf/pypdf/_writer.py", line 1067, in update_page_form_field_values
    if flatten and appearance_stream_obj is not None:
                   ^^^^^^^^^^^^^^^^^^^^^
UnboundLocalError: cannot access local variable 'appearance_stream_obj' where it is not associated with a value

Metadata

Metadata

Assignees

No one assigned

    Labels

    is-bugFrom a users perspective, this is a bug - a violation of the expected behavior with a compliant PDFworkflow-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