Convert PDF to image

IMagick is a native PHP extension to create and modify images using the ImageMagick API. ImageMagick Studio LLC did not write nor does it maintain the IMagick extension, however, IMagick users are welcome to discuss the extension here.
Post Reply
umid

Convert PDF to image

Post by umid »

Hi there,

We use Imagick for online pdf viewer.

Here is the code:

=============================================
public function convertPDF($pdfName, $code) {

$images = new Imagick();
$images->setResolution( 300, 300 );
$images->readImage($_SERVER['DOCUMENT_ROOT'] . "/uploads/" . $pdfName );

foreach ($images as $k => $image) {

/******************************
* CREATE THE IMAGE
****************************/

$image->setImageFormat("png");
$image->setImageUnits(Imagick::RESOLUTION_PIXELSPERINCH);
$image->setImageResolution(150, 150);

// Write out large image, go on.
$large_image = $_SERVER["DOCUMENT_ROOT"] . "/pdf-reader/pages/large/" . $code . "_" . $k . "_.png";
$image->thumbnailImage(NULL, 1403);
$image->writeImage($large_image);

// Write out small image, go on.
$small_image = $_SERVER["DOCUMENT_ROOT"] . "/pdf-reader/pages/" . $code . "_" . $k . "_.png";
$image->thumbnailImage(NULL, 584);
$image->writeImage($small_image);


}

$image->clear();
$image->destroy();

}
=============================================

But we are having problem when we try to convert larger pdfs. For example about 0.5-1 MB pdfs with images.

It takes long to convert pdf into png.

I am a newbie for Imagick and Image Magick.

Any help will be highly appreciated.

Thanks!
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Convert PDF to image

Post by magick »

Do you need to convert the entire PDF or just the first page? If its just the first page add [0] to your filename (e.g. 'image.pdf[0]') and only the first page is rendered speeding up the process. If you need to render the entire PDF, the process can be slow-- ImageMagick is pushing a lot of pixels.
umid

Re: Convert PDF to image

Post by umid »

magick wrote:Do you need to convert the entire PDF or just the first page? If its just the first page add [0] to your filename (e.g. 'image.pdf[0]') and only the first page is rendered speeding up the process. If you need to render the entire PDF, the process can be slow-- ImageMagick is pushing a lot of pixels.
Thank you for your reply!

I want to convert all pages of pdf to png. After that the user will be redirected to Flash Flipping book page and converted images will be used to view pdf like flipping book.

Is there any way to speed pdf conversion to image?

Any help will be highly appreciated.

Thanks!
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Convert PDF to image

Post by magick »

Try using Ghostscript directly from the command line and bypass ImageMagick. To get started take a look at the command ImageMagick uses:
  • convert -verbose image.pdf image.png
Post Reply