[Python] PyPDF2 add_blank_page has an unexpected cropping

Discussão em 'Python' iniciado por Stack, Setembro 29, 2024.

  1. Stack

    Stack Membro Participativo

    I am creating a new page with a PdfWriter to put there a scaled version of my original page, but the result has an unexpected cropping at a vertical line which is about 300pt away from the actual right page border

    from PyPDF2 import PdfReader, PdfWriter, PageObject

    def scale_pages(page1):
    scale_page = PageObject.create_blank_page(width=842, height=595) # Landscape A4 size
    if page1:
    page1.add_transformation([842/595, 0, 0, 842/595, 0, 595-842])
    scale_page.merge_page(page1)
    return scale_page
    def create_new_pdf(input_pdf, output_pdf):
    with open(input_pdf, 'rb') as infile:
    reader = PdfReader(infile)
    num_pages = len(reader.pages)
    writer = PdfWriter()

    for i in range(0, num_pages, 2):
    page1 = reader.pages
    new_page = scale_pages(page1)
    writer.add_page(new_page)

    with open(output_pdf, 'wb') as outfile:
    writer.write(outfile)

    input_pdf = 'input.pdf'
    output_pdf = 'output.pdf'

    create_new_pdf(input_pdf, output_pdf)


    input page output page

    I tried using Transformation() but the result is the same. I'd expect the scaled page occupy all width of the new page (and white space is there!)

    Continue reading...

Compartilhe esta Página