Skip to content

PageObject.scale() scales media box incorrectly #3487

@dominik-korsa

Description

@dominik-korsa

The file scaling example from the docs does not work. The content is scaled correctly, but the resulting page size is unexpected, as if the scaling got applied twice. See the reproduction example.

Environment

Which environment were you using when you encountered the problem?

$ python -m platform
Linux-6.14.0-32-generic-x86_64-with-glibc2.41

$ python -c "import pypdf;print(pypdf._debug_versions)"
pypdf==6.1.1, crypt_provider=('cryptography', '45.0.7'), PIL=none

Code + PDF

This is a minimal, complete example that shows the issue:

# /// script
# requires-python = ">=3.12"
# dependencies = [
#     "pypdf",
# ]
# ///
from pypdf import PdfReader, PdfWriter

reader = PdfReader("input.pdf") # A4 document
writer = PdfWriter()

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

# The output contains A8 pages instead,
# with the content scaled by the factor 0.5
writer.write("output.pdf")

The input PDF, A4:
input.pdf

Image

The output contains A8 pages, not A6 as expected. The content is scaled correctly by the factor 0.5:

output.pdf
Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    is-bugFrom a users perspective, this is a bug - a violation of the expected behavior with a compliant PDFis-regressionRegression introduced as a side-effect of another change

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions