192

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"
4
  • 7
    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
    Commented Sep 24, 2017 at 5:38
  • 4
    How are those haphazard solution? How were you disappointed by each one of them? Did you actually try them?
    – simlev
    Commented Sep 16, 2018 at 1:31
  • 2
    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. Commented Feb 16, 2021 at 23:31
  • 1
    Best answer.
    – mcp
    Commented Feb 4, 2022 at 22:44

10 Answers 10

168

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:

8
  • 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
    Commented May 8, 2018 at 20:21
  • Output is rotated but still scaled small enough to fit the original orientation. Not helpful.
    – JohnMudd
    Commented May 10, 2018 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
    Commented May 12, 2018 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
    Commented May 14, 2018 at 9:29
  • 5
    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.... Commented Jan 7, 2019 at 18:13
106

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 --outfile outfile.pdf
pdfjam --angle 180 input.pdf --outfile outfile.pdf
pdfjam --landscape --angle 270 input.pdf --outfile outfile.pdf
5
  • 12
    Looks like 90 goes counterclockwise and 270 goes clockwise - perhaps you should mention that.
    – Aaron Hall
    Commented Dec 26, 2018 at 18:33
  • 10
    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 Commented Jan 7, 2019 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
    Commented Apr 27, 2020 at 17:45
  • 2
    To rotate 90° clockwise: pdfjam --landscape --angle -90 input.pdf. Mind the - in -90.
    – Diego
    Commented Oct 13, 2021 at 11:09
  • I think an --outfile outfile.pdf is needed today for pdfjam (2024, debian trixie), feel free to revert, if you have different experience.
    – peterh
    Commented Jun 27 at 22:02
90

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
7
  • 4
    Specifying the page-range seems to be compulsory, at least in my current qpdf version (8.0.2).
    – Aritz
    Commented Mar 27, 2019 at 9: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
    Commented Nov 8, 2019 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 Commented May 4, 2020 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
    Commented Jun 10, 2020 at 9: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
    Commented Dec 9, 2020 at 17:27
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.

6
  • 20
    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
    Commented Nov 8, 2017 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
    Commented Nov 8, 2017 at 21:46
  • 18
    Result pdf is very low quality. Commented Mar 4, 2018 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
    Commented Mar 4, 2018 at 18:36
  • 1
    @IvanRomanov Adding -quality 100 removes some of the noticeable noise
    – Pablo A
    Commented Sep 10, 2019 at 1:18
26

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
5
  • 1
    And has proper cmdline syntax: --rotate not -rotate.
    – mcp
    Commented Feb 4, 2022 at 22:43
  • What's more: --rotate=90 not -rotate 90.
    – mcp
    Commented Feb 15, 2022 at 17:49
  • To rotate a single file in place: qpdf --rotate=90 --replace-input in.pdf.
    – mcp
    Commented Feb 15, 2022 at 17:50
  • 1
    Best answer out of all the solutions. Some of the other packages are 200MB to install!
    – WalksB
    Commented Oct 10, 2022 at 6:17
  • 1
    I also liked it very much.
    – Lv2eof
    Commented Nov 12, 2022 at 22:31
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

4
  • 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. Commented Aug 7, 2018 at 2:51
  • 1
    sudo snap install pdftk works just fine on Ubuntu 18.04. There is more usage info in this answer.
    – Asclepius
    Commented May 26, 2020 at 0:24
  • 1
    pdfshuffler has been renamed to pdfarranger, and it indeed is great
    – piegames
    Commented Apr 14, 2021 at 19:14
  • pdftk is back in the default Ubuntu repositories in Ubuntu 20.04 and later.
    – karel
    Commented Dec 15, 2023 at 5: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.

2
  • Adding -density 300, 600 and even 1200 had only slight improvement on output quality.
    – JohnMudd
    Commented May 10, 2018 at 20:20
  • 1
    As noted in the other answer suggesting convert, this is inherently a lossy operation.
    – piegames
    Commented Apr 14, 2021 at 19:31
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.

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. Commented Apr 17, 2018 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
  • 1
    This had zero effect. Commented Aug 16, 2018 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
    Commented Aug 17, 2018 at 12:02
  • This worked for me. I guess it may work for some PDF documents not for others. Commented May 25, 2020 at 0:43

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .