PDFTK Rotating Pages Problem
Asked Answered
N

4

43

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?

Nobility answered 28/6, 2010 at 22:28 Comment(0)
L
26

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).

Lindalindahl answered 29/6, 2010 at 0:38 Comment(5)
Some versions of pdftk require "south" etc. in stead of "S"Collbaith
man page is silent, no rotate by custom degree, like cclock 3deg?Fibro
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)Lindalindahl
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.Olnay
You can rotate the page contents by any angle with, for example cpdf -rotate-contents 3 in.pdf -o out.pdf.Empiricism
U
37

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

Update 08Oct2023

After reading various comments and having experienced with my own pdf, sometimes the relative rotation commands are better due to the pdf's own orientation metadata. for me south, west, east, didn't work as expected, so I have to use relative commands.

I will given an example of it below:

left to rotate left, right to rotate right, down to rotate. (Also short codes L, R, D did not work).

in below example, I am rotating page 11, 17-18, 24-26 to right, that is rotate the page 90dec clockwise. Similary you can use left and down.

PS D:\tmp> pdftk in.pdf cat 1-10 11right 12-16 17-18right 19-23 24-26right 27-end output out.pdf

Unification answered 3/9, 2018 at 8:24 Comment(1)
It would be more better if you could provide example to all the rotation commands.Rubbish
L
26

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).

Lindalindahl answered 29/6, 2010 at 0:38 Comment(5)
Some versions of pdftk require "south" etc. in stead of "S"Collbaith
man page is silent, no rotate by custom degree, like cclock 3deg?Fibro
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)Lindalindahl
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.Olnay
You can rotate the page contents by any angle with, for example cpdf -rotate-contents 3 in.pdf -o out.pdf.Empiricism
B
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.

Blame answered 21/6, 2023 at 14:25 Comment(0)
L
-1

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

Leto answered 10/5, 2023 at 13:27 Comment(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.Empiricism

© 2022 - 2024 — McMap. All rights reserved.