All posts
Split PDF via CLI
Extract pages with Ghostscript -sPageList.
Open online converterPackages to install
The ghostscript package provides gs. PageList accepts numbers and ranges such as 1,3,5-7.
ghostscript
sudo apt-get update
sudo apt-get install -y ghostscriptHow to run the command
gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -sPageList=1,3,5-7 -sOutputFile=pages.pdf input.pdf. Only the listed pages are written.
gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -sPageList=1,3,5-7 -sOutputFile=pages.pdf 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 split-pdf .
docker run --rm -v "$PWD:/work" -w /work split-pdf \
gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -sPageList=1,3,5-7 -sOutputFile=pages.pdf input.pdfExample commands
Run these locally after installing the packages, or inside the container from the Dockerfile.
gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -sPageList=1,3,5-7 -sOutputFile=pages.pdf input.pdfgs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -sPageList=1-3 -sOutputFile=first-pages.pdf input.pdf