From 0456cf77fdbffa15c0c23aa16d7096fc2c994ee1 Mon Sep 17 00:00:00 2001 From: pubpub-zz <4083478+pubpub-zz@users.noreply.github.com> Date: Tue, 5 Sep 2023 22:02:27 +0200 Subject: [PATCH] BUG : cope with extra space closes #1903 --- pypdf/_writer.py | 1 + tests/test_writer.py | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/pypdf/_writer.py b/pypdf/_writer.py index dc132349e..5a197bff3 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 0d06f3f17..7c0af2049 100644 --- a/tests/test_writer.py +++ b/tests/test_writer.py @@ -1783,3 +1783,17 @@ def test_viewerpreferences(): assert reader.viewer_preferences is None writer = PdfWriter(clone_from=reader) 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