-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
page1.merge_page(page2, expand=True) doesn't seem to work #1035
Comments
Ok 😄 Can you share those? |
Here [main.txt](https://github.com/py-pdf/PyPDF2/files/9002217/main.txt |
Thanks for sharing! It looks to me as if you're merging them as
The x-axis goes (left-right), the y-axis goes (top-bottom). |
Yep, but even with that, the | 1 | is still missing or partly written... |
This is an intermediate fix: from PyPDF2.generic import RectangleObject
from PyPDF2 import PdfReader, PdfWriter, Transformation
# Get the original data
reader = PdfReader("1.pdf")
page1 = reader.pages[0]
print(page1.cropbox)
reader2 = PdfReader("2.pdf")
page2 = reader2.pages[0]
# Merge them into one page
offset = page1.cropbox.right
print(offset)
op = Transformation().translate(tx=offset, ty=0)
page2.add_transformation(op)
cb = page2.cropbox
page2.mediabox = RectangleObject((cb.left + offset, cb.bottom, cb.right + offset, cb.top))
page2.cropbox = RectangleObject((cb.left + offset, cb.bottom, cb.right + offset, cb.top))
page2.trimbox = RectangleObject((cb.left + offset, cb.bottom, cb.right + offset, cb.top))
page2.bleedbox = RectangleObject((cb.left + offset, cb.bottom, cb.right + offset, cb.top))
page2.artbox = RectangleObject((cb.left + offset, cb.bottom, cb.right + offset, cb.top))
page1.merge_page(page2, expand=True)
mb = page1.mediabox
page1.mediabox = RectangleObject((mb.left, mb.bottom, mb.right, mb.top))
page1.cropbox = RectangleObject((mb.left, mb.bottom, mb.right, mb.top))
page1.trimbox = RectangleObject((mb.left, mb.bottom, mb.right , mb.top))
page1.bleedbox = RectangleObject((mb.left, mb.bottom, mb.right, mb.top))
page1.artbox = RectangleObject((mb.left, mb.bottom, mb.right, mb.top))
print(page1.mediabox )
# Write the result back
writer = PdfWriter()
writer.add_page(page1)
with open("output.pdf", "wb") as fp:
writer.write(fp) |
There are two issues in PyPDF2:
|
Ok thanks a lot ! |
Please leave the issue open, even if the quick-fix works for you. I want the library itself to be correct. At the very least I feel like we need more documentation here, but I'm pretty certain we need to adjust PyPDF2. |
On 10.07.2022 I've merged #1066 . Transformations done via the
only affect the content of a page, not the page itself. I've just noticed that this is not documented (e.g. in https://pypdf2.readthedocs.io/en/latest/user/cropping-and-transforming.html ). I'll fix that. |
I am trying to merge two PDF page into one page. One thing is, those Pdf are kinda long (3 meters each, so 6 meters in the end)
When I try to merge those two pdf, the second PDF doesn't write past a certain area.
Code
Environment
PyPDF==2.4.0
The text was updated successfully, but these errors were encountered: