PHP PDF Thumbnail Creator

PDF Icon

For many people, PDF files play a vital role in their business. But PDFs are cumbersome to handle for website designers because browsers rely on plug-ins/extensions/add-ons to display them. You can link to them directly to let users view them as separate documents, but plain text links are dull and boring so it's natural for a designer to prefer to use a thumbnail image for a link to a PDF file. I faced this issue myself recently. I was redesigning a website for a client who offered a small library of whitepapers in PDF format on his website. I wanted to highlight a handful of them on the site's main page to brighten it up a bit.

Some quick Google searches showed several online conversion tools and a small number of downloadable applications that would convert PDF files into JPG image files one-by-one. But I wanted to give the client the capability of changing this display at any time. So I needed a general purpose script to create the thumbnail images instantly on demand. Knowing that ImageMagick could read PDF files (with the help of GhostScript - a common companion library found on most servers), I decided to write my own script to create the converted thumbnail images.



ImageMagick makes this job very simple. The script requires that ImageMagick v6.x and GhostScript be installed on the server, and that you've created a directory that will hold the thumbnail images. The script is designed to be used as the 'src' of an <img> tag, so you can embed the images in your web pages easily. An example of the HTML involved would be:


a href="/pdf/myDocument.pdf" title="View This PDF File"
img src="/scripts/pdf2jpg.php?src=pdf/myDocument.pdf" width="250" alt="PDF Document" /
/a

You can either modify the script to alter this, or use the 'path' parameter in the query string of the 'src' attribute of your img tag to select the image's directory as well.

At execution time, the script checks to see if the thumbnail already exists in the cache to avoid re-creating it every time a user visits the page in question. If it exists, that image is served to the user. If not, a new JPG thumbnail image is created from the first page of the PDF document. This reduces the load on the server and speeds up page rendering for users and Google!

The core of the script is simply an ImageMagick "convert" command, so there's no elegant, extensive, or exotic coding involved here at all. ImageMagick does the file format conversion and image resizing in one stroke. But there are some handy features included to make the script very versatile:

These options are controlable with parameters in the query string, so you can change them in your web pages as needed without having to modify the script. I always find choosing a method for setting the path names to be a difficult decision. Hard-coding everything limits the script's capabilities, but making the path names selectable makes the code a bit convoluted. By default, the script expects the 'src' parameter to be the path to the PDF file from the website root directory (see the example code above). The script is self-documenting, so if you don't like the way I handled this issue, you can change it easily enough.

The script outputs error messages if a problem arises, so if you don't get the result you wanted, you can load the URL from the img tag 'src' into your browser to check for errors - especially in those path names.

I chose to use JPG for the image file format because they give a broad color pallette with good compression, but you could modify the script to output PNG or GIF easily, too.

My PDF to JPG Converter is provided free of charge. If you use and enjoy this software, please place the following link in an appropriate location on your website:

a href="http://www.rainbodesign.com/pub/" Rainbo Design Tools & Scripts/a




This page was last modified on December 09, 2015


Copyright © 2005-2017 by Richard L. Trethewey - Rainbo Design Minneapolis. It's not that I think this is such an earth-shatteringly great presentation, but if you want to copy it, I'd appreciate it if you would ask for permission. I've been working with computers for over 30 years now, and one phrase keeps popping into my head - "I hate it when it's right!" And laptops and smartphones are no better.

Looking for more website design information? See my Sitemap!


  Share This Page!  
Still Have Questions? 
Talk To Me! Click Here!