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

writer.add_page(page) increasing page size #1338

Closed
Atomnp opened this issue Sep 9, 2022 · 3 comments
Closed

writer.add_page(page) increasing page size #1338

Atomnp opened this issue Sep 9, 2022 · 3 comments
Labels
is-bug From a users perspective, this is a bug - a violation of the expected behavior with a compliant PDF

Comments

@Atomnp
Copy link
Contributor

Atomnp commented Sep 9, 2022

writer. add_page(page) increase page size, I have a pdf file, each file of this pdf file contains two slides merged into one, I want to split each page into two and create a new pdf by doing this to all pages in the pdf. I have included my solution which doesn't work. The issue is that it never finishes running. When I tried to inspect the issue I found that the size of the page object in each iteration increased very quickly.

Environment

Arch linux
python version 3.10.6

Code + PDF

from PyPDF2 import PdfWriter, PdfReader
reader = PdfReader('rmi.pdf') 
writer = PdfWriter()
import copy

for page in reader.pages:
    
    left_half=copy.deepcopy(page)
    right_half=copy.deepcopy(page)
    

    old_ll=page.cropbox.lower_left 
    old_lr=page.cropbox.lower_right
    old_ul= page.cropbox.upper_left
    old_ur= page.cropbox.upper_right 
    

    x1,y1=old_ur[0]/2,old_ur[1]
    x2,y2=old_lr[0]/2,old_lr[1]
    
    left_half.mediabox.upper_left = old_ul 
    left_half.mediabox.lower_right = (x2,y2)


    right_half.mediabox.upper_left = (x1,y1) 
    right_half.mediabox.lower_right = old_lr
    
    writer.add_page(left_half)
    writer.add_page(right_half)
  
with open('result.pdf','wb') as fp:
    writer.write(fp) 

rmi.pdf

I also found the working solution but I don't know why the code below works but the one above don't

from PyPDF2 import PdfWriter, PdfReader
reader = PdfReader('rmi.pdf') 
writer = PdfWriter()
import copy

for page in reader.pages:
    
    left_half=copy.deepcopy(page)
    right_half=copy.deepcopy(page)
    

    old_ll=page.cropbox.lower_left 
    old_lr=page.cropbox.lower_right
    old_ul= page.cropbox.upper_left
    old_ur= page.cropbox.upper_right 
    

    x1,y1=old_ur[0]/2,old_ur[1]
    x2,y2=old_lr[0]/2,old_lr[1]
    
    left_half.mediabox.upper_left = old_ul 
    left_half.mediabox.lower_right = (x2,y2)


    right_half.mediabox.upper_left = (x1,y1) 
    right_half.mediabox.lower_right = old_lr
    
    writer.add_page(left_half)
    writer.add_page(right_half)
  
with open('result.pdf','wb') as fp:
    writer.write(fp) 
@MartinThoma
Copy link
Member

What is different in the two versions? Could you maybe add a comment in the lines of the second code block that differ from the first one?

@MartinThoma MartinThoma added the is-bug From a users perspective, this is a bug - a violation of the expected behavior with a compliant PDF label Sep 24, 2022
pubpub-zz added a commit to pubpub-zz/pypdf that referenced this issue Sep 27, 2022
@pubpub-zz
Copy link
Collaborator

@Atomnp
Can you retry with latest version and confirm this is closed

@pubpub-zz
Copy link
Collaborator

I close this issue as fixed. @Atomnp feel free to provide more info if you want to reopen it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
is-bug From a users perspective, this is a bug - a violation of the expected behavior with a compliant PDF
Projects
None yet
Development

No branches or pull requests

3 participants