27

I was wondering in a pdf file, how to rotate pdf pages: 90 degree for even pages and -90 degree for odd pages?

By pdftk, all I know is to rotate a page by pdftk in.pdf cat 1W output out.pdf, and rotate all pages by pdftk in.pdf cat 1-endW output out.pdf.

Thanks!

  • FYI: for newer pdftk versions, syntax is: pdftk in.pdf cat 1-endwest output out.pdf, i.e. west instead of W or south instead of S... – colidyre Aug 22 at 22:57
11

This is easy since pdftk 1.44 which added the shuffle operation allowing different transformations on odd and even pages (amongst other uses).

If you have an older version of pdftk, you can use this Python script with the PyPdf library. (Warning, typed directly into the browser.)

#!/usr/bin/env python
import sys
from pyPdf import PdfFileWriter, PdfFileReader
input = PdfFileReader(sys.stdin)
output = PdfFileWriter()
for i in range(0,input.getNumPages()):
    output.addPage(input.getPage(i).rotateClockwise(90 if i%2==0 else -90))
output.write(sys.stdout)
30

With pdftk version 1.45 (6 December 2012) or later, use:

pdftk A=MyPdfFile.pdf shuffle AoddWest AevenEast output MyRotatedFile.pdf

If you have pdftk version 1.44 (thanks to Gilles for pointing that out!), you can use:

pdftk A=MyPdfFile.pdf shuffle AoddL AevenR output MyRotatedFile.pdf

Some similar example cases are described in man pdftk (at least in recent versions).

1

As an improvement to Gilles's answer:

#!/usr/bin/env python
import sys
from pyPdf import PdfFileWriter, PdfFileReader
input = PdfFileReader(open(sys.argv[1], "rb"))
output = PdfFileWriter()
for i in range(0,input.getNumPages()):
    output.addPage(input.getPage(i).rotateClockwise(90 if i%2==0 else -90))
output.write(open(sys.argv[2], "wb"))

Where the program accepts command line arguments of an input and output file: python script.py <inputFileName> <outputFileName>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.