Skip to content
Open
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: 3 additions & 3 deletions pypdf/_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -1503,7 +1503,7 @@ def scale(self, sx: float, sy: float) -> None:
Scale a page by the given factors by applying a transformation matrix
to its content and updating the page size.

This updates the various page boundaries (mediabox, cropbox, etc.)
This updates the various page boundaries (artbox, cropbox, etc.)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change does not seem necessary?

and the contents of the page.

Args:
Expand All @@ -1512,11 +1512,11 @@ def scale(self, sx: float, sy: float) -> None:

"""
self.add_transformation((sx, 0, 0, sy, 0, 0))
self.mediabox = self.mediabox.scale(sx, sy)
self.cropbox = self.cropbox.scale(sx, sy)
self.bleedbox = self.bleedbox.scale(sx, sy)
self.trimbox = self.trimbox.scale(sx, sy)
self.artbox = self.artbox.scale(sx, sy)
self.cropbox = self.cropbox.scale(sx, sy)
self.mediabox = self.mediabox.scale(sx, sy)

if PG.ANNOTS in self:
annotations = self[PG.ANNOTS]
Expand Down
68 changes: 68 additions & 0 deletions tests/test_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import json
import logging
import math
import shutil
import subprocess
from copy import deepcopy
from io import BytesIO
from pathlib import Path
Expand All @@ -28,11 +30,13 @@
)

from . import get_data_from_url, normalize_warnings
from .test_images import image_similarity

TESTS_ROOT = Path(__file__).parent.resolve()
PROJECT_ROOT = TESTS_ROOT.parent
RESOURCE_ROOT = PROJECT_ROOT / "resources"
SAMPLE_ROOT = PROJECT_ROOT / "sample-files"
GHOSTSCRIPT_BINARY = shutil.which("gs")


def get_all_sample_files():
Expand Down Expand Up @@ -1504,3 +1508,67 @@ def __getitem__(self, item) -> Any:
page[NameObject("/Resources")] = resources
with mock.patch.object(none_reference, "get_object", return_value=None):
assert page.extract_text() == ""


@pytest.mark.enable_socket
def test_scale_by():
"""Tests for #3487"""
url = "https://github.com/user-attachments/files/22685841/input.pdf"
name = "issue3487.pdf"
reader = PdfReader(BytesIO(get_data_from_url(url, name=name)))

original_box = RectangleObject((0, 0, 595.275604, 841.88974))
expected_box = RectangleObject((0.0, 0.0, 297.637802, 420.94487))
for page in reader.pages:
assert page.artbox == original_box
assert page.bleedbox == original_box
assert page.cropbox == original_box
assert page.mediabox == original_box
assert page.trimbox == original_box

page.scale_by(0.5)
assert page.artbox == expected_box
assert page.bleedbox == expected_box
assert page.cropbox == expected_box
assert page.mediabox == expected_box
assert page.trimbox == expected_box


@pytest.mark.enable_socket
@pytest.mark.skipif(GHOSTSCRIPT_BINARY is None, reason="Requires Ghostscript")
def test_box_rendering(tmp_path):
"""Tests for issue #3487."""
url = "https://github.com/user-attachments/files/22685841/input.pdf"
name = "issue3487.pdf"
reader = PdfReader(BytesIO(get_data_from_url(url, name=name)))
writer = PdfWriter()

for page in reader.pages:
page.scale_by(0.5)
writer.add_page(page)

target_png_path = tmp_path / "target.png"
url = "https://github.com/user-attachments/assets/e9c2271c-bfc3-4a6f-8c91-ffefa24502e2"
name = "issue3487.png"
target_png_path.write_bytes(get_data_from_url(url, name=name))

pdf_path = tmp_path / "out.pdf"
writer.write(pdf_path)

for box in ["Art", "Bleed", "Crop", "Media", "Trim"]:
png_path = tmp_path / f"{box}.png"
# False positive: https://github.com/PyCQA/bandit/issues/333
subprocess.run( # noqa: S603
[
GHOSTSCRIPT_BINARY,
f"-dUse{box}Box",
"-dFirstPage=1",
"-dLastPage=1",
"-sDEVICE=pngalpha",
"-o",
png_path,
pdf_path,
]
)
assert png_path.is_file(), box
assert image_similarity(png_path, target_png_path) >= 0.95, box
Loading