4

Using PDFtk Server, I want to rotate a PDF file 90˚ and save it in-place, to overwrite the input file. I tried the following, but it fails, probably because it starts writing before the file is finished reading.

pdftk in.pdf cat 1-endright output - > in.pdf
Elliott B
  • 200
  • 2
  • 9

2 Answers2

5

in your operation bash overwrite in.pdf file before call pdftk. You can't do it that way. I would recommend something like

pdftk in.pdf cat 1-endright output in-new.pdf && mv in-new.pdf in.pdf

This command also protect you when converting fail - file would not be overwriten.

undefine
  • 956
  • 8
  • 20
1

Use moreutils sponge for this:

pdftk in.pdf cat 1-endright output - | sponge in.pdf

One bug I've found is that pdftk doesn't always return non-zero on error, meaning that in.pdf may be wiped (sponge doesn't wipe if zero exit status).

Tom Hale
  • 1,005
  • 1
  • 12
  • 23
  • 1
    Just out of curiosity: do you know what mechanisms `sponge` uses to determine exit-code of a process piped into it? I'm a bit puzzled. – Sasha Dec 20 '20 at 20:24