20

I want to rotate a 351K PDF named 08-file.pdf using CLI tools. I've tried imagemagick:

convert 08-file.pdf -rotate 90 08-file-rotated.pdf

But the original quality:

enter image description here

Suffered serious degradation:

enter image description here

I've tried adding the -density 300x300 argument, but the outcome was a 2.5M file, nearly one order of magnitude larger than the original, which is a huge waste.

Any idea how to losslessly rotate a PDF file using imagemagick?

Adam Matan
  • 128,757
  • 147
  • 397
  • 562
  • ImageMagick does not work directly on PDFs. A note on its "supported formats" page says it "uses ghostScript to convert the file"; presumably, to a simple bitmap, so it can works its magick on it. Then the result can be written to a variety of bitmap formats, of which PDF is just one of them. Use a tool specifically designed to work with PDFs. – Jongware Jul 09 '16 at 14:33
  • I would try Ghostscript directly. – Bonzo Jul 09 '16 at 15:42
  • Imagemagick and Ghostscript both rasterize the PDF. So if you rotate the PDF and try to write back to PDF, then the original PDF will be rasterize, rotated, then the raster result put into a vector PDF shell. This is not a good way. If you want to improve quality, then increase the density before reading the pdf and then resize appropriately. `convert -density 288 08-file.pdf -rotate 90 -resize 25% 08-file-rotated.pdf`. But I would suggest you look for pure PDF vector tools to do that. You did not post your original PDF, so we cannot test with it. – fmw42 Aug 15 '18 at 15:41

2 Answers2

16

I always had bad results in converting/altering pdf file with imagemagik/convert (bad resolution, or huge file). Playing with options -compress -density -quality was always frustrating and a waste of time (but i am no expert).

Proposal 1: pdftk

So I would recommend pdftk (you may need to install it via apt-get install)

Try :

pdftk  08-file.pdf cat 1-endright output 08-file-rotated.pdf

For old version of pdftk (v<3) rotation was indicated only by one letter: N: 0, E: 90, S: 180, W: 270, L: -90, R: +90, D: +180. The same command was:

pdftk  08-file.pdf cat 1-endR output 08-file-rotated.pdf

From another post on this site, I have a brief an explanation of the syntax

pdftk input.pdf cat 1-endsouth output output.pdf
#     \_______/     \___/\___/        \________/
#     input file    range  |          output file
#                         direction

You can see also https://linux.die.net/man/1/pdftk

Edit 2020:

Proposal 2: qpdf

I have found another alternative which is equivalent: qpdf, easier to remember and more powerful

see QPDF manual

#Syntax (you can rotate only some pages of the document -- see the manual --
qpdf --rotate=[+|-]angle[:page-range]

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

Other options worth to consider

pdfjam

PDF manipulation tools (CLI) To be considered if pdftk is not available on your system.

pdfjam looks quite similar to pdftk

pdfsam

This is a toolbox to modify pdf files with a GUI (graphical user interface).

Code is open source and multiplateform.

enter image description here

PiTheNumber
  • 22,828
  • 17
  • 107
  • 180
kFly
  • 340
  • 2
  • 11
  • `convert` does work and one can compress losslessly (which actually reduces the filesize depending on encoding). – jeffmcneill May 21 '21 at 17:04
4

Please use -compress lossless option:

convert -rotate 90 -compress lossless 08-file.pdf 08-file-rotated.pdf

From the documentation: https://www.imagemagick.org/script/command-line-options.php#compress

Lossless refers to lossless JPEG, which is only available if the JPEG library has been patched to support it.

Another option is to use the following command:

jhead -cmd "jpegtran -progressive -perfect -rotate 270 &i > &o" Image-0001.jpeg

It will write output to a temporary file and when it succeeds it will overwrite the original file:

Cmd:jpegtran -progressive -perfect -rotate 270 "Image-0001.jpeg" > "h1xQ6q"

Modified: Image-0001.jpeg

K.-Michael Aye
  • 5,465
  • 6
  • 44
  • 56
Jirka
  • 365
  • 2
  • 8
  • 6
    the convert with -compress lossless degrades the quality. I'll prefer to open the file in GIMP, rotate it, then Print it with "Print as File" option. This is the best work around I found. – ghosh'. Dec 06 '17 at 09:53
  • `-compress jpeg` worked for me. My issue was file size. – Victor Basso Mar 31 '21 at 20:28