cannot rotate pages of some PDF #1378
-
Problem description. Solution I'd like Alternatives I've considered |
Beta Was this translation helpful? Give feedback.
Replies: 8 comments 4 replies
-
I don't understand: |
Beta Was this translation helpful? Give feedback.
-
Look at this: >>> for degree in (0, 90, 180, 270):
page.set_rotation(degree)
print("de-rot:", page.derotation_matrix)
print(" rot:", page.rotation_matrix)
de-rot: Matrix(1.0, -0.0, -0.0, 1.0, 0.0, 0.0)
rot: Matrix(1.0, 0.0, 0.0, 1.0, 0.0, 0.0)
de-rot: Matrix(0.0, -1.0, 1.0, 0.0, -0.0, 842.0)
rot: Matrix(0.0, 1.0, -1.0, 0.0, 842.0, 0.0)
de-rot: Matrix(-1.0, -0.0, -0.0, -1.0, 595.0, 842.0)
rot: Matrix(-1.0, 0.0, 0.0, -1.0, 595.0, 842.0)
de-rot: Matrix(0.0, 1.0, -1.0, 0.0, 595.0, -0.0)
rot: Matrix(0.0, -1.0, 1.0, 0.0, 0.0, 595.0)
>>> |
Beta Was this translation helpful? Give feedback.
-
I'll check my PDF again. thanks |
Beta Was this translation helpful? Give feedback.
-
Thanks to your advice, I've understood it's not related to rotation matrix. I'm doing a kind of 2-ups and it works for some pdf and does not work for some others. it looks working if i simply overwrite src_pdf. are there any solutions you come up with? a3_w, a3_h = fitz.paper_size("a3-l")
rect_a3_left = fitz.Rect(0, 0, a3_w/2, a3_h)
rect_a3_right = fitz.Rect(a3_w/2, 0, a3_w, a3_h)
a4_w, a4_h = fitz.paper_size("a4")
rect_a4_portrait = fitz.Rect(0, 0, a4_w, a4_h)
rect_a4_landscape = fitz.Rect(0, 0, a4_h, a4_w)
page = new_pdf.new_page(-1, width = a3_w, height = a3_h)
if paper_size == 'a4_landscape':
print(paper_size_of(src_pdf[i]))
print(src_pdf[i].rotation)
print(src_pdf[i].rotation_matrix)
print(src_pdf[i].derotation_matrix)
if src_pdf[i].rotation == 0:
src_pdf[i].set_rotation(270)
else:
src_pdf[i].set_rotation(src_pdf[i].rotation - 90)
print(paper_size_of(src_pdf[i]))
print(src_pdf[i].rotation)
print(src_pdf[i].rotation_matrix)
print(src_pdf[i].derotation_matrix)
page.show_pdf_page(rect_a3_left, src_pdf, i) |
Beta Was this translation helpful? Give feedback.
-
The simplest way is probably this: set the source page's rotation to 0 before inserting it. src = fitz.open("source.pdf")
rect = fitz.paper_rect("a3-l")
w, h = fitz.paper_size("a3-l")
rl = fitz.paper_rect("a4") # equal left side of A3 landscape
rr = rl + (rl.width, 0, rl.width, 0) # the right side
doc = fitz.open()
for srcpage in src:
srcpage.set_rotation(0)
if not srcpage.number % 2: # make new output page on even input number
page = doc.new_page(width=w, height=h)
page.show_pdf_page(rl, src, srcpage.number)
else:
page.show_pdf_page(rr, src, srcpage.number) |
Beta Was this translation helpful? Give feedback.
-
If a page is landscape, then |
Beta Was this translation helpful? Give feedback.
-
Please send me that file - we are talking past each other. |
Beta Was this translation helpful? Give feedback.
-
Here is the script that does work for both those files. import fitz
doc1 = fitz.open("syousa2.pdf")
doc2 = fitz.open("syousa_2up.pdf")
newpdf = fitz.open()
w, h = fitz.paper_size("a3-l")
# left and right A4 rectangles:
rl = fitz.paper_rect("a4")
rr = rl + (rl.width, 0, rl.width, 0)
for page in doc1: # loop through source pages
if not page.number % 2: # on even pages create the output A3 page
rect = rl
newpage = newpdf.new_page(width=w, height=h)
else:
rect = rr
if page.rotation == 0 and page.rect.width > page.rect.height:
rot = 90
else:
rot = 0
newpage.show_pdf_page(rect, doc1, page.number, rotate=rot)
newpdf.ez_save(doc1.name + "-up.pdf")
newpdf.close()
newpdf = fitz.open()
for page in doc2:
if not page.number % 2:
rect = rl
newpage = newpdf.new_page(width=w, height=h)
else:
rect = rr
if page.rotation == 0 and page.rect.width > page.rect.height:
rot = 90
else:
rot = 0
newpage.show_pdf_page(rect, doc2, page.number, rotate=rot)
newpdf.ez_save(doc2.name + "-up.pdf") |
Beta Was this translation helpful? Give feedback.
Here is the script that does work for both those files.
Explanation:
The "rotate" argument in
show_pdf_page
can only work in a foreseeable way, if the source page is not rotated. If it is rotated, use my code snippet from a previous post.