1
votes

I'm using Python/Django.
PyPDF2 to read my current pdf.

I want to read a pdf that I have saved and get the orientation of a single page within the pdf.

I'm expecting to be able to determine if the page is either landscape or portrait.

tempoutpdffilelocation =  settings.TEMPLATES_ROOT + nameOfFinalPdf
pageOrientation = pageToEdit.mediaBox
pdfOrientation = PdfFileReader(file(temppdffilelocation, "rb"))
# tempPdfOrientationPage = pdfOrientation.getPage(numberOfPageToEdit).mediaBox
print("existing pdf width: ")
# print(existing_pdf.getPage(numberOfPageToEdit).getWidth)
# print("get page size with rotation")
# print(tempPdfOrientationPage.getPageSizeWithRotation) 

existing_pdf = pdfOrientation.getPage(numberOfPageToEdit).mediaBox
# print(pageOrientation)
if pageOrientation.getUpperRight_x() - pageOrientation.getUpperLeft_x() > pageOrientation.getUpperRight_y() - pageOrientation.getLowerRight_y():
  print('Landscape')
  print(pageOrientation)
  # print(pdfOrientation.getWidth())
else:
  print('Portrait')
  print(pageOrientation)
  # print(pdfOrientation.getWidth())
# create a new PDF with Reportlab
can = canvas.Canvas(packet, pagesize=letter)

The last line setting the pagesize=letter what I want to determine based on my current pdf.
And here's my imports:

from PyPDF2 import PdfFileWriter, PdfFileReader
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter, landscape
import urllib

I've tried pyPdf .mediaBox but that always returns the same value of the expected file size, not the actual size. And pyPdf is outdated. As you can see I've also tried getWidth and withRotation.
I would think there's be an easy way for PyPDF2 PdfFileReader to determine the orientation of a selected object.

Any help is appreciated. Thanks.

4

4 Answers

4
votes

I used simply "/Rotate" attribute of the page:

OrientationDegrees = pdf.getPage(numberOfPageToEdit).get('/Rotate')

it can be 0, 90, 180, 270 or None

2
votes

This one works, fully tested:

import PyPDF2
from PyPDF2  import PdfFileReader

pdf = PdfFileReader(open('YourPDFname.pdf', 'rb'))
page = pdf.getPage(0).mediaBox

if page.getUpperRight_x() - page.getUpperLeft_x() > page.getUpperRight_y() - 
page.getLowerRight_y():
    print('Landscape')
else:
    print('Portrait')
2
votes

The rotate attribute will override the mediaBox settings. To account for this, check the page rotation before making final judgement. Note the text too can be rotated.

from PyPDF2 import PdfFileReader

pdf_path = 'yourPDFname.pdf'
pdf_reader = PdfFileReader(pdf_path)
deg = pdf_reader.getPage(0).get('/Rotate')    
page = pdf_reader.getPage(0).mediaBox
if page.getUpperRight_x() - page.getUpperLeft_x() > page.getUpperRight_y() -page.getLowerRight_y():
    if deg in [0,180,None]:
        print('Landscape')
    else:
        print('Portrait')
else:
    if deg in [0,180,None]:
        print('Portrait')
    else:
        print('Landscape')
-1
votes

You can detect it by using this code snippet:

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')