Anyone know of a way using php (no shell_execs), to convert a pdf to an image type of png, jpg,gif, or tif?

In my existing app we are shelling out using ghostscript, pdftk, etc, and would like to switch this over to all inside of php.

Yes, I've googled. a lot! ;)

it looks like fpdf will allow you to convert an image of those types, but can't see how to load an existing pdf and then to convert it.

Following code might help you in converting from PDF to JPG/TIF/GIF. Further description of following code is available here

  // Saving every page of a TIFF separately as a JPG thumbnail
  $images = new Imagick("testing.tif");
  foreach($images as $i=>$image) {
    // Providing 0 forces thumbnail Image to maintain aspect ratio
    echo "<img src='page$i.jpg' alt='images' ></img>";
catch(Exception $e)
  echo $e->getMessage();

Just replace testing.tif with your pdf file. It will work fine. You need to install ImageMagick and ghostscript for this to work.

Following is further simpler example of pdf to jpg.

$im = new imagick($file_location); // your pdf file 
$im->setImageFormat( "jpg" );

Thank you so much I have done with pdf to png images generate.

Again thank! You save me from the pain.

Member Avatar


The OP was 6 years ago. Please refrain from resurrecting this dead thread. The last post is specific to .NET technologies. The OP, although not specified, but stated in the tags, points to PHP. If anybody has a specific useful link / custom solution for this that improves upon information already given in this thread, fine. Else, please leave alone. Thanks.