Search code examples
pdfprintinglandscapeportraitpdftk

Change orientation of landscape pages only in PDF


I have a PDF file exported from LibreOffice Writer. Here is an example. Some pages have portrait orientation, and others have landscape orientation. I am using Linux, and when I view the file in evince or Foxit Reader, it is shown correctly,i.e., all text lines are horizontal. It is also printed correctly on my printer on A4 paper: landscape pages are rotated 90 degrees counterclockwise so that text lines become vertical.

The problem is: the document is going to be printed on another device (in a publisher), and I was told all pages must have portrait orientation. I am not sure if this is already the case and evince simply shows the landscape pages rotated clockwise. For example, pdfinfo file.pdf prints Page rot: 0. If so, then everything is fine. If, however, the PDF file has a command saying that the following page is in landscape mode, then it is necessary to rotate it by 90 degrees and declare it portrait.

So is it possible to rotate only landscape pages from the command line? I know I can do it with pdftk, but then I need to specify page numbers manually. I have a lot of documents and manually specifying landscape page numbers is hard. Is there an automatic way?


Solution

  • It is possible with Python and module PyPDF2. See an example code:

    import PyPDF2
    
    pdf_in = open('test.pdf', 'rb')
    pdf_reader = PyPDF2.PdfFileReader(pdf_in)
    pdf_writer = PyPDF2.PdfFileWriter()
    
    numofpages = pdf_reader.numPages
    
    numrotated = 0 
    for pagenum in range(numofpages):
        page = pdf_reader.getPage(pagenum)
        mb = page.mediaBox
        if (mb.upperRight[0] > mb.upperRight[1]) and (page.get('/Rotate') is None):
            page.rotateCounterClockwise(90)
            numrotated = numrotated + 1
        pdf_writer.addPage(page)
    
    print str(numrotated) + " of " + str(numofpages) + " pages were rotated"
    pdf_out = open('test_rotated.pdf', 'wb')
    pdf_writer.write(pdf_out)
    pdf_out.close()
    
    pdf_in.close()
    

    Tested on Red Hat Enterprise Linux 6, Python 2.7.12, PyPDF2 1.26.0. Also works on Windows.