diff --git a/pypdf/_writer.py b/pypdf/_writer.py index 4ef8b9ffa..25a6444d3 100644 --- a/pypdf/_writer.py +++ b/pypdf/_writer.py @@ -862,6 +862,7 @@ def _update_text_field(self, field: DictionaryObject) -> None: # Extract font information da = cast(str, field[AA.DA]) font_properties = da.replace("\n", " ").replace("\r", " ").split(" ") + font_properties = [x for x in font_properties if x != ""] font_name = font_properties[font_properties.index("Tf") - 2] font_height = float(font_properties[font_properties.index("Tf") - 1]) if font_height == 0: diff --git a/tests/test_writer.py b/tests/test_writer.py index 6c4764cb7..c9766f979 100644 --- a/tests/test_writer.py +++ b/tests/test_writer.py @@ -1788,6 +1788,20 @@ def test_viewerpreferences(): assert writer.viewer_preferences is None +def test_extra_spaces_in_da_text(caplog): + writer = PdfWriter(clone_from=RESOURCE_ROOT / "form.pdf") + t = writer.pages[0]["/Annots"][0].get_object()["/DA"] + t = t.replace("/Helv", "/Helv ") + writer.pages[0]["/Annots"][0].get_object()[NameObject("/DA")] = TextStringObject(t) + writer.update_page_form_field_values( + writer.pages[0], {"foo": "abcd"}, auto_regenerate=False + ) + t = writer.pages[0]["/Annots"][0].get_object()["/AP"]["/N"].get_data() + assert "Font dictionary for not found." not in caplog.text + assert b"/Helv" in t + assert b"(abcd)" in t + + @pytest.mark.enable_socket() def test_object_contains_indirect_reference_to_self(): url = "https://github.com/py-pdf/pypdf/files/12389243/testbook.pdf"