By Mathew Byrne

2009-01-22 01:47:25 8 Comments

What libraries, extensions etc. would be required to render a portion of a PDF document to an image file?

Most PHP PDF libraries that I have found center around creating PDF documents, but is there a simple way to render a document to an image format suitable for web use?

Our environment is a LAMP stack.


@jewelhuq 2018-03-29 17:58:36

Think differently, You can use the following library to convert pdf to image using javascript

@imal hasaranga perera 2017-08-22 03:03:47

I'm the author of PDFlib which is a GhostScript wrapper for php, advantage of using this library is, it is already tested and it does not require ImageMagic

Always GhostScript commands are faster than ImageMagic when it comes to pdf so you should either go for a GhostScript wrapper or pure GhostScript commands

$pdflib = new ImalH\PDFLib\PDFLib();

@user2413654 2018-03-08 12:27:00

Hi, I am using your PDFlib to create PNG's from PDFs. $pdflib = new ImalH\PDFLib\PDFLib(); $pdflib->setPdfPath($pdf_file_path); $pdflib->setOutputPath($folder_path_for_images); $pdflib- >setImageFormat(\ImalH\PDFLib\PDFLib::$IMAGE_FORMAT_PNG); $pdflib->setDPI(100); $pdflib->setPageRange(1,$pdflib->getNumberOfPages()); $pdflib->convert(); Does PDFlib have an option to set the width and height of the created PNG files?

@imal hasaranga perera 2018-03-08 12:31:54

Hi, no PDFLib does not provide methods to manipulate images but you can do like this. once you generated the pngs using PDFLib you can use another Image manipulation libraray like to get your work done...

@imal hasaranga perera 2018-03-08 12:34:44

please try the above method I suggested but if you are still struggling send me a mail to [email protected] and I'll help you out with a sample code... cheers !!

@user2413654 2018-03-08 12:38:32

I will give a try. Thanks for the suggestion.

@Sebastian 2014-11-17 18:57:21

Use the php extension Imagick. To control the desired size of the raster output image, use the setResolution function

$im = new Imagick();
$im->setResolution(300, 300);     //set the resolution of the resulting jpg
$im->readImage('file.pdf[0]');    //[0] for the first page
header('Content-Type: image/jpeg');
echo $im;

(Extension on Paolo Bergantino his answer and Luis Melgratti his comment. You need to set the resolution before loading the image.)

@Paolo Bergantino 2009-01-22 01:57:08

You need ImageMagick and GhostScript

$im = new imagick('file.pdf[0]');
header('Content-Type: image/jpeg');
echo $im;

The [0] means page 1.

@Luis Melgratti 2009-01-22 02:16:46

yes, it works. You can also do: $im->setResolution( 300, 300 ) for example to render your pdf at desire resolution.

@David Hobs 2012-09-05 01:27:24

I haven't tried it, but if you're using google docs, and don't mind the iFrame, this suggestion might work (it's also a wp plugin)

@namratha 2013-11-29 06:41:47

but how to include ImageMagick and GhostScript in php file

@think123 2014-01-18 10:34:52

@LuisMelgratti Hmm, that setResolution thingy doesn't seem to work. I put it after the $im = new imagick('file.pdf[0]'); line from Paolo's example. Doesn't seem to do anything. What if I wanted an image from a PDF with a width of say, 1500, a height to scale, and a high resolution? How would I do that? Thanks.

@Kevin Jantzer 2014-01-21 17:37:22

@think123 use $im->thumbnailImage(1500, 0); That will set your jpg image to a width of 1500 and retain scale. See documentation

@Ravi Dhoriya ツ 2014-02-13 11:02:49

How long will it take to build Imagick, I can't wait much, still its building... having cofee :D

@jrjohnson 2014-07-17 03:58:04

From my answer below - If you're loading the PDF from a blob this is how you get the first page instead of the last page: $im->readimageblob($blob); $im->setiteratorindex(0);

@user3784251 2015-02-24 16:34:04

@LuisMelgratti will it work in windows 7??? Is it possible to install in win 7??

@Arvind K. 2016-08-25 07:11:55

I tried it however it gave me: "error no decode delegate for this image format `\path\filename.pdf' @ error/constitute.c/ReadImage/532". I tried $exec = "convert -scale $width $source $dest";exec($exec); and it worked like a charm.

@imal hasaranga perera 2016-09-13 03:21:28

Did any of you felt this is slow ? I felt this very slow and wanted to use Ghostscript only which is more faster.. if any of you interested I wrote a wrapper for it

@vision 2016-09-15 06:47:55

