Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
coverage
  • Loading branch information
pubpub-zz committed Jul 22, 2023
commit 8407aaa10bd7e27dcde75d1e69625f52edd02b2c
10 changes: 5 additions & 5 deletions pypdf/_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -939,17 +939,17 @@ def update_page_form_field_values(
auto_regenerate: set/unset the need_appearances flag ;
the flag is unchanged if auto_regenerate is None
"""
if CatalogDictionary.ACRO_FORM not in self._root_object:
raise PyPdfError("No /AcroForm dictionary in PdfWriter Object")
af = cast(DictionaryObject, self._root_object[CatalogDictionary.ACRO_FORM])
if InteractiveFormDictEntries.Fields not in af:
raise PyPdfError("No /Fields dictionary in Pdf in PdfWriter Object")
if isinstance(auto_regenerate, bool):
self.set_need_appearances_writer(auto_regenerate)
# Iterate through pages, update field values
if PG.ANNOTS not in page:
logger_warning("No fields to update on this page", __name__)
return
if CatalogDictionary.ACRO_FORM not in self._root_object:
raise PyPdfError("No /AcroForm dictionary in PdfWriter Object")
af = cast(DictionaryObject, self._root_object[CatalogDictionary.ACRO_FORM])
if InteractiveFormDictEntries.Fields not in af:
raise PyPdfError("No /Fields dictionary in Pdf in PdfWriter Object")
# /Helvetica is just in case of but this is normally insufficient as we miss the font ressource
default_da = af.get(
InteractiveFormDictEntries.DA, TextStringObject("/Helvetica 0 Tf 0 g")
Expand Down
24 changes: 23 additions & 1 deletion tests/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
PdfWriter,
Transformation,
)
from pypdf.errors import DeprecationError, PageSizeNotDefinedError
from pypdf.errors import DeprecationError, PageSizeNotDefinedError, PyPdfError
from pypdf.generic import (
ArrayObject,
ContentStream,
Expand Down Expand Up @@ -1508,3 +1508,25 @@ def test_da_missing_in_annot():
fields = reader.get_form_text_fields()
fields["PCN-1"] = "0"
writer.update_page_form_field_values(writer.pages[0], fields)


def test_missing_fields(pdf_file_path):
reader = PdfReader(RESOURCE_ROOT / "form.pdf")

writer = PdfWriter()
writer.add_page(reader.pages[0])

with pytest.raises(PyPdfError) as exc:
writer.update_page_form_field_values(
writer.pages[0], {"foo": "some filled in text"}, flags=1
)
assert exc.value.args[0] == "No /AcroForm dictionary in PdfWriter Object"

writer = PdfWriter()
writer.append(reader, [0])
del writer._root_object["/AcroForm"]["/Fields"]
with pytest.raises(PyPdfError) as exc:
writer.update_page_form_field_values(
writer.pages[0], {"foo": "some filled in text"}, flags=1
)
assert exc.value.args[0] == "No /Fields dictionary in Pdf in PdfWriter Object"