pypdf icon indicating copy to clipboard operation
pypdf copied to clipboard

BUG: page.mediabox not updating correctly when merging

Open felle9900 opened this issue 2 years ago • 4 comments

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.

felle9900 avatar Aug 23 '21 12:08 felle9900

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.

FraPsiZeta avatar Sep 03 '21 19:09 FraPsiZeta

Yeah I had to make a edit in pdf.py (pypdf2) to finally get what I wanted.

felle9900 avatar Sep 03 '21 19:09 felle9900

Could you make a pr with the fix? Maybe also with a unit test that shows the issue?

MartinThoma avatar Apr 06 '22 21:04 MartinThoma

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"])

felle9900 avatar Apr 06 '22 21:04 felle9900

This is not fixed by the recent release, right?

Lars147 avatar Feb 22 '23 13:02 Lars147

Yes It should. as you were at the author can you close it with the reference to the good PR ? (I'm delegating now 😁)

pubpub-zz avatar Feb 22 '23 18:02 pubpub-zz

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.

Lars147 avatar Feb 23 '23 08:02 Lars147

this is closed by #1567

pubpub-zz avatar Feb 23 '23 20:02 pubpub-zz