skip to Main Content

I’m working with a bunch of PDF files, some of which have been scanned at a bit of an angle. Adobe Acrobat allows me to rotate PDF files by 90 or 180 degrees. But is there a way to rotate a PDF just a few degrees – just enough to make it straighter?

I could perhaps take a screenshot, open it in Photoshop and rotate it, then somehow convert the Photoshop file to a PDF. However, that seems like a really clumsy process.

7

Answers


  1. I had this at one time. I don’t know how many pages there are that you have.
    What I did is print the pages that wear off use a paper cutter to square them up and rescanned them. Hope this helps.
    And yes I’ve try to find some type of program to fix this and I still have not found one .

    Login or Signup to reply.
  2. PDF supports for complete pages only /Rotate values of 90 degrees, because that is (of course) simple. What you need to do is rotate the contents, not the page. So you need to use something which can remake the PDF file for you.

    You could use either Ghostscript or MuPDF to do this. Either will require some coding:

    • MuPDF will require coding in C,
    • Ghostscript will require you to do some PostScript programming.

    Using Ghostscript you would need to define a BeginPage procedure which rotates the content by a small amount and moves the origin of the content slightly as well (because the rotation rotates around the origin, which is at the bottom left, not the centre).

    Login or Signup to reply.
  3. Here is a short utility script for rotating pages (written in Perl). It converts each page of the input PDF to a PDF XObject Form, rotates the form, then outputs the rotated page.

    #! /usr/bin/perl                                                                                                                      
    use warnings; use strict;
    use PDF::API2;
    use Getopt::Long;
    
    my $degrees = 3;
    my $scale = 1.0;
    my $x = 0;
    my $y = 0;
    GetOptions ("rotate=i" => $degrees, "scale=f" => $scale, "x=f" => $x, "y=f" => $y)
        or die "usage: $0 IN_PDF OUT_PDF --rotate=DEG --scale=ALPHA --x=POINTS --y=POINTS";
    
    my $infile = shift (@ARGV);
    my $outfile = shift (@ARGV);
    
    my $pdf_in = PDF::API2->open($infile);
    my $pdf_out = PDF::API2->new;
    
    foreach my $pagenum (1 .. $pdf_in->pages) {
    
        my $page_in = $pdf_in->openpage($pagenum);
        #                                                                                                                                 
        # create a new page                                                                                                               
        #                                                                                                                                 
        my $page_out = $pdf_out->page(0);
    
        my @mbox = $page_in->get_mediabox;
        $page_out->mediabox(@mbox);
    
        my $xo = $pdf_out->importPageIntoForm($pdf_in, $pagenum);
        #                                                                                                                                 
        # lay up the input page in the output page                                                                                        
        # note that you can adjust the position and scale, if required                                                                    
        #                                                                                                                                 
        my $gfx = $page_out->gfx;
        $gfx->rotate($degrees);                                                                                                               
        $gfx->formimage($xo,  $x, $y, $scale);
    }
    
    $pdf_out->saveas($outfile);
    

    You’ll need to ensure the PDF::API2 and Geopt::Long modules are installed from CPAN.

    The script by default rotates 3 degrees anticlockwise, this is configurable vi the --rotate options.

    There are also -x, -y and --scale options to allow fine adjustments of the positioning and scale of the output pages.

    Login or Signup to reply.
  4. This question has also been asked on unix.stackexchange.com .

    Another option is using LaTeX:

    documentclass{standalone}
    usepackage{graphicx}
    
    begin{document}
    includegraphics[angle=-1.5]{odd-scan}
    end{document}
    

    In this case, I have the file odd-scan.pdf (a slightly rotated one page scan) in the same folder as the LaTeX file rotated.tex with the content above and then I run pdflatex rotated.tex. The output is a file rotated.pdf with the PDF rotated by 1.5 degrees clockwise.

    Login or Signup to reply.
  5. (I assume a *nix-style environment. On Windows, you can follow these instructions in Cygwin, although I think you might have to build MuPDF from source there as it doesn’t appear to be in the Cygwin repos. If you don’t want to do that and you’re okay with rasterizing the PDF, ImageMagick is in the Cygwin repos and can do the whole job if needed—see below.)

    MuPDF’s mutool utility can do this. Say you have a PDF file rotate_me.pdf and you want a version of it rotated by 20° clockwise written to a file rotated.pdf:

    #!/bin/bash
    
    mutool draw -R 20 -o rotated.pdf rotate_me.pdf
    

    (mutool draw docs)

    You can also rasterize the PDF using mutool convert, work with the image files, and then create a new PDF from them (this assumes rotate_me.pdf has between a hundred and a thousand pages—edit the %3d to your liking):

    #!/bin/bash
    
    # - for whatever reason convert's `rotate` is counter-clockwise
    # - %nd is replaced with the page number
    mutool convert -O rotate=-20 -o 'rotated_%3d.png' rotate_me.pdf
    

    (mutool convert docs)

    Once you’ve done whatever else you need to do the image files and you’re ready to turn them back into a PDF, you can use ImageMagick:

    #!/bin/bash
    
    magick convert $(ls | grep -P 'rotated_[0-9]{3}.png') rotated_finished.pdf
    

    (If you get an error saying the security policy for PDFs doesn’t permit this, you may need to edit /etc/ImageMagick-7/policy.xml and comment out or remove the <policy domain="coder" rights="none" pattern="PDF" /> line. Be aware of this Ghostscript pre-v9.24 vulnerability which that security policy may be intended to mitigate. If you’re working with files you made yourself, you should be safe here, but you may want to re-enable this policy afterwards depending on your needs and environment. If you’re not working with files you made yourself, especially PDFs, be careful, whether you have a pre-v9.24 Ghostscript installed or not. PDF as a format is very complex and offers many different places to squirrel away maliciousness, and practically speaking you can never be 100% confident that the software you’re using to work with it is perfectly hardened.)

    ImageMagick can also rasterize PDFs on its own, although it’s a bit more complicated. For example:

    #!/bin/bash
    
    magick convert -density 150 -rotate 20 rotate_me.pdf rotated.pdf
    

    This might look similar to the mutool draw command, but the difference is that ImageMagick will rasterize the input PDF and then use the resulting images to make the output PDF, so you can use all the regular ImageMagick transformations with this command.

    Anyway, -density is for DPI. It will default to 72 DPI if you don’t pass that argument, which is likely to not look very good. Also, ImageMagick doesn’t seem to be quite as smart as MuPDF about margins and things like that as far as PDFs go, so you may need to do more work with it than this to get reasonable output for your use case. If you do have access to both MuPDF and ImageMagick, I think doing the rasterization with MuPDF and then doing further work on the resulting images with ImageMagick tends to give the nicest results with the least work, but of course that may or may not be practical for you.

    (magick convert docs)

    Rasterization has obvious disadvantages if your PDF is vector-based—increased file size, fixed resolution, loss of flexibility, etc. Also, even if your PDF is already storing raster graphics, you may lose text data or the like from it in the conversion. If the PDF is really horrible, though, sometimes this is the least painful approach. You can OCR it if needed once you’ve cleaned it up using Tesseract, often with superior results to whatever may have been done before you arrived.

    Login or Signup to reply.
  6. This can be done with cpdf:

    cpdf -rotate-contents 5 in.pdf -o out.pdf

    (Rotates around the centre of the page by five degrees)

    Login or Signup to reply.
  7. On Linux (and maybe on Windows via cygwin?) unpaper has a content deskew function that uses edge detection to straighten content. The PDF has to be converted to an image file first, such as ppm e.g.:

    pdftoppm input.pdf > input.ppm
    

    NOTE: If the PDF is multi-page, the command above will generate one output file per page.

    Then use unpaper with the default settings:

    unpaper input.ppm output.ppm
    

    If the content is quite heavily skewed, then arguments like --deskew-scan-range may help, see the docs and man page.

    And lastly, convert back to PDF. Here is an example:

    pnmtops -imagewidth 8.5 -imageheight 11 ./output.ppm | 
      ps2pdf -dEPSCrop - > output.pdf
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search