35

In a PDF file, how to rotate pages 90 degrees for even pages and -90 degrees for odd pages?

With PDFtk, all I know is to rotate a single page:

pdftk in.pdf cat 1W output out.pdf

And rotate all pages:

pdftk in.pdf cat 1-endW output out.pdf
0

4 Answers 4

39

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
  • 1
    rotate even pages by 180 degrees: pdftk A=infile.pdf shuffle Aoddnorth Aevensouth output outfile.pdf (source)
    – milahu
    Aug 4, 2022 at 18:12
14

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)
0
2

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>

1

With QPDF

qpdf in.pdf out.pdf --rotate=+90:1-z:even --rotate=-90:1-z:odd

See the documentation for more.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .