186

When I scan documents that are landscape-oriented, the output PDF files are portrait and so all the PDF viewers display the scanned documents in portrait.

From the command line, how do you rotate a PDF file 90 degrees?

I tried searching and found a bunch of solutions but I had trouble finding what looked like an authoritative solution[1] that uses a stable and robust Linux/Unix tool.


footnote [1]

For example, here is a sampling of some of the haphazard solutions I found:

  • "just use Adobe Acrobat Pro to rotate the file and then save the file"
  • "use pdfjam"
  • "use PDFtk"
  • "use ${PROGRAM_NAME} from Poppler"
  • "use ImageMagick's convert" -- but then all the comments were very negative and stating "the image quality is ruined"
  • "open the file in a PDF viewer, then rotate, then print using a PDF printer like cutePDF or PDF printer or etc"
  • "use ${PROGRAM_NAME}", then I searched for "${PROGRAM_NAME}" and there is something about "Fedora removed ${PROGRAM_NAME} because of licensing issues"

10 Answers10

162

Use PDFtk.

  1. For rotating clockwise:

     pdftk input.pdf cat 1-endeast output output.pdf
    
  2. For rotating anti-clockwise:

     pdftk input.pdf cat 1-endwest output output.pdf
    

Regarding the installation of PDFtk on Fedora, I found these links:

Lesmana
  • 27,439
user163859
  • 1,737
  • 16
    The man page of pdftk states more options for rotating: The page rotation setting can cause pdftk to rotate pages and documents. Each option sets the page rotation as follows (in degrees): north: 0, east: 90, south: 180, west: 270, left: -90, right: +90, down: +180. left, right, and down make relative adjustments to a page's rotation. – Tapper May 08 '18 at 20:21
  • Output is rotated but still scaled small enough to fit the original orientation. Not helpful. – JohnMudd May 10 '18 at 20:13
  • 1
    @JohnMudd What do you mean by "scaled small enough to fit the original orientation" ? I suppose you now have a problem with the PDF viewer, not with the rotation of pages. – user163859 May 12 '18 at 09:40
  • My original PDF was a letter size page rotated 90 degrees so page height was reduced from 11" to 8.5". Didn't change after correcting rotation. Found this to be true with many online tools. Might not be a problem with conversion after all. I found a scaling option while viewing/printing rotated PDF and that fixed it. Sorry, I was so frustrated at that point that I just printed and moved on and didn't note the exact steps I took. – JohnMudd May 14 '18 at 09:29
  • +1 for sharing pdftk solution. https://www.pdflabs.com/docs/pdftk-man-page/ – Abhi Nov 29 '18 at 10:05
  • 4
    pdftk is removed in Ubuntu 18.04 and above. Ubuntu itself suggests to install a snap, which works only in simple situations (it did not find my files, perhaps because they were outside of my home directory ?). I moved to pdfjam ... --angle 270.... – Stéphane Gourichon Jan 07 '19 at 18:13
  • pdftk still available in CentOS7.9, nice quality. – karsten May 22 '23 at 09:03
  • For rotating 180 degrees: pdftk input.pdf 1-endsouth output output.pdf – karsten May 22 '23 at 09:04
  • Also works under Windows, using WSL. Move to the right folder and do your thing: cd /mnt/c/Users/John/Downloads/ – SPRBRN Jan 29 '24 at 13:20
102

I just stumbled upon this thread and saw that there is no good solution mentioned yet. I found that (at least on Debian and Ubuntu) pdfjam comes with the following commands:

pdf90 input.pdf
pdf180 input.pdf
pdf270 input.pdf

I think that is the easiest and fastest approach. These are scripts using the pdfjam command properly. Btw. the pdfjam is a virtual package that comes with texlive-extra-utils

UPDATE

The above commands were dropped from pdfjam since version 3.02, and are now available (but unmaintained) in the package pdfjam-extras. The direct commands that work with pdfjam (as mentioned in the comments) are respectively:

pdfjam --landscape --angle 90 input.pdf
pdfjam --angle 180 input.pdf
pdfjam --landscape --angle 270 input.pdf
derschueddi
  • 1,161
  • 12
    Looks like 90 goes counterclockwise and 270 goes clockwise - perhaps you should mention that. – Aaron Hall Dec 26 '18 at 18:33
  • 9
    pdf90 is indeed a thin wrapper. Yet it forces a suffix convention. So I used pdfjam directly: pdfjam --outfile myoutputfile.pdf --angle 270 --fitpaper true --rotateoversize true myinputfile.pdf – Stéphane Gourichon Jan 07 '19 at 18:15
  • 1
    you can also change the suffix using the argument --suffix desired_suffix. Default is : --suffix rotated90 so the output is : initial_name-rotated90.pdf – jeannej Apr 27 '20 at 17:45
  • 1
    To rotate 90° clockwise: pdfjam --landscape --angle -90 input.pdf. Mind the - in -90. – Diego Oct 13 '21 at 11:09
