Search code examples
pythonpython-3.xpymupdf

rotate PDF 90 degrees relative to current rotation


I have rotated a PDF using fitz by 90 degrees using this code:

fitz_doc = fitz.open(origin, filetype="pdf")
fitz_doc_name = f"{fitz_doc.name}.pdf"

page = fitz_doc[int(0)]
page.setRotation(90)
fitz_doc.save(fitz_doc_name)
fitz_doc.close()

However, if I want to rotate the document again by another 90 degrees, I have to set page.setRotation to 180 and not 90. I suspect this has to do with how the 3x3 Matrix values are being manipulated but not sure if that is correct or how to manipulate the values directly.

How can I rotate a document relative to the current rotation so that if a document was previously rotated by 90 I only need to set rotation value to 90 and not 180 for the second rotation?


Solution

  • I'm not sure there's a direct way to do this. Instead, you get the current rotation value and then add to it how much more you want to rotate.

    more_rot = 90  # extra rotation desired
    current_rot = page.rotation
    page.setRotation(current_rot + more_rot)