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.

Dani AI

Generated

If you want this 100% inside PHP (no shell_exec), the usual path is the Imagick extension. It rasterizes PDFs via Ghostscript under the hood, but you call it from PHP, not the shell. Note that FPDF/FPDI are for generating/importing PDFs, not for rendering them to pixels, so they will not do a PDF->PNG/JPG conversion on their own, . (setasign.com)

Two tips that bite a lot of people: set the DPI before you read the PDF, and target pages with bracket syntax. Example below converts pages one-by-one to PNG with a white background while keeping memory low.

<?php
$pdf = '/path/input.pdf';
$outDir = '/path/out';
$probe = new Imagick();
$probe->pingImage($pdf);
$total = $probe->getNumberImages();
$probe->clear();

for ($i = 0; $i < $total; $i++) {
    $im = new Imagick();
    $im->setResolution(200, 200);            // set before readImage
    $im->readImage($pdf . "[$i]");           // pick page i (0-based)
    $im->setImageBackgroundColor('white');
    $im->setImageAlphaChannel(Imagick::ALPHACHANNEL_REMOVE);
    $flattened = $im->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN);
    $flattened->setImageFormat('png');
    $flattened->writeImage(sprintf('%s/page-%03d.png', $outDir, $i + 1));
    $im->clear(); $flattened->clear();
}

Why this works and what to watch for:

  • Call setResolution() before readImage() to control raster DPI; otherwise you get the default 72 DPI. (php.net)
  • Selecting a single page or a range with [0], [0-3], etc. is the supported way to pick frames/pages from multi-page formats. (imagemagick.org)
  • Imagick relies on Ghostscript to read PDFs. If you see errors like "not authorized" or "no decode delegate for this image format PDF", install Ghostscript and ensure PDF is allowed in ImageMagick’s security policy (some distros disable PDF/PS by default). (github.com)
  • For page counts without loading full pages, use pingImage() + getNumberImages() as shown. (php.net)

’s direction was on the right track; adding DPI, explicit page selection, and flattening will make it robust. is right that TIFF->JPG is different, but the same Imagick pipeline works once Ghostscript is in place.

Recommended Answers

All 6 Replies

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.

Member Avatar for Member #120589

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.