Skip to content

Commit f68138c

Browse files
authored
BUG: Cope with extra space (#2151)
Closes #1903
1 parent 5a42b96 commit f68138c

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

pypdf/_writer.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,7 @@ def _update_text_field(self, field: DictionaryObject) -> None:
862862
# Extract font information
863863
da = cast(str, field[AA.DA])
864864
font_properties = da.replace("\n", " ").replace("\r", " ").split(" ")
865+
font_properties = [x for x in font_properties if x != ""]
865866
font_name = font_properties[font_properties.index("Tf") - 2]
866867
font_height = float(font_properties[font_properties.index("Tf") - 1])
867868
if font_height == 0:

tests/test_writer.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1788,6 +1788,20 @@ def test_viewerpreferences():
17881788
assert writer.viewer_preferences is None
17891789

17901790

1791+
def test_extra_spaces_in_da_text(caplog):
1792+
writer = PdfWriter(clone_from=RESOURCE_ROOT / "form.pdf")
1793+
t = writer.pages[0]["/Annots"][0].get_object()["/DA"]
1794+
t = t.replace("/Helv", "/Helv ")
1795+
writer.pages[0]["/Annots"][0].get_object()[NameObject("/DA")] = TextStringObject(t)
1796+
writer.update_page_form_field_values(
1797+
writer.pages[0], {"foo": "abcd"}, auto_regenerate=False
1798+
)
1799+
t = writer.pages[0]["/Annots"][0].get_object()["/AP"]["/N"].get_data()
1800+
assert "Font dictionary for not found." not in caplog.text
1801+
assert b"/Helv" in t
1802+
assert b"(abcd)" in t
1803+
1804+
17911805
@pytest.mark.enable_socket()
17921806
def test_object_contains_indirect_reference_to_self():
17931807
url = "https://github.com/py-pdf/pypdf/files/12389243/testbook.pdf"

0 commit comments

Comments
 (0)