33

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/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 PDFPRINTER 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
    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
  • 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
33

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 this link.

  • 2
    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
  • +1 for sharing pdftk solution. pdflabs.com/docs/pdftk-man-page – Abhi Nov 29 '18 at 10:05
18

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

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

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.

  • 10
    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
  • 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
  • 11
    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
10

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

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

Install and run it. Open the desired pdf file, right click on the page, and rotate each one individually. It also collate pdfs and some other nice things.

sudo apt install pdfshuffler
  • 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
4

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.

  • Adding -density 300, 600 and even 1200 had only slight improvement on output quality. – JohnMudd May 10 '18 at 20:20
2

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.

1

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.

  • 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
0

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"
  • 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

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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