Managing PDF files with Imagick
October 14, 2008
Filed under
IMagick
Many doesn’t know, but with Imagick anyone can manage PDF files. As far as I know you can’t create one, but this function can be extremely useful to create a screenshot of a PDF file on the server’s side.
First of all, the PDF file you just created has to be loaded in Imagick, either the whole document or only one page from it, then you can output it as any type of image, let’s see how to do it:
$pdf = new imagick( 'your.pdf' ); $pdf->setImageFormat( "gif" ); header( "Content-Type: image/gif" ); echo $pdf;
Be aware that you can not output anything else before this code block neither a whitespace or text. Nothing. Also, you can specify any image format you wish as Imagick can output almost any image format.
Look at the above code block. The first line says:
$pdf = new imagick( 'your.pdf' );
If you would like to select a specific page to output from the PDF, just specify the page number as it follows:
/*This would select the cover page: */ $pdf = new imagick( 'your.pdf[0]' ); /*and this the actual 1st page: */ $pdf = new imagick( 'your.pdf[1]' );
Now you should get the idea, if you don’t, feel free to question me.
The last thing I show you is how to create a thumbnail of a PDF. There is no too much difference, the only one is that before outputting the image we resize it using thumbnailImage():
$pdf = new imagick( 'your.pdf' ); $pdf->setImageFormat( "gif" ); $im->thumbnailImage( 200, NULL ); header( "Content-Type: image/gif" ); echo $pdf;
I used ‘NULL’ to preserve the image’s aspect ratio.
As always, if anything is unclear, feel free to question me.
Possible related posts (automatic):
Related posts brought to you by Yet Another Related Posts Plugin.



















Comments
Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!
If you want to use your OpenID, fill out the field labeled "Website" with the OpenID URL. The other fields may remain empty.