1. Introduction

PDF files are widely used for document sharing and archiving. However, sometimes, it may be necessary to adjust the contents of pages for better readability or presentation. The Linux ecosystem has many command-line tools that provide efficient ways to manipulate PDF files for this purpose.

In this tutorial, we’ll explore how to rotate pages in a PDF file leveraging tools such as PDFtk, QPDF, and PDFjam, each with its unique approach.

2. Using PDFtk

PDFtk (PDF Toolkit) is a command-line tool for manipulating PDF documents. It enables us to merge, split, watermark, rotate, and even encrypt PDF files, among other operations.

2.1. Installation

We can install the pdftk command from the local package manager.

On Debian, we can use APT:

$ sudo apt install pdftk

Alternatively, on Arch Linux, we can use Pacman:

$ sudo pacman -S pdftk

Finally, on Fedora, we can leverage DNF:

$ sudo dnf install pdftk

After successful installation, let’s look at how to use the pdftk command to rotate pages.

2.2. Basic Document Rotation

To rotate pages in a PDF file using pdftk, we can use the cat operation and the north, south, east, and west options to specify the direction of rotation.

Let’s look at a basic example of rotating pages in a sample.pdf file using pdftk:

$ pdftk sample.pdf cat 1-endwest output output.pdf

We’re using 1-endwest option to rotate all pages 90 degrees anticlockwise (west). The output option specifies the name of the output PDF file.

Let’s look at some other examples of customizations:

  • endeast rotates the pages 90 degrees clockwise (east)
  • endsouth rotates the pages 180 degrees (south)
  • endnorth rotates the pages 180 degrees (north)

Naturally, we might want to only handle some pages.

2.3. Page Subset Rotation

In addition, we can also rotate specific pages and ignore the others. To do this, we must specify the cat operation’s page range.

For instance, let’s rotate the third page by 90 degrees clockwise:

$ pdftk sample.pdf cat 1-2 3east 4-end output output.pdf

Moreover, we can also rotate multiple pages in the PDF:

$ pdftk sample.pdf cat 1-3east 4-end output output.pdf

The command above rotates pages 1 to 3 90 degrees clockwise, and the rest retain their original orientation.

In addition, we can use keywords such as even and odd to rotate only the relevant pages:

$ pdftk sample.pdf cat eveneast output output.pdf

Critically, the command above removes all even pages and rotates all odd pages.

Finally, we can go further and rotate all odd pages to the west and all even pages to the east:

$ pdftk A=sample.pdf shuffle AoddWest AevenEast output output.pdf

In this case, we assign the sample.pdf input file to the variable A, and then we rotate all even pages 90 degrees clockwise, and all odd pages 90 degrees anticlockwise.

The shuffle option collates pages from input PDFs to create a new output PDF.

3. Using QDPF

QDPF is a PDF conversion tool that can perform various functions such as merging, splitting, rotation, linearization, encryption, conversion to different formats, and more. Furthermore, it also has several options for inspecting or checking PDF files, some of which are useful primarily to PDF developers and creators.

So, let’s first look at how to install QPDF on Linux. Below, we’ve covered how to install it in different Linux distros.

3.1. Installation

The qpdf command is usually available for installation from the local package manager.

On Ubuntu, we can use APT:

$ sudo apt install qpdf

On the other hand, on Arch Linux, we use Pacman:

$ sudo pacman -S qpdf

Finally, on Fedora, we usually leverage DNF:

$ sudo dnf install qpdf

At this point, we should have qpdf available as a command.

3.2. Basic Document Rotation

Next, let’s use the qpdf command to rotate pages in the input sample.pdf file.

As an illustration, we rotate all pages 90 degrees clockwise:

$ qpdf sample.pdf --rotate=90 -- output.pdf

We’re using the –rotate option to set the rotation angle.

Supported angles include 0, 90, 180, and 270 for clockwise rotations and their negative counterparts for anticlockwise rotations.

3.3. Page Subset Rotation

Moreover, we can use the –rotate option multiple times to rotate select pages in varying directions:

$ qpdf --rotate=90:2,4,6 --rotate=180:3,5 sample.pdf output.pdf

Here, we’re rotating pages 2, 4, and 6 90 degrees clockwise, and then, pages 3 and 5 180 degrees clockwise.

Finally, we can also rotate even and odd pages in different directions:

$ qpdf sample.pdf output_odd_even.pdf --rotate=90:1-z:even --rotate=-90:1-z:odd

Here, we’re rotating all even pages clockwise (90) and all odd pages anticlockwise (-90).

3.4. Encrypted Document Rotation

In case we’re dealing with an encrypted PDF file, we can use the qpdf command to first decrypt the file:

$ qpdf --decrypt --password=$PASSWORD sample.pdf output_no_pass.pdf

Then, we can rotate all pages in the output_no_pass.pdf file, 90 degrees anticlockwise:

$ qpdf output_no_pass.pdf --rotate=-90 -- output.pdf

We can modify the command to perform more advanced rotations.

4. Using PDFjam

PDFjam is a free and open-source tool that enables users to manipulate and handle PDF documents in various ways. It serves as a front-end to several tools in the TeX Live distribution, offering a user-friendly interface for regular PDF-related tasks.

Let’s find out how to install PDFjam on different Linux distros.

4.1. Installation

We can install the pdfjam command on Debian from the APT package manager:

$ sudo apt-get install texlive-extra-utils

Conversely, on Arch Linux, we employ Pacman:

$ sudo pacman -S texlive-pdfjam-7

Finally, On Fedora Linux, we can use DNF:

$ sudo dnf install texlive-pdfjam-7

Let’s use the pdfjam command to rotate pages in the sample.pdf file.

4.2. Basic Document Rotation

First, we leverage the pdfjam command to rotate all pages anticlockwise:

$ pdfjam sample.pdf --angle -90 -o output.pdf
          ----
  pdfjam: This is pdfjam version 3.03.
  pdfjam: Reading any site-wide or user-specific defaults...
          (none found)
  pdfjam: Effective call for this run of pdfjam:
          /usr/bin/pdfjam --angle '-90' --outfile output.pdf -- sample.pdf - 
  pdfjam: Calling /usr/bin/pdflatex...
  pdfjam: Finished.  Output was written to 'output.pdf'.

We’re using the –angle option to specify the angle of anticlockwise rotation and the –landscape option ensures that every page has a landscape orientation.

Then, the -o option specifies the name of our output PDF file.

With the pdfjam command, we can specify any angle of rotation, but it only supports landscape and portrait orientations. Notably, only the page contents rotate within the page orientation we specify.

Further, pdfjam version 3.03 is used in this example, and the commands might be slightly different in earlier versions of the PDFjam.

5. Conclusion

In this article, we’ve looked at several Linux command-line tools that can rotate pages within a PDF file.

In addition, we ran a few examples showing how to rotate specific pages in different directions. We also looked at how to first decrypt an encrypted PDF file, and then rotate the pages using the qpdf command.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments