8

Q: how do I rotate PDFs in pdf-tools?

Sometimes, I want to rotate a PDF page to toggle between portrait and landscape. This comes up in two common settings:

  1. An academic article has a wide table/figure typeset 90 degrees to portrait.
  2. I've scanned a book chapter with two book pages to a PDF page, and the two book pages are set side-by-side in landscape.

How do I rotate pages 90 degrees?

Dan
  • 32,584
  • 6
  • 98
  • 168

2 Answers2

4

As @kaushalmodi's post notes, rotation is apparently an open issue for pdf-tools, and it's apparently trickier to implement than expected. Comments on github indicate that a workaround is to use pdftk. Either I missed the code, or it wasn't posted.

So here's the code for a workaround until rotation gets implemented in pdf-tools. Two points:

  1. It requires pdftk to work.
  2. It overwrites the file, so be careful. I haven't used pdf-tools enough to know how it'll interact with the editing minor modes, so for now it's only going to work in pdf-view-mode.
(defun pdf-view--rotate (&optional counterclockwise-p page-p)
  "Rotate PDF 90 degrees.  Requires pdftk to work.\n
Clockwise rotation is the default; set COUNTERCLOCKWISE-P to
non-nil for the other direction.  Rotate the whole document by
default; set PAGE-P to non-nil to rotate only the current page.
\nWARNING: overwrites the original file, so be careful!"
  ;; error out when pdftk is not installed
  (if (null (executable-find "pdftk"))
      (error "Rotation requires pdftk")
    ;; only rotate in pdf-view-mode
    (when (eq major-mode 'pdf-view-mode)
      (let* ((rotate (if counterclockwise-p "left" "right"))
             (file   (format "\"%s\"" (pdf-view-buffer-file-name)))
             (page   (pdf-view-current-page))
             (pages  (cond ((not page-p)                        ; whole doc?
                            (format "1-end%s" rotate))
                           ((= page 1)                          ; first page?
                            (format "%d%s %d-end"
                                    page rotate (1+ page)))
                           ((= page (pdf-info-number-of-pages)) ; last page?
                            (format "1-%d %d%s"
                                    (1- page) page rotate))
                           (t                                   ; interior page?
                            (format "1-%d %d%s %d-end"
                                    (1- page) page rotate (1+ page))))))
        ;; empty string if it worked
        (if (string= "" (shell-command-to-string
                         (format (concat "pdftk %s cat %s "
                                         "output %s.NEW "
                                         "&& mv %s.NEW %s")
                                 file pages file file file)))
            (pdf-view-revert-buffer nil t)
          (error "Rotation error!"))))))

(defun pdf-view-rotate-clockwise (&optional arg)
  "Rotate PDF page 90 degrees clockwise.  With prefix ARG, rotate
entire document."
  (interactive "P")
  (pdf-view--rotate nil (not arg)))

(defun pdf-view-rotate-counterclockwise (&optional arg)
  "Rotate PDF page 90 degrees counterclockwise.  With prefix ARG,
rotate entire document."
  (interactive "P")
  (pdf-view--rotate :counterclockwise (not arg)))
Dan
  • 32,584
  • 6
  • 98
  • 168
2

It looks like this feature needs to be added to pdf-tools.

Someone has already posted an issue on this github requesting this enhancement:


From one comment, the workaround used by someone is to,

use pdftk to rotate the PDF and save it to another file.

Kaushal Modi
  • 25,203
  • 3
  • 74
  • 179
  • Thanks for the detective work! I must have missed the code for the `pdftk` workaround, so I wrote some up myself. – Dan Jul 22 '16 at 19:08