Skip to content

Commit

Permalink
BUG: Cope with extra space (#2151)
Browse files Browse the repository at this point in the history
Closes #1903
  • Loading branch information
pubpub-zz authored Sep 10, 2023
1 parent 5a42b96 commit f68138c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions pypdf/_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
14 changes: 14 additions & 0 deletions tests/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit f68138c

Please sign in to comment.