pypdf
pypdf copied to clipboard
BUG: page.mediabox not updating correctly when merging
I cropped a 2 page business card PDF file so most of the bleed and cropmarks where cut of. I wanted my card to have a Trim Size of 85 x 55 mm and have a bleed of 2.5 mm. Doing so - when I place 2 cards right next to each other, I have a 5 mm gap. This is what I want.
The cropping looked like It was doing ok, I opened the cropped pdf and it looked fine. BUT when I try placing this cropped pdf, suddenly its placing the pages like it was never cropped! Its placing the full original mediaBox size. Theres a bug in there somehow... I even tried placing the cropped pdf in indesign and "brew" a new identical pdf, to se if it was a bug in the file. The indesign pdf imported correctly showing the file as intended.
I had the same problem, cropping a pdf changing mediaBox (or TrimBox or cropBox) attributes seems to work fine, saving the cropped page as a new PDF show the cropped image as intended, but merging the cropped pdf (even the one saved on file) keeps adding the original uncropped image.
Yeah I had to make a edit in pdf.py (pypdf2) to finally get what I wanted.
Could you make a pr with the fix? Maybe also with a unit test that shows the issue?
I'm not that into githubs features yet :) But this is what I altered, in the "pdf.py" file in PyPDF2 for it to work with my needs:
Add this after line 2280 (indented) (if page1Content is not None:)
if page2.mediaBox.getWidth() == page2.trimBox.getWidth():
print("PyPDF2: mediaBox is equal to trimBox")
page2Content = ContentStream(page2Content, self.pdf)
page2Content.operations.insert(0, [map(FloatObject, [float(page2.trimBox.getLowerLeft_x()), float(page2.trimBox.getLowerLeft_y()), float(page2.trimBox.getWidth()), float(page2.trimBox.getHeight())]), "re"])
page2Content.operations.insert(1, [[], "W"])
page2Content.operations.insert(2, [[], "n"])
elif page2.mediaBox.getWidth() > page2.trimBox.getWidth():
print("PyPDF2: mediaBox is greater than trimBox")
page2Content = ContentStream(page2Content, self.pdf)
bleed = float(7.086614175)
page2Content.operations.insert(0, [map(FloatObject, [float(page2.trimBox.getLowerLeft_x()) - bleed, float(page2.trimBox.getLowerLeft_y()) - bleed, float(page2.trimBox.getWidth()) + bleed + bleed, float(page2.trimBox.getHeight()) + bleed + bleed]), "re"])
page2Content.operations.insert(1, [[], "W"])
page2Content.operations.insert(2, [[], "n"])
This is not fixed by the recent release, right?
Yes It should. as you were at the author can you close it with the reference to the good PR ? (I'm delegating now 😁)
Yes It should. as you were at the author can you close it with the reference to the good PR ? (I'm delegating now 😁)
I am sorry but I do not understand what you are referring to with "as you were at the author".
Unfortunately, I was not able to merge a cropped pdf in the desired way.
I upgraded to Release 3.4.1, where it is using the cropbox
instead of trimbox
for merging. However, when merging the cropped PDF it was still showing this unwanted result.
I am happy to prepare an example if desired.
this is closed by #1567