Creating thumbnails of PDFs

articles ✒ pdf-thumbnail

I've been doing a lot of work on our student website recently, and one of the things I wanted to do was show a small thumbnail for each file.

For normal images, PHP's GD image functions suffice.

But GD doesn't do PDFs. I tried ImageMagick (convert) but its PDF support is buggy - it had trouble with landscape pages and rendering certain fonts.

Ghostscript to the rescue:

gs -q -sDEVICE=jpeg -dBATCH -dSAFER -dNOPLATFONTS -dNOPAUSE \
-dFirstPage=1 -dLastPage=1 -r100 -sOutputFile=DEST_JPG' 'SRC_PDF'

Replace DEST_JPG and SRC_PDF with the appropriate paths.

This creates a (large) JPEG version of the PDF.

You can either use ImageMagick or PHP's GD functions to resize it to thumbnail size.

convert -thumbnail x300 'DEST_JPG' 'DEST_JPG'

You can see this technique in use at Merton JCR.

comment on this page