Rotate every second page of a PDF file on Linux

A little bit special today, but I would like to show how to rotate every second page of every PDF file in a directory. This mostly happends if you scan a book for example and that looks something like this after scanning.

Scan

Install PDFtk

PDFtk is a small command line tool to manipulate pdf files. On Debian based operating systems you can install PDFtk with.

sudo apt-get install pdftk

Rotating the second page

With the following command you can rotate the second page of your PDF file. Please note, this command assumes that your PDF file has only 2 pages and is named scan1.pdf. It will create a file rotate.pdf in the same directory.

pdftk scan1.pdf cat 1 2-endsouth output rotate.pdf
As you may can see, the command is not only saying rotate just the second page. With the word cat followed by the pages, we say exactly which pages will be in the output.

On all PDF files in a directory

PDFtk can't save the output in the same file it has as input. So I assume you have a setup like this.

.
|-- rotated/
`-- scans/
    |-- scan1.pdf
    |-- scan2.pdf
    |-- scan3.pdf
    |-- scan4.pdf
    `-- scan5.pdf
Means, all scanned files are in the directory scans under our current directory for example and the output will be generated to the directory rotated. The following command will iterate over every file in the directory scans and put the modulfied version into the directory rotated.
(cd scans/ && for f in *.pdf ; do pdftk "$f" cat 1 2-endsouth output "../rotated/${f%}" ; done)
After that command you should see files scan1.pdf, ..., scan5.pdf in the directory rotated.
.
|-- rotated/
|   |-- scan1.pdf
|   |-- scan2.pdf
|   |-- scan3.pdf
|   |-- scan4.pdf
|   `-- scan5.pdf
`-- scans/
    |-- scan1.pdf
    |-- scan2.pdf
    |-- scan3.pdf
    |-- scan4.pdf
    `-- scan5.pdf

GUI

If you would like to have a program with a gui or user interface I would recommend PDF-Shuffler.

PDF-Shuffler

Installation under Debian

You can install PDF-Shuffler very easy under Debian with Synaptic or with the following command.

sudo apt-get install pdfshuffler
Next Previous