Page 1 of 1

Convert PDF to image

Posted: 2009-03-31T04:48:01-07:00
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!

Re: Convert PDF to image

Posted: 2009-03-31T06:32:21-07:00
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.

Re: Convert PDF to image

Posted: 2009-03-31T07:18:11-07:00
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!

Re: Convert PDF to image

Posted: 2009-03-31T08:43:16-07:00
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