Skip to content

Scaling a page does not scale the embedded images #3109

Open
@j-m-GD

Description

@j-m-GD

When trying to scale a PDF to a specific size, the objects inside do not scale properly, leaving a background that should not be visible.

Environment

It has been tested in multiple environments:

Python 3.13 with pypdf 5.1.0 and 5.2.0
Python 3.12 with various versions starting from 4.1.0

Code + PDF

from pypdf import PageObject, Transformation, PdfReader, PdfWriter, PaperSize
from pypdf.papersizes import Dimensions

def main( size: Dimensions = PaperSize.A5):
    reader = PdfReader('bug.pdf')
    
    pages: list[PageObject] = reader.pages
    scaled_pages: list[PageObject] = []

    for page in pages:
        new_page = PageObject.create_blank_page(
            width=size.width,
            height=size.height,
        )

        sx = size.width / float(page.mediabox.width)
        sy = size.height / float(page.mediabox.height)

        transformation = Transformation().scale(sx=sx, sy=sy)

        new_page.merge_transformed_page(page, transformation)

        scaled_pages.append(new_page)

    writer = PdfWriter()

    for page in scaled_pages:
        writer.add_page(page)

    with open('example.pdf', 'wb') as file:
        writer.write(file)

main()

bug.pdf

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions