Search code examples
pdfimagemagickimage-rotationlossless

Lossless rotation of PDF files with imagemagick


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?


Solution

  • 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