How do I convert a PDF document to a preview image in PHP?

PhpImagePdfLamp

Php Problem Overview


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.

Php Solutions


Solution 1 - Php

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.

Solution 2 - Php

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

Solution 3 - Php

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.']'

Solution 4 - Php

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.)

Solution 5 - Php

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);

Solution 6 - Php

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

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

Solution 7 - Php

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();

Solution 8 - Php

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

Solution 9 - Php

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!

Solution 10 - Php

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMathew ByrneView Question on Stackoverflow
Solution 1 - PhpPaolo BergantinoView Answer on Stackoverflow
Solution 2 - PhpAndrewView Answer on Stackoverflow
Solution 3 - PhpJasonView Answer on Stackoverflow
Solution 4 - PhpSebastianView Answer on Stackoverflow
Solution 5 - PhpjrjohnsonView Answer on Stackoverflow
Solution 6 - PhpPreet SandhuView Answer on Stackoverflow
Solution 7 - Phpimal hasaranga pereraView Answer on Stackoverflow
Solution 8 - PhpjewelhuqView Answer on Stackoverflow
Solution 9 - PhpDuy KhanhView Answer on Stackoverflow
Solution 10 - Phpuser664995View Answer on Stackoverflow