89

In Linux Mint 18.3 (I believe in other Debian derived distributions as well) you have a simple command line tool named qpdf.

You can use: qpdf in.pdf out.pdf --rotate=[+|-]angle[:page-range].

From the documentation:

--rotate=[+|-]angle[:page-range]

Apply rotation to specified pages. The page-range portion of the option value has the same format as page ranges in Section 3.5, “Page Selection Options”. If the page range is omitted, the rotation is applied to all pages. The angle portion of the parameter may be either 90, 180, or 270. If preceded by + or -, the angle is added to or subtracted from the specified pages' original rotations. Otherwise the pages' rotations are set to the exact value. For example, the command qpdf in.pdf out.pdf --rotate=+90:2,4,6 --rotate=180:7-8 would rotate pages 2, 4, and 6 90 degrees clockwise from their original rotation and force the rotation of pages 7 through 9 to 180 degrees regardless of their original rotation, and the command qpdf in.pdf out.pdf --rotate=180 would rotate all pages by 180 degrees.

In your case, to rotate all pages in a PDF 90 degrees clockwise, you can do:

qpdf --rotate=+90 in.pdf out.pdf
Royi
  • 1,033
  • 4
    Specifying the page-range seems to be compulsory, at least in my current qpdf version (8.0.2). – Aritz Mar 27 '19 at 09:58
  • 5
    It seems at least not to be the case anymore (9.0.2): qpdf in.pdf out.pdf --rotate==-90 works well for all pages. – iNyar Nov 08 '19 at 15:30
  • 2
    The only solution that worked for me. All others I tried didn't work. pdftk didn't produce any output. pdf90 and friends would require 192MB of space for all texlive-extra-utils stuff. I only had to tweak the command line a bit. As stated above, page is mandatory. I used this: qpdf input.pdf output.pdf --rotate=90:1 – Alexandre Schmidt May 04 '20 at 18:07
  • 1
    Page doesn't seem to be mandatory to me (8.4.2), but I had to put the parameter before input and output: qpdf --rotate=90 input.pdf output.pdf. In any case, that's easy to figure out and the package itself is only a few hundred kilobytes, so it's the best option IMO. – Ale Jun 10 '20 at 09:36
  • 2
    qpdf seems great. It can rotate pages as said in the answer, it can also concat files using the following syntax: qpdf --collate --empty --pages a.pdf 1-3 back.pdf 1-4 -- c.pdf. It has no dependency, can be installed using apt install qpdf and is really light (takes ~2KB according to apt). – tleb Dec 09 '20 at 17:27
  • I just added an answer to provide some more information about how to install and use qpdf for rotating: https://unix.stackexchange.com/questions/394065/command-line-how-do-you-rotate-a-pdf-file-90-degrees/634882#634882. – Gabriel Staples Feb 16 '21 at 23:26
  • The true answer in 2023 – sgt-hartman May 14 '23 at 18:18
31

You can use ImageMagick: display or convert - e.g. to rotate it clockwise use

convert -rotate 90 <file>.pdf <rotated-file>.pdf

Use -90 for a counterclockwise rotation.

N.B. Only use this method when the original image is a bitmap (e.g. produced by scanning). If the original image is not a bitmap, this method will convert it to one and quality will suffer. In the latter case, please use one of the methods in the other answers.

NickD
  • 2,926
24

qpdf works great and is super fast!:

Tested on Ubuntu 20.04 with qpdf version 8.0.2 (see qpdf --version), but I suspect it would work fine on Ubuntu 18.04, 16.04, and 14.04 too, since I've used qpdf in other ways on all of those systems as well.

sudo apt update
sudo apt install qpdf

Rotate in.pdf +90 deg and save as out.pdf

qpdf --rotate=+90 in.pdf out.pdf

Rotate in.pdf -90 deg and save as out.pdf

qpdf --rotate=-90 in.pdf out.pdf

Rotate in.pdf +/-180 deg and save as out.pdf

