Search code examples
pythonhtmlpdfweb-applications

PDF FILE ORIENTATION detection


I need a help to detect a orientation of a PDF file (Portrait or Landscape) by using Python. anyone has an idea?

I have been trying different libs but not successful yet.


Solution

  • Each page can be in a different orientation, but you can use PyPDF to detect the page size of the first page and determine the orientation accordingly:

    from PyPDF2  import PdfFileReader
    
    pdf = PdfFileReader(file('example.pdf'))
    page = pdf.getPage(0).mediaBox
    if page.getUpperRight_x() - page.getUpperLeft_x() > page.getUpperRight_y() - page.getLowerRight_y():
        print('Landscape')
    else:
        print('Portrait')