Accessibility Navigation

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

Posted on 20th January 2011 by Jacob Wyke

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:

<?php
	exec('command line to run');
?>

So to run ImageMagick from the command line we do:

<?php
	exec('/path/to/imagemagick');
?>

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

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

Create JPEG Thumbnails of ALL Pages Within PDF

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

Create Large PNG 1024px Image of First PDF Page

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

Create Large PNG 1024px Images of ALL Pages Within PDF

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

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


Have Your Say

Have Your Say Form









Comments

  • Andy hughes on 20th January 2011 wrote:

    I’ve been looking for something like this for ages.
    I’m actually coding in Classic ASP on a Windows 2008 Server, so i’m probably not going to be able use this particular version.
    If there is a way, i’d love to know.
    Thanks
    Andy


  • Jacob on 20th January 2011 wrote:

    @Andy – I don’t see why you wouldn’t be able to. I have installed imagemagick and ghostscript on many windows machines and you’re only calling that on the command line. So just use the same basic commands as above, but within the ASP function/method that lets you run something in DOS.


  • Andy Hughes on 20th January 2011 wrote:

    Hi Jacob -
    Thanks for the post. Yes i agree. I didn’t really take enough time to look at the code, but i’m pretty sure i’ll be able to get this working.
    I’ll post back when i’ve taken a good look at it.
    One other thing though. Would there be any security issues with running the scripts on the live server?
    Not too familier with this.
    Thanks again -
    Andy


  • Jacob Wyke on 20th January 2011 wrote:

    @Andy – Just make sure you filter any user input that is included within the command passed to the command line or else anybody could use your script to run any command on your system.


  • Luca Garcia on 24th January 2011 wrote:

    Thanks for making it simple, but somehow I still fail in setting up the correct parameter.

    Btw, I don’t know if it is the right question. I googled and found this website,

    Could you review and explain how http://www.commandlinepdf.com works? I am currently using their demo version and happy with the result.

    An expert’s (like you) seem to be the right people to give me some review.

    Thanks in advance


  • Sleepydog on 14th February 2011 wrote:

    Why is Ghostscript needed but not called?


  • Jacob Wyke on 14th February 2011 wrote:

    @Sleepydog – ImageMagick uses ghostscript to be able to process the PDF.


  • Peter Arnhem on 17th February 2011 wrote:

    Thanks for the clear and readable explatation!Saved me a lot of time.

    Peter


  • Anes on 7th March 2011 wrote:

    Hi Jacob Wyke,
    I am new to imageMagick , I need to create the image of my first page of PDF for the purpose of PREVIEW. But I have not exec() function access in my server . without that How can I implement same thing , waiting your reply
    any one can advise me and reply at : anes(dot)pa(@)gmail(dot)com

    Thankfully
    Anes P.A


  • Christoph on 21st March 2011 wrote:

    We got an issue under Mac OS X 10.6 with following error_log:

    “sh: convert: command not found
    sh: gs: command not found”

    We figured out after googling that one has to symlink pathes from gs and convert to /usr/bin/ like this to use it via exec() in PHP:

    “ln -s `which gs` /usr/bin/”
    “ln -s `which convert` /usr/bin/”


  • Sendy on 6th April 2011 wrote:

    hi Jacob,
    how to create pdf thumbnail from pdf url..?

    Thanks


  • Jacob Wyke on 6th April 2011 wrote:

    @Sendy – Just download the file from the remote URL first and then treat it the same as any other local file. You canuse the usual fopen() or curl etc.


  • Leave Airykson on 9th November 2011 wrote:

    great job!

    I found an other solution, what about this?

    setImageFormat( “png” );
    header( “Content-Type: image/png” );
    echo $im;

    ?>


  • Leave Airykson on 9th November 2011 wrote:

    damn.. comment broke.

    $im = new imagick( ‘test.pdf[0]‘ );
    $im->setImageFormat( “png” );
    header( “Content-Type: image/png” );
    echo $im;


  • Neil on 16th December 2011 wrote:

    Can this be done without using the exec command as this is not generally enabled for shared hosting