@Paolo Bergantino.i have tried your example but i am getting following error. Fatal error: Uncaught exception 'ImagickException' with message 'UnableToOpenBlob `file.pdf': No such file or directory @ error/blob.c/OpenBlob/2657' in D:\xampp\htdocs\learn\index.php:39 Stack trace: #0 D:\xampp\htdocs\learn\index.php(39): Imagick->__construct('file.pdf') #1 {main} thrown in D:\xampp\htdocs\learn\index.php on line 39

@vision 2016-09-15 06:48:09

Can you help me on this ?

@Geoffrey Brier 2017-01-30 13:53:37

Set the image format to JPG otherwise the image may not render correctly in PNG. Thank you very much ;)

@Chris Happy 2018-02-01 19:28:34

In case the Ghostscript link dies, here the Github page:

@Preet Sandhu 2012-05-17 13:17:10

You can also try executing the 'convert' utility that comes with imagemagick.

exec("convert pdf_doc.pdf image.jpg");
echo 'image-0.jpg';

@John Kurlak 2012-08-14 00:24:42

Instead of pdf_doc.pdf, pdf_doc.pdf[0] is preferred.

@Jack Miller 2014-11-18 05:35:47

convert is part of the package ImageMagick; try sudo yum install ImageMagick

@craned 2016-01-29 15:58:55

If it's still not working, you might need to provide the full path to the convert binary.

@suz 2016-12-01 09:51:01

echo 'image-0.jpg'; returns a string.

@Andrew 2011-03-07 15:10:52

For those who don't have ImageMagick for whatever reason, GD functions will also work, in conjunction with GhostScript. Run the ghostscript command with exec() to convert a PDF to JPG, and manipulate the resulting file with imagecreatefromjpeg().

Run the ghostscript command:

exec('gs -dSAFER -dBATCH -sDEVICE=jpeg -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -r300 -sOutputFile=whatever.jpg input.pdf')

To manipulate, create a new placeholder image, $newimage = imagecreatetruecolor(...), and bring in the current image. $image = imagecreatefromjpeg('whatever.jpg'), and then you can use imagecopyresampled() to change the size, or any number of other built-in, non-imagemagick commands

@Tim Dearborn 2013-12-03 18:44:36

To just get a image of the first page, add the switches -dFirstPage=1 -dLastPage=1 to the command.

@Sharky 2014-11-04 09:14:47

hey andrew change the input.jpg to input.pdf :D

@Andrew 2014-11-04 14:14:21

Thanks Sharky, done!

@craned 2016-01-29 15:57:42

If it's still not working, you might need to give the full path to the gs binary.

@Hayden Thring 2017-09-04 23:11:03

thanks, this worked right away for me

@iProgram 2018-01-16 15:37:59

This looks like it is running a system command. What if the user uploads a file named something;rm -rf /?

@Dhara 2018-05-07 08:03:17

If i want all page of pdf to convert into image. then how to do?

@Jason 2009-01-24 08:25:37

You can also get the page count using


Then you can can create thumbs of all the pages using a loop, eg.


@Simon East 2013-12-18 00:23:47

(This should actually be a comment on Paolo Bergantino's answer)

@user664995 2012-05-11 17:01:57

Here is a simple class I've written and used on a couple of projects. It just wraps imagick and handles writing each page out to disk. If anyone is still looking for an easy way to do this, this link might be helpful.

@Duy Khanh 2012-04-07 15:05:49

I install finished! It's worked!

You may be do base install imagemagick on windows.

In php (local) use call exec(<command line>) ex:

$pdf = "filename.pdf";
$info = pathinfo($pdf);
$file_name =  basename($pdf,'.'.$info['extension']);
echo $file_name;
$pdf = "filename.pdf[0]";
exec("convert $pdf convert-img/$file_name.jpg");    

Besides, you may be use class imagick in PHP Imagick class

Thanks all helped me!

@jrjohnson 2011-06-14 04:23:33

If you're loading the PDF from a blob this is how you get the first page instead of the last page:


@Simon East 2013-12-18 00:24:06

(This should actually be a comment on Paolo Bergantino's answer)

Related Questions

Sponsored Content

29 Answered Questions

[SOLVED] How do you parse and process HTML/XML in PHP?

31 Answered Questions

[SOLVED] How to vertically align an image inside a div?

7 Answered Questions

[SOLVED] How does PHP 'foreach' actually work?

15 Answered Questions

[SOLVED] Merge / convert multiple PDF files into one PDF

24 Answered Questions

[SOLVED] How do I get PHP errors to display?

30 Answered Questions

[SOLVED] Convert HTML + CSS to PDF with PHP?

28 Answered Questions

[SOLVED] How do I auto-resize an image to fit a div container

28 Answered Questions

[SOLVED] How can I prevent SQL injection in PHP?

2 Answered Questions

2 Answered Questions

[SOLVED] PDF to image for mobile device?

Sponsored Content