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

<?php
try
{
  // 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
    $image->thumbnailImage(768,0);
    $image->writeImage("page".$i.".jpg");
    echo "<img src='page$i.jpg' alt='images' ></img>";
  }
  $images->clear();
}
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" );
$pdf_pages=$im->getNumberImages();

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

Again thank! You save me from the pain.

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.

This article has been dead for over six months. Start a new discussion instead.