Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

mergeRotatedTranslatedPage doubt #558

Closed
josejachuf opened this issue Jun 2, 2020 · 5 comments
Closed

mergeRotatedTranslatedPage doubt #558

josejachuf opened this issue Jun 2, 2020 · 5 comments
Labels
is-bug From a users perspective, this is a bug - a violation of the expected behavior with a compliant PDF needs-discussion The PR/issue needs more discussion before we can continue

Comments

@josejachuf
Copy link

Hello everyone

I can't understand what the process is like when applying page1.mergeRotatedTranslatedPage (page2, ...)

Assuming the two pages of equal size

  1. Given page1 portrait (rotated 90 °) and page2 landscape
  • Are they considered aligned bottom-left?
  • What is the rotation point?
  • Once rotated, it is transferred by tx and ty?
  • Does all of the above apply to page2?

The following works well:

page1.mergeRotatedTranslatedPage (
    page2,
    rotation = 90,
    tx = page1.mediaBox.getWidth () / 2,
    ty = page1.mediaBox.getWidth () / 2,
    expand = True
    )
    1. Given page1 portrait (rotated 270 °) and page2 landscape

This works fine

page1.mergeRotatedTranslatedPage (
    page2,
    rotation = 270,
    tx = page1.mediaBox.getHeight () / 2,
    ty = page1.mediaBox.getHeight () / 2,
    expand = True
    )
  1. The problem I am having with page1 portrait (rotated 270 °) with cropBox! = MediaBox
pag1.mergeRotatedTranslatedPage (
    page2,
    rotation = 270,
    tx = page1.mediaBox.getHeight () / 2 + page1.cropBox [0],
    ty = page1.mediaBox.getHeight () / 2 + page1.cropBox [1],
    expand = True
    )

more or less it comes out as I want but not as well as I wish

Could someone please explain this to me?
Thank you

@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 Apr 8, 2022
@MartinThoma
Copy link
Member

more or less it comes out as I want but not as well as I wish

What do you mean by that?

@MartinThoma MartinThoma added the needs-discussion The PR/issue needs more discussion before we can continue label Apr 22, 2022
@Atlasfreak
Copy link

I too have some problems understanding how excatcly this function works. When I am trying to center a landscape page which I rotated to portrait on a bigger page I am absolutley unable to do it as I have no idea what way I need to translate it. I tried to understand the translation matrix that is applied but I was unable to grasp the concept fully.
Additionally I noticed that when the rotation is positive and not 180 degrees you need to use the page height instead of page width but when the rotation is exactly 180 degrees you need to use page width and height with tx and ty respectively.

@josejachuf
Copy link
Author

josejachuf commented May 18, 2022

I do not know the origin of the PDF that you use, per do you make a recommendation, use the gs command to clean inconsistent info containing the PDF before using py-pdf2

gs -o output.pdf -sDEVICE=pdfwrite -dPDFSETTINGS=/screen input.pdf

@Atlasfreak
Copy link

Test.pdf
Just rotate it and then try to center it on a page that is bigger than A4 while it is possible it is not obvious as it differs from the way you do it with none rotated pages.
With rotated pages when you change tx or ty the page is moved diagonally and not along their respective axis except when it is rotated 180 degrees.
I think this should at least be documented somewhere so people who do not know how transformation matrices work (like me) know how it works and don't need to try arbitrarily until they find something that works.
Side note I found solution for my initial problem.

@MartinThoma
Copy link
Member

MartinThoma commented May 26, 2022

The rotation with mergeRotatedTranslatedPage is deprecated now. Please use the following pattern instead (which should give the same results):

from PyPDF2 import Transformation, PdfReader, PdfWriter

reader = PdfReader("box.pdf")
page = reader.pages[0]

x1, y1, x2, y2 = page.cropbox
assert x1 == 0
assert y1 == 0

op = Transformation().rotate(rotation=180).translate(tx=x2, ty=y2)
page.add_transformation(op)

print(page.cropbox)
print(page.mediabox)

writer = PdfWriter()
writer.add_page(page)
with open("output.pdf", "wb") as fp:
    writer.write(fp)

The center point of the coordinates is in the bottom-left corner. That means if you want to flip the image by 180 degrees, the content would go from the upper-right quadrant to the lower-left one.

You can see that those are just simple coordinate transformations. PyPDF2 does not care for this operation where the actual content is. It also doesn't care about the mediabox / cropbox / trimbox. It just applies the transformation to the coordinate system.

Did that clarify the topic?

@py-pdf py-pdf locked and limited conversation to collaborators May 26, 2022
@MartinThoma MartinThoma converted this issue into discussion #903 May 26, 2022

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
is-bug From a users perspective, this is a bug - a violation of the expected behavior with a compliant PDF needs-discussion The PR/issue needs more discussion before we can continue
Projects
None yet
Development

No branches or pull requests

3 participants