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

Scaling a page does not scale the embedded images #3109

Open
j-m-GD opened this issue Feb 7, 2025 · 1 comment
Open

Scaling a page does not scale the embedded images #3109

j-m-GD opened this issue Feb 7, 2025 · 1 comment
Labels
is-feature A feature request

Comments

@j-m-GD
Copy link

j-m-GD commented Feb 7, 2025

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

@stefan6419846
Copy link
Collaborator

Thanks for the report. This is a known limitation as element like images usually remain unaffected of the cm operation.

I am not completely sure how feasible an automated solution would be, but you are of course invited to propose a proper solution for this.

@stefan6419846 stefan6419846 changed the title Bug when trying to scale a PDF Scaling a page does not scale the embedded images Feb 7, 2025
@stefan6419846 stefan6419846 added the is-feature A feature request label Feb 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
is-feature A feature request
Projects
None yet
Development

No branches or pull requests

2 participants