RedVodkaJelly Logo

How-to: Create PDF preview images in PHP – Part 2

POSTED AT 21:44 on 15th April 2008

There were comments on the original part of this example expressing that it wasn’t very newbie friendly since I didn’t show much code and focussed mostly on the installation of ImageMagick and not what to do once you have it installed. This post will hopefully make it clearer for all those wanting to write the code to create the PDF previews.

The Problem
Users upload PDF’s to our websites and we want to show a preview or the whole PDF content in image form.

Requirements
The following applications need to be installed on the server you are running your webserver.
- ImageMagick
- GhostScript

The Code
To create the images of the PDF we call the external ImageMagick application from the command line. In PHP this is done like so:

  1. <?php
  2. exec(‘command line to run’);
  3. ?>

So to run ImageMagick from the command line we do:

  1. <?php
  2. exec(‘/path/to/imagemagick’);
  3. ?>

The paramters we then use to call ImageMagick depend on what we want to do, and consulting the ImageMagick help pages are the best way to learn exactly what is happening.

Below are some common examples:

Create GIF Thumbnail of First PDF Page

  1. <?php
  2. //the path to the PDF file
  3. $strPDF = "my_pdf.pdf";
  4. exec("convert \"{$strPDF}[0]\" -colorspace RGB -geometry 200 \"output.gif\"");
  5. ?>

Create JPEG Thumbnails of ALL Pages Within PDF

  1. <?php
  2. //the path to the PDF file
  3. $strPDF = "my_pdf.pdf";
  4. exec("convert \"{$strPDF}\" -colorspace RGB -geometry 200 \"output.jpg\"");
  5. ?>

Create Large PNG 1024px Image of First PDF Page

  1. <?php
  2. //the path to the PDF file
  3. $strPDF = "my_pdf.pdf";
  4. exec("convert \"{$strPDF}[0]\" -colorspace RGB -geometry 1024 \"output.png\"");
  5. ?>

Create Large PNG 1024px Images of ALL Pages Within PDF

  1. <?php
  2. //the path to the PDF file
  3. $strPDF = "my_pdf.pdf";
  4. exec("convert \"{$strPDF}\" -colorspace RGB -geometry 1024 \"output.png\"");
  5. ?>

As you can see the code is very simple as we are just calling an external command line application to do the hard work – the only thing we have to do is provide the parameters to tell ImageMagick what to do.

This was a follow-up to How-to: Create PDF preview images in PHP

Trackbacks

Comments

  • Robert

    Hi, my hosting company will not install ghostscript unless im on a dedicated server :( and kinda stuck with them. is there any other work around for generating a thumbnail of the cover of a pdf? im in a pickel as i have 500 entries to show from db… any sugestions would be great.

    Posted at 03:28 on 19th August 2008
  • Jacob Wyke

    Hi Robert,

    It’s going to be difficult to do unless your hosting company install other software to do the actual processing.

    If you can’t convince them or change host then the only other option would be to try and link in a 3rd party service that does the conversion.

    A quick search came up with ‘http://convert.neevia.com/’ – here you can upload a file and have it converted to any image type you like. You would just need to write a script that uploaded the file to them and download the converted image to your server.

    If your going to batch process your entries, then you could do it on your own PC. Simply install ImageMagick and Ghostscript on your PC/Mac and have it process the files and then upload the files.

    If you want any help with either option then I would be happy to help – just drop and email to jacob@redvodkajelly.com.

    Regards,
    Jacob.

    Posted at 15:28 on 19th August 2008
  • Robert

    Thanks i convienced them to install Ghostscript as they don’t normally allow it, only on dedicated servers but i pleaded and they did and now all is fine.
    Note: took 24 hrs. after they confirmed the install. this drove me nuts with testing. but now works.
    Thanks again.

    Posted at 04:28 on 20th August 2008
  • Robert

    Sorry to bother you again,
    is there an easy way to ad a drop shadow and a colored border. i looked at the link for the commads and don’t understand them. im using this code based on what you provided. <?php
    //the path to the PDF file
    $strPDF = “GC_MediaKit2008Web5.pdf”;

    exec(“/usr/bin/convert \”{$strPDF}[0]\” -colorspace RGB -geometry 40% -border 2 \”44.png\”");
    echo ”;
    ?>

    Posted at 05:24 on 20th August 2008
  • Robert

    one more question, I have different size ads and the script seems to use the paper size for the canvas is their an easy way to create the thumbnail using document size and not paper size? i have about 150 pdf’s…

    Posted at 22:11 on 22nd August 2008
  • kal l

    Thanks – this info was just what I was looking for!

    Posted at 18:46 on 27th September 2008
  • raj

    you are gr8 :) , i hope you die and go to haven sir,
    raj from india

    Posted at 22:20 on 10th August 2009

Leave a Comment