Skip to content
MagickCmd › ImageMagick PDF to image (and back)

ImageMagick PDF to image (and back)

Convert PDF pages to images with ImageMagick at the right resolution, extract single pages, and fix the two errors everyone hits: the PDF security policy and missing Ghostscript.

This is the ImageMagick task that fails most often, and almost never because of your command. Two environment problems account for nearly every report — both are covered below.

Convert a PDF to PNG at print resolution

magick -density 300 input.pdf -background white -alpha remove \
  -quality 100 page-%02d.png

Three details matter here:

  • -density must come before the input filename. It tells Ghostscript what resolution to rasterise at. Placed afterwards, the PDF has already been rendered at the default 72 DPI and you are only upscaling a blurry image.
  • PDF pages have a transparent background. Converting straight to JPEG turns that black, so flatten onto white.
  • A multi-page PDF needs a counter such as %02d in the output name, or ImageMagick appends its own suffixes.

Choosing a density

UseDensity
Screen preview, thumbnails72–150
Web display, reading150–200
Print, OCR300–400

Density multiplies pixel count quadratically. A 300 DPI render of an A0 poster is around 40,000 pixels wide and will exhaust ImageMagick's resource limits. Match the density to the page size, not to a habit.

Extract a single page

magick -density 300 'input.pdf[0]' -quality 100 cover.png

The bracket index is zero-based, so [0] is the first page and [0-3] is the first four. The quotes are required or your shell tries to expand the brackets as a glob pattern.

Images to PDF

magick page-*.jpg -quality 92 output.pdf

Zero-pad your filenames — the shell sorts them as text, so page-10.jpg comes before page-2.jpg.

ImageMagick re-encodes every page, so assembling scans this way often produces a larger file than the sources. For lossless assembly of existing JPEGs, img2pdf is the better tool.


Error: "not authorized by the security policy `PDF'"

Your command is fine — policy.xml is blocking it. After the 2018 Ghostscript vulnerabilities, distributions began shipping ImageMagick with PDF, PS, EPS and XPS disabled by default. Find the file:

magick -list policy

Open the path it prints — commonly /etc/ImageMagick-7/policy.xml — and remove or comment out the lines reading:

<policy domain="coder" rights="none" pattern="PDF" />
<policy domain="coder" rights="none" pattern="PS" />
<policy domain="coder" rights="none" pattern="EPS" />

Only do this if you trust the PDFs. On a server that rasterises files uploaded by strangers, that policy exists for a reason — keep it and use a sandboxed pdftoppm from poppler-utils instead. Full explanation →

Error: "no images defined" or "FailedToExecuteCommand `gs'"

Ghostscript is not installed. ImageMagick cannot decode PDF by itself — it shells out to gs, and when that binary is missing it reads zero images and then complains it has nothing to write.

apt install ghostscript      # Debian / Ubuntu
brew install ghostscript     # macOS
dnf install ghostscript      # Fedora

Full explanation →


Related

Copied