How-to: Create PDF preview images in PHP
POSTED AT 12:57 on 15th October 2007
A few months ago I was looking to create screenshots of PDF documents on the fly so that a preview could be shown to users of a website before downloading or opening the file.
The solution to my problem was simple…so simple in fact that I can’t believe I haven’t done it before.
This works on all the major OS’s – Windows, linux and Mac – you will just have to download and install the correct packages for the machine you want to run it on. Mac users may find it easier to use a package management tool such as Fink to handle the installation.
Install GhostScript
Download and install GhostScript which is an interpreter for PostScript and PDF. If your installing on a Linux machine it is likely that you will already have this installed, so check before hand.
Install ImageMagick
Download and install ImageMagick which is an fully feature laden image manipulation suite which we will use to convert the PDF. Again if your running Linux you may already have this installed – so double check and/or upgrade.
ImageMagick is an amazing tool and can handle pretty much anything you could think of doing to an image, and it’s fully command line based so you can use it on the fly from within your scripts.
The PHP Code
The PHP code that we will use to convert the PDF to a png will make use of the ImageMagick software that we just installed and call it from within the PHP script using the exec() function.
exec(’convert “document.pdf” -colorspace RGB -geometry 200 “document.png”‘);
This line will convert the whole PDF document into individual images for each page at 200px wide. To just create an image of the first page of the PDF we use the following:
exec(’convert “document.pdf[0]” -colorspace RGB -geometry 200 “document.png”‘);
To get the second page you would just replace ‘[0]‘ with ‘[1]‘ etc.
For more options check out the ImageMagick help pages or simply run ‘convert’ from the command line and play about with the options.
If your not a fan of PNG’s then simply change the extension to your favorite image type and it will output the screenshot in that format.
The Result
![]()
UPDATE: There is an updated version of this post here.
Trackbacks
Comments
-
Ben J. Schmidt
Now thats slick, I am definitely going to have to remember that for one of my upcoming projects involving some e-books.
Posted at 03:05 on 16th October 2007
-
Lucie
Hi, I have tried the same steps that you have given above and it works great,and I have created Image from the PDF. thanks a lot.
Posted at 00:55 on 11th January 2008
-
Jacob Wyke
Glad that it worked for you Lucie!
Posted at 01:38 on 11th January 2008
-
Brenda
Sorry, but for a newbie, this isn’t very clear. Can you give an example of what the code looks like in a web page please? Thanks.
Posted at 20:56 on 15th April 2008
-
Radu
I have tryed your code but I get an error because exec has been disabled on the server. Could you please point me in the right direction by offering an alternative
Thanks
Posted at 16:32 on 16th May 2008
-
Matt
Thank you for the tips, I love you :D
Posted at 02:29 on 29th October 2008
-
Jeff
Is there a way to make this work with GD instead of ImageMagick?
Posted at 15:49 on 26th March 2009
-
Jacob Wyke
HI Jeff,
No I don’t think there is as it relies on GhostScript to view the PDF and there is no way that I know of to link GS with GD.
You having a problem with getting ImageMagick installed on your server?
Posted at 16:03 on 26th March 2009
-
Troy
I have both GhostScript and ImageMagick installed but I still don’t see my image.
Both programs are installed to their default locations, also using a Mac.Any suggestions?
Posted at 15:01 on 8th May 2009
-
Jacob Wyke
Hi Troy,
Test it all out in the Terminal and see what works and what doesnt.
Start off with the basics and see if:
‘convert’
works in the terminal, then try and convert a gif into a png:
‘convert image.gif image.png’
Then a PDF into an image:
‘convert document.pdf image.jpg’
Etc. My initial guess as to your problem would be incorrect paths to the program or its not in the PATH and so you need to reference it fully.
Posted at 15:24 on 8th May 2009
-
Troy
Hey Jacob, Thanks for the rockstar quick reply, it seems I am running version 6.3.3 and i can’t seem to find the convert command. Is that because I am running 6.3.3?
When you suggest incorrect paths to the program, I assume you are referring to the php.ini?
Posted at 15:57 on 8th May 2009
-
Jacob Wyke
Hi Troy,
You first need to find the location of where ‘convert’ is installed. On my mac it is installed at ‘/usr/bin/local/convert’ and on my linux server its at ‘/usr/bin/convert’.
Then use that full path in your code.
All the exec() does it run the command on the terminal – so for testing its quicker to use the terminal app (Applications>Utilities>Terminal).
If it still doesnt work with the correct full path then it may be a security issue and can probably be fixed by tweaking the php.ini file to make sure safe mode isnt on etc.
Jacob.
Posted at 16:10 on 8th May 2009




How-to: Create PDF preview images in PHP - Part 2 - RedVodkaJelly.com
Added at 21:44 on 15th April 2008