Gmagick thumbnail for multi-page PDF

1.1k views Asked by At

I'm attempting to create a thumbnail of a multi-page PDF document using Gmagick, however I only want the first page of the PDF, not all of them.

$thumb = new Gmagick();
$thumb->readImage("/path/to/file/document.pdf");
$thumb->setImageFormat('JPG');
$thumb->thumbnailimage(198, 255);
$thumb->writeImage("/path/to/file/document.jpg");
$thumb->destroy();

This code works, however instead of creating just 1 image 'document.jpg' it creates 'document.jpg.0', 'document.jpg.1', 'document.jpg.2', etc. for all of the PDF pages. I could go and delete all the additional pages and rename the first image just 'document.jpg' but that seems a bit hacky to me.

Is there a way to designate just the first page of the PDF? I can't seem to find anything, and Gmagick's documentation seems to be lacking.

1

There are 1 answers

0
Jpsy On

Pekka is right. GraphicsMagick - just like ImageMagick - accepts the special pdf file name notation filename.pdf[0] which would render only the first page, filename.pdf[1] for the second page, and so on.