219

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.

Mathew Byrne
  • 3,713
  • 5
  • 25
  • 23

10 Answers10

248

You need ImageMagick and GhostScript

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

The [0] means page 1.

jg2703
  • 169
  • 4
  • 20
Paolo Bergantino
  • 480,997
  • 81
  • 517
  • 436
  • 12
    yes, it works. You can also do: $im->setResolution( 300, 300 ) for example to render your pdf at desire resolution. – Luis Melgratti Jan 22 '09 at 02:16
  • 6
    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) https://forrst.com/posts/PDF_thumbnails_with_Google_Docs-6G6 – David Hobs Sep 05 '12 at 01:27
  • 6
    but how to include ImageMagick and GhostScript in php file – namratha Nov 29 '13 at 06:41
  • @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. – Lucas Jan 18 '14 at 10:34
  • 3
    @think123 use `$im->thumbnailImage(1500, 0);` That will set your jpg image to a width of 1500 and retain scale. [See documentation](http://www.php.net/manual/en/imagick.examples-1.php#example-3301) – Kevin Jantzer Jan 21 '14 at 17:37
  • How long will it take to build Imagick, I can't wait much, still its building... having cofee :D – Ravi Dhoriya ツ Feb 13 '14 at 11:02
  • 3
    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); – jrjohnson Jul 17 '14 at 03:58
  • @LuisMelgratti will it work in windows 7??? Is it possible to install in win 7?? – user3784251 Feb 24 '15 at 16:34
  • 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. – Arvind K. Aug 25 '16 at 07:11
  • 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 https://github.com/imalhasaranga/PDFLib – Imal Hasaranga Perera Sep 13 '16 at 03:21
  • @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 – scott Sep 15 '16 at 06:47
  • Can you help me on this ? – scott Sep 15 '16 at 06:48
  • Set the image format to JPG otherwise the image may not render correctly in PNG. Thank you very much ;) – Geoffrey Brier Jan 30 '17 at 13:53
  • In case the Ghostscript link dies, here the Github page: https://github.com/ArtifexSoftware/ghostpdl-downloads/releases – Chris Happy Feb 01 '18 at 19:28
  • I used this but opted to have the server serialize/cache the resulting thumbnail. The next time the API is requested to provide a thumbnail, it checks if a cached thumbnail is available and provides that by unserializing the resulting image. It has made thumbnail retrieval using this method incredibly fast and I'm not even storing them in a database. Would probably be even faster if properly stored in a DB. – Robert Talada Jul 29 '19 at 14:42
  • "ImageMagick was not designed to securely handle untrusted PDF files. Enabling PDF file handling is dangerous if any malicious PDF files are ever processed." from https://serverpilot.io/docs/how-to-install-the-php-imagemagick-extension/ – Avatar May 05 '21 at 11:22
  • How to store this image in variable that can be directly use in `` – Ashish Patel May 19 '21 at 12:35
38

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

Andrew
  • 5,095
  • 6
  • 42
  • 48
33

You can also get the page count using

$im->getNumberImages();

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

'file.pdf['.$x.']'
kaiser
  • 21,817
  • 17
  • 90
  • 110
Jason
  • 2,035
  • 11
  • 13
18

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

<?php    
$im = new Imagick();
$im->setResolution(300, 300);     //set the resolution of the resulting jpg
$im->readImage('file.pdf[0]');    //[0] for the first page
$im->setImageFormat('jpg');
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.)

Sebastian
  • 5,471
  • 5
  • 35
  • 53
11

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);
jrjohnson
  • 2,401
  • 17
  • 23
10

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

exec("convert pdf_doc.pdf image.jpg");
echo 'image-0.jpg';
Toby Allen
  • 10,997
  • 11
  • 73
  • 124
Preet Sandhu
  • 435
  • 1
  • 8
  • 11
5

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();
$pdflib->setPdfPath($pdf_file_path);
$pdflib->setOutputPath($folder_path_for_images);
$pdflib->convert();
Imal Hasaranga Perera
  • 9,683
  • 3
  • 51
  • 41
  • 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? – WGS Mar 08 '18 at 12:27
  • 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 https://github.com/Treinetic/ImageArtist to get your work done... – Imal Hasaranga Perera Mar 08 '18 at 12:31
  • please try the above method I suggested but if you are still struggling send me a mail to imal@treinetic.com and I'll help you out with a sample code... cheers !! – Imal Hasaranga Perera Mar 08 '18 at 12:34
  • I will give a try. Thanks for the suggestion. – WGS Mar 08 '18 at 12:38
5

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

http://usefulangle.com/post/24/pdf-to-jpeg-png-with-pdfjs

jewelhuq
  • 1,210
  • 15
  • 19
  • If I want to display the preview image in server how can we use that? – chithra Feb 14 '19 at 09:03
  • 1
    You may want to take a look at this: https://github.com/scandel/pdfThumbnails. I think you can upload the image thumbnail generated here alongside the actual file and save it, this way you will save more computational time on your server (since the thumbnailing process were done in a client-side computer). – sajed zarrinpour Sep 10 '19 at 11:16
  • And only receiving and allowing an image file is saver than using ImageMagick on untrusted PDFs. – Avatar May 05 '21 at 11:25
  • The only problem with this solution: The necessary [pdf.min.js](https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.2.228/pdf.min.js) is 328 KB in size. – Avatar May 05 '21 at 11:32
3

I install finished! It's worked!

You may be do base install imagemagick on windows.

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

<?php
$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!

Duy Khanh
  • 384
  • 1
  • 5
  • 11
1

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.

askmish
  • 6,464
  • 23
  • 42
user664995
  • 21
  • 1
  • 3
    Link-only answers are low value on StackOverflow because if the link moves or dies, the answer is rendered absolutely useless. To improve your answer, the bulk of your solution should be hardcoded here. – mickmackusa Feb 13 '19 at 05:39