All posts

Rotate PDF via CLI

Ghostscript rotates PDF pages: Orientation is the number of 90° clockwise turns.

Open online converter

Packages to install

ghostscript is enough. -dAutoRotatePages=/None disables automatic rotation on write.

ghostscript
apt install
sudo apt-get update
sudo apt-get install -y ghostscript

How to run the command

Clockwise 90° is Orientation 1, 180° is 2, 270° or counterclockwise 90° is 3. Rotate page 2 left: gs ... -sPageList=2 -c "<</Orientation 3>> setpagedevice" -f input.pdf. Omit PageList to rotate the whole document.

gs
gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -dAutoRotatePages=/None -sPageList=2 -sOutputFile=rotated.pdf -c "<</Orientation 1>> setpagedevice" -f input.pdf

Dockerfile example

Build a minimal Ubuntu image with the required packages and run the conversion inside the container.

Dockerfile
FROM ubuntu:24.04

RUN apt-get update && \
    apt-get install -y --no-install-recommends \
        ghostscript && \
    rm -rf /var/lib/apt/lists/*

WORKDIR /work
docker build && docker run
docker build -t rotate-pdf .
docker run --rm -v "$PWD:/work" -w /work rotate-pdf \
  gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -dAutoRotatePages=/None -sPageList=2 -sOutputFile=rotated.pdf -c "<</Orientation 1>> setpagedevice" -f input.pdf

Example commands

Run these locally after installing the packages, or inside the container from the Dockerfile.

Example 1
gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -dAutoRotatePages=/None -sPageList=2 -sOutputFile=rotated.pdf -c "<</Orientation 1>> setpagedevice" -f input.pdf
Example 2
gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -dAutoRotatePages=/None -sPageList=2 -sOutputFile=rotated-ccw.pdf -c "<</Orientation 3>> setpagedevice" -f input.pdf