Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ROB: cope with fields with upside down box/rectangle #2729

Merged
merged 1 commit into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 4 additions & 2 deletions pypdf/_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,9 @@ def set_need_appearances_writer(self, state: bool = True) -> None:

def create_viewer_preferences(self) -> ViewerPreferences:
o = ViewerPreferences()
self._root_object[NameObject(CatalogDictionary.VIEWER_PREFERENCES)] = self._add_object(o)
self._root_object[
NameObject(CatalogDictionary.VIEWER_PREFERENCES)
] = self._add_object(o)
return o

def add_page(
Expand Down Expand Up @@ -778,7 +780,7 @@ def _update_field_annotation(
) -> None:
# Calculate rectangle dimensions
_rct = cast(RectangleObject, anno[AA.Rect])
rct = RectangleObject((0, 0, _rct[2] - _rct[0], _rct[3] - _rct[1]))
rct = RectangleObject((0, 0, abs(_rct[2] - _rct[0]), abs(_rct[3] - _rct[1])))

# Extract font information
da = anno.get_inherited(
Expand Down
17 changes: 17 additions & 0 deletions tests/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2269,3 +2269,20 @@ def test_no_ressource_for_14_std_fonts(caplog):
p, {a["/T"]: "Brooks"}, auto_regenerate=False
)
assert "Font dictionary for /Helvetica not found." in caplog.text


@pytest.mark.enable_socket()
def test_field_box_upside_down():
"""Cf #2724"""
url = "https://github.com/user-attachments/files/15996356/FRA.F.6180.55.pdf"
name = "iss2724.pdf"
writer = PdfWriter(BytesIO(get_data_from_url(url, name=name)))
writer.update_page_form_field_values(None, {"FreightTrainMiles": "0"})
assert writer.pages[0]["/Annots"][13].get_object()["/AP"]["/N"].get_data() == (
b"q\n/Tx BMC \nq\n1 1 105.29520000000001 10.835000000000036 re\n"
b"W\nBT\n/Arial 8.0 Tf 0 g\n2 2.8350000000000364 Td\n(0) Tj\nET\n"
b"Q\nEMC\nQ\n"
)
box = writer.pages[0]["/Annots"][13].get_object()["/AP"]["/N"]["/BBox"]
assert box[2] > 0
assert box[3] > 0
Loading