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.