qpdf --rotate=+180 in.pdf out.pdf

The manual pages (man qpdf) show very little, but what they do show is very important. Here's a key part right at the end of it (emphasis added):

For a summary of qpdf's options, please run qpdf --help. A complete manual can be found in /usr/share/doc/qpdf/qpdf-manual.html or /usr/share/doc/qpdf/qpdf-manual.pdf.

So, read the full manual which is stored locally on your computer! Here are the links. Copy and paste them into your browser's address bar to open them. You can also view the manual online here: https://qpdf.readthedocs.io/.

  1. Local QPDF manual location on Linux:
    1. QPDF User manual in PDF form: file:///usr/share/doc/qpdf/qpdf-manual.pdf
    2. QPDF User manual in HTML form: file:///usr/share/doc/qpdf/qpdf-manual.html

To see the help options, run qpdf --help instead. Or, even better, pipe to less:

qpdf --help | less -RFX

Then you can press / and search for --rotate. Here's a snippet from the help pages:

--rotate=[+|-]angle[:page-range]
                        rotate each specified page 90, 180, or 270 degrees;
                        rotate all pages if no page range is given

and:

The --rotate flag can be used to specify pages to rotate pages either 90, 180, or 270 degrees. The page range is specified in the same format as with the --pages option, described below. Repeat the option to rotate multiple groups of pages. If the angle is preceded by + or -, it is added to or subtracted from the original rotation. Otherwise, the rotation angle is set explicitly to the given value.

References:

  1. qpdf --help | less -RFX
  2. This answer by @Royi
  3. QPDF on GitHub
  4. Manual of QPDF (more extensive than man page)

Related

  1. My answer: How to extract PDF pages by page number or range of page numbers
19

pdftk is no longer available on Ubuntu 18.04 due to some deprecated dependencies.

The best solution I found was the (graphical) tool pdfshuffler pdfarranger*.

Install (see the installation steps in https://github.com/pdfarranger/pdfarranger) and run it. Open the desired PDF file, select the files to rotate and then click the rotate button in the header bar (or use the right click menu). It can also concatenate PDFs and some other nice things.

* pdfarranger is a fork of the abandoned (?) pdfshuffler

αғsнιη
  • 41,407
tronic
  • 319
  • 1
    Do you have to rotate each page manually? That's a deal breaker. I need to rotate a hundreds-page-long PDF book to view it better (larger) on a Kindle. UPDATE: Just tried it, nope, you can Ctrl-A to select all pages then rotate them all in one step. – Gerry Lufwansa Aug 07 '18 at 02:51
  • 1
    sudo snap install pdftk works just fine on Ubuntu 18.04. There is more usage info in this answer. – Asclepius May 26 '20 at 00:24
  • 1
    pdfshuffler has been renamed to pdfarranger, and it indeed is great – piegames Apr 14 '21 at 19:14
  • pdftk is back in the default Ubuntu repositories in Ubuntu 20.04 and later. – karel Dec 15 '23 at 05:49
8

To improve output resolution (default is 72 DPI), I got good results with:

convert -rotate -90 -density 200 input.pdf output.pdf

This was for a .pdf of a scanned document. I found that -density 300 reduced quality somewhat versus -density 200.

muru
  • 72,889
Greg Ben
  • 107
4

I needed to print a large PDF file (200+ pages) as a grouping of signatures for bookbinding.

  • I used pdfjam to make the signature sets:
    pdfjam --landscape --signature 16 filename.pdf
    
  • I opened the new pdfjam file in a viewer and printed odd pages.
  • I put the stack of printed pages back into the printer flipped around, then printed even pages.

I now have a perfect set of signatures for binding. pdfjam flips the odd pages upside down and would need to be rotated 180 if you use the duplexer on your printer. Flip the pages manually after printing the odds and there is no need to rotate them electronically. I also found pdfsam to be a very useful tool for batching edits to PDF files.

Just hoping this manual flip might save someone some time.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
2

Although it does have its limitations, I quite often use PDFedit, especially for the rotation of PDF scans. While ImageMagick's convert (using rather high -density values) achieves quite a good quality, it also bloats the file (original: 155 kiB, 180° rotated copy: 1.2 MiB). PDFedit rotates the same image with unchanged quality without noticably changing the file size.

2

I use this command to automatically rotate pdf files to become upright down if they aren't upright in the first place :

gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -dAutoRotatePages=/All -sOutputFile="$outputFile" "$file"
SebMa
  • 2,149