All posts

PDF to images via CLI

Rasterize PDF pages to PNG with Ghostscript.

Open online converter

Packages to install

You need ghostscript and the png16m device. -r sets DPI: 150 for previews, 300 for print.

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

How to run the command

gs -sDEVICE=png16m -r150 -sOutputFile=page-%02d.png input.pdf. The %02d pattern writes page-01.png, page-02.png, and so on.

gs
gs -dNOPAUSE -dBATCH -dSAFER -sDEVICE=png16m -r150 -sOutputFile=page-%02d.png 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 pdf-to-images .
docker run --rm -v "$PWD:/work" -w /work pdf-to-images \
  gs -dNOPAUSE -dBATCH -dSAFER -sDEVICE=png16m -r150 -sOutputFile=page-%02d.png input.pdf

Example commands

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

Example 1
gs -dNOPAUSE -dBATCH -dSAFER -sDEVICE=png16m -r150 -sOutputFile=page-%02d.png input.pdf
Example 2
gs -dNOPAUSE -dBATCH -dSAFER -sDEVICE=png16m -r300 -sOutputFile=page-%03d.png input.pdf