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
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityIn 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
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).
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)
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>