105

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"
3
  • 5
    There are no "authorative solutions", but you should keep in mind what method the various solutions use: Direct manipulation of PDF structure (pdftk, Adobe Acrobat, and other programs), converting to an image and then converting back to a PDF (e.g. Imagemagick's convert, printing using PDF printer etc.). The latter is obviously a bad idea. – dirkt Sep 24 '17 at 5:38
  • 1
    How are those haphazard solution? How were you disappointed by each one of them? Did you actually try them? – simlev Sep 16 '18 at 1:31
  • 1
    I've found qpdf to work great and be super fast. I tested it on Ubuntu 20.04 too. See 1) this answer, and 2) my answer, with additional info. and examples. – Gabriel Staples Feb 16 at 23:31

10 Answers 10

96

Just 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:

6
  • 8
    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 8 '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
  • @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 9: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 9:29
  • 2
    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 7 '19 at 18:13
72

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
3
  • 9
    Looks like 90 goes counterclockwise and 270 goes clockwise - perhaps you should mention that. – Aaron Hall Dec 26 '18 at 18:33
  • 6
    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 7 '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
47

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.

6
  • 3
    Specifying the page-range seems to be compulsory, at least in my current qpdf version (8.0.2). – Xtreme Biker Mar 27 '19 at 9:58
  • 2
    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 8 '19 at 15:30
  • 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 4 '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 9:36
  • 1
    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 9 '20 at 17:27
26

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.

5
  • 16
    This will first convert the PDF text to an image, so size will increase and quality will drop (Unless it was already only a scan). – aviv Nov 8 '17 at 12:55
  • 1
    Yes indeed, that is a useful reminder, but in this case the OP states that he is scanning the documents. – NickD Nov 8 '17 at 21:46
  • 17
    Result pdf is very low quality. – Ivan Romanov Mar 4 '18 at 11:05
  • This method is good only for scanned originals: they are already bitmaps. For ordinary PDFs, see @aviv's comment above and maybe use the method in the other answer. – NickD Mar 4 '18 at 18:36
16

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

3
  • 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 7 '18 at 2:51
  • 1
    sudo snap install pdftk works just fine on Ubuntu 18.04. There is more usage info in this answer. – Acumenus May 26 '20 at 0:24
  • pdfshuffler has been renamed to pdfarranger, and it indeed is great – piegames Apr 14 at 19:14
9

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.

2
  • Adding -density 300, 600 and even 1200 had only slight improvement on output quality. – JohnMudd May 10 '18 at 20:20
  • As noted in the other answer suggesting convert, this is inherently a lossy operation. – piegames Apr 14 at 19:31
4

Late to this thread but wanted to post what I found.

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.

3

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 it does 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:

  1. 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

Related

  1. [my answer] How to extract PDF pages by page number or range of page numbers: https://askubuntu.com/questions/221962/how-can-i-extract-a-page-range-a-part-of-a-pdf/1318201#1318201
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.

1
  • sounds very promising! i also noticed the issue with changing the file size drastically that you refer to. – Trevor Boyd Smith Apr 17 '18 at 14:47
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"
3
  • This had zero effect. – Colin 't Hart Aug 16 '18 at 14:09
  • @Colin'tHart My command only restores the document to become upright down if it wasn't upright in the first place, to force the rotation use pdf90 or pdf180 or pdf270 instead, see this answer – SebMa Aug 17 '18 at 12:02
  • This worked for me. I guess it may work for some PDF documents not for others. – Simon Kissane May 25 '20 at 0:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.