39

I'm trying to use PDFTK to rotate pages in my PDF document. Executing something like the following should result in no changes to the page rotation:

pdftk in.pdf cat 1N output out.pdf

(This is rotating page 1 "north" or "0 degrees.")

In some PDF test documents, it works as expected (meaning, no changes to the page occurs). However, on some test documents, the PDF document is rotated 90 degrees. An additional 90 degrees is consistently applied to any page rotation I attempt to do. So, if I do this:

pdftk in.pdf cat 1E output out.pdf

(This is rotating page 1 "east" or "90 degrees.") The result is the page is rotated 180 degrees -- an additional 90 degrees!

The PDF looks OK when viewed in Acrobat Reader.

The only difference with these problem test PDF documents is that I used Acrobat Pro to already change their rotation. When applying PDFTK page roation on these already rotated PDF documents, I run into this problem.

Any idea what's going on?

StackOverflowNewbie
  • 39,403
  • 111
  • 277
  • 441

4 Answers4

34

To rotate page 1 by 90 degrees clockwise:

pdftk in.pdf cat 1E output out.pdf    # old pdftk
pdftk in.pdf cat 1east output out.pdf # new pdftk

To rotate all pages clockwise:

pdftk in.pdf cat 1-endE output out.pdf    # old pdftk
pdftk in.pdf cat 1-endeast output out.pdf # new pdftk

Similarly, to rotate all pages anti-clockwise:

 pdftk in.pdf cat 1-endwest output out.pdf
Ricky Robinson
  • 21,798
  • 42
  • 129
  • 185
24

When you use the "normal" rotation parameters (N, E, S, W), you are setting the rotation flag on the PDF pages to your parameter (e.g. 90 degrees). This does not take into account the current rotation setting. Here is the paragraph from the pdftk documentation about rotation:

The page rotation setting can cause pdftk to rotate pages and documents. Each option sets the page rotation as follows (in degrees): N: 0, E: 90, S: 180, W: 270, L: -90, R: +90, D: +180. L, R, and D make relative adjustments to a page's rotation.

In addition to the NESW rotation settings, you also have the L, R and D options, that allow you to make relative adjustments that take the current rotation flag into account.

If that does not solve your problem, I would need access to a couple of test documents (one that does work correctly, and one that results in the wrong rotation setting).

D. A.
  • 33
  • 6
khkremer
  • 594
  • 3
  • 4
  • 6
    Some versions of pdftk require "south" etc. in stead of "S" – LeRookie Oct 04 '14 at 18:17
  • man page is silent, no rotate by custom degree, like cclock 3deg? – droid192 Jun 20 '19 at 19:06
  • The man page is silent because you cannot do that: The page content in a PDF file does not actually get rotated when you select to rotate. All that’s done is that a flag gets added that tells the PDF viewer (or printer) to render the page at a certain rotation angle. This flag can have four states: 0, 90, 180 and 270 degrees. There is no support for other angles. With appropriate software, you can of course rotate every page object by e.g. 6 degrees, but that is not something supported by pdftk (or Adobe Acrobat) – khkremer Jun 21 '19 at 20:09
  • pdftk is awesome :) Alright, a less convenient but possible solution with a genuine rotate is possible with Inkscape: 1) open file, 2) group all objects, 3) rotate by whatever angle you like, 4) if needed adjust document properties to auto-resize page size, 5) save. Sure, not practical for multi-page operations. – Mario Apr 08 '21 at 00:27
  • You can rotate the page contents by any angle with, for example `cpdf -rotate-contents 3 in.pdf -o out.pdf`. – johnwhitington May 10 '23 at 15:07
0

The most upvoted answer did not work in my situation.

I had to use the following to rotate right 90 degrees:

    pdf2tk input.pdf cat 1right output outrot90.pdf

When I used 1-endeast, the pdf rotated 180 degrees just as the original poster stated. I suspect it is due to some other metadata stored in the pdf files.

I am using Pdftk 2.02

  • 1
    Each page in a PDF can have a "soft" rotation set, as you suggest, in a kind of metadata. It looks like pdftk has "left, right and down" for relative changes, and "north, south, east and west" for absolute replacements. Cpdf separates the two also using `cpdf -rotate 90 in.pdf -o out.pdf` and `cpdf -rotate-by 90 in.pdf -o out.pdf` for example. You can use `cpdf -page-info` to show the existing rotations . The upright function `cpdf -upright in.pdf -o out.pdf` sets the rotation to 0, counter-rotating the page dimensions and content to compensate - a sort of normalization. – johnwhitington May 10 '23 at 15:11
0

When pdftk is no longer available in your distribution, or it's selective-page rotation (as opposed to whole-document rotation) doesn't work for you either, qpdf is a good, fast, replacement.
It has good documentation on the web and as pdf.

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.

I'm not affiliated with the qpdf project, but I'm glad I found a good and really fast pdftk alternative.

nyov
  • 1,382
  • 7
  • 23