All posts
Rotate PDF via CLI
Ghostscript rotates PDF pages: Orientation is the number of 90° clockwise turns.
Open online converterPackages to install
ghostscript is enough. -dAutoRotatePages=/None disables automatic rotation on write.
ghostscript
sudo apt-get update
sudo apt-get install -y ghostscriptHow 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 -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -dAutoRotatePages=/None -sPageList=2 -sOutputFile=rotated.pdf -c "<</Orientation 1>> setpagedevice" -f input.pdfDockerfile example
Build a minimal Ubuntu image with the required packages and run the conversion inside the container.
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 -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.pdfExample commands
Run these locally after installing the packages, or inside the container from the Dockerfile.
gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -dAutoRotatePages=/None -sPageList=2 -sOutputFile=rotated.pdf -c "<</Orientation 1>> setpagedevice" -f input.pdfgs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -dAutoRotatePages=/None -sPageList=2 -sOutputFile=rotated-ccw.pdf -c "<</Orientation 3>> setpagedevice" -f input.pdf