Search code examples
pythonpdfpypdfpdfminerpymupdf

How to flip a pdf page upside down using python?


I'm trying to flip pdf pages upside down using python. I have tried multiple libraries like PyPdf2, PyMuPDF and pdfminer. There is documentation on how to rotate a page, but that is not what I'm looking for. The closest solution I found was on one of PyMuPDF documentation pages, but its an example and doesn't offer the code as to how it was achieved. You can find the example page here: https://pymupdf.readthedocs.io/en/latest/matrix.html#flipping . Again the task is to flip the pdf pages not to rotate them.

Thank you, in advance for your answers.


Solution

  • The flipping is achieved by multiplying with a matrix that is initialized with those parameters:

    # Flip the page left-right (a = -1).
    destpage.showPDFpage(r * fitz.Matrix(a=-1), src, sourcepage.number)
    

    OR:

    # Flip the page up-down (d = -1).
    destpage.showPDFpage(r * fitz.Matrix(d=-1), src, sourcepage.number)