Converting PDF pages to a single image

I am always amazed how far one can get by chaining the output of simple programs to create something bigger. This time, I had to convert the pages of a PDF file to a single PNG thumbnail image. As I learned in five minutes this is super simple with pdftoppm, Imagemagick’s convert tool and a Makefile to hook it up together:

%.png: %.pdf
	pdftoppm -png $< tmp
	convert +append tmp-* -resize x320 -unsharp 0x1 $@
	rm tmp-*

What I do here is calling pdftoppm to generate numbered PNG files with a tmp- prefix. convert reads them, appends them horizontally (denoted by the + sign), resizes the final image to a height of 320 pixels and filters the output with an unsharp mask to “recover” some of the lost sharpness.

To make the pages stand out you can also add a black single pixel frame and a white separation border with

%.png: %.pdf
	convert $<-*.png -resize x320 -unsharp 0x1 -bordercolor black -border 1x1 -bordercolor white -border 2x2 +append $@