Convert pdf to image with imagemagick in php
In a previous article we saw how to use imagemagick to convert pdfs to image to create a snapshot or thumbnail of the pdf. Now we are going to do the same in php using the Imagick class which provides the bindings for imagemagick library inside php. The library is available in the form of a class Imagick that does all the job of imagemagick utility.
On ubuntu install the packages from the commandline as follows
It will create the jpg image file from the pdf file. The image would contain the last page of the pdf file.
To convert a specific page from the pdf use the following syntax
The number in the bracket indicates the page number starting with 0.
Scale down the image
The image can be scaled down using the scaleImage method.
The first parameter of the scaleImage function specifies the height in pixels. The second parameter is left 0 so that imagemagick calculates the correct value for it to preserve the aspect ratio.
There is however an issue when scaling images down. If the pdf has transparency then the transparent regions would turn black. This is fixed by flattening the image as follows
The images created in the above examples would have text that is not very sharp or clear. The clarity is improved by setting a higher resolution. The resolution has to be set before opening the image.
Output to browser directly
To output the image to the browser directly replace the writeImages line with the following
The imagick object can be directly echoed which would echo the image content.
Using convert utility
The convert utility can also be used directly instead of the Imagick class. Then it would need to execute shell function through either of the functions shell_exec, system, exec or passthru.
Conclusion
Lots of things can be done with pdf to image feature. For example make an online pdf reader, that convert each page to image for reading. Or create thumbnails, previews of pdfs
Related Posts
Php developer, blogger and Linux enthusiast.
6 Comments + Add Comment
First of all Thanks for this nice work,
i have one issue my one pdf convert to jpg but other is not can u please tell me what is the reason for this.
Very useful post.
I have following error
Fatal error: Uncaught exception ‘ImagickException’ with message ‘Postscript delegate failed `D:/Xmapp/xampp/htdocs/test1/pdf.pdf’: No such file or directory @ error/pdf.c/ReadPDFImage/645′ in D:Xmappxampphtdocstest1index.php:16 Stack trace: #0 D:Xmappxampphtdocstest1index.php(16): Imagick->__construct(‘D:/Xmapp/xampp/…’) #1
Use the following code to save the pdf page by page:
$img = new imagick($_SERVER[‘DOCUMENT_ROOT’].’/201307301103041375192984.pdf’);
//this must be called before reading the image, otherwise has no effect – “-density <$x_resolution>x<$y_resolution>”
//this is important to give good quality output, otherwise text might be unclear
// -flatten option, this is necessary for images with transparency, it will produce white background for transparent regions
//save image file
Great script helps a lot…..
The above code is worked fine, but only I can view a single page.
Convert pdf to image with imagemagick in php
In a previous article we saw how to use imagemagick to convert pdfs to image to create a snapshot or thumbnail of the pdf. Now we are going to do the same in php using the Imagick class which provides the bindings for imagemagick library inside php. The library is available in the form of a class Imagick that does all the job of imagemagick utility.
On ubuntu install the packages from the commandline as follows
It will create the jpg image file from the pdf file. The image would contain the last page of the pdf file.
To convert a specific page from the pdf use the following syntax
The number in the bracket indicates the page number starting with 0.
Scale down the image
The image can be scaled down using the scaleImage method.
The first parameter of the scaleImage function specifies the height in pixels. The second parameter is left 0 so that imagemagick calculates the correct value for it to preserve the aspect ratio.
There is however an issue when scaling images down. If the pdf has transparency then the transparent regions would turn black. This is fixed by flattening the image as follows
The images created in the above examples would have text that is not very sharp or clear. The clarity is improved by setting a higher resolution. The resolution has to be set before opening the image.
Output to browser directly
To output the image to the browser directly replace the writeImages line with the following
The imagick object can be directly echoed which would echo the image content.
Using convert utility
The convert utility can also be used directly instead of the Imagick class. Then it would need to execute shell function through either of the functions shell_exec, system, exec or passthru.
Conclusion
Lots of things can be done with pdf to image feature. For example make an online pdf reader, that convert each page to image for reading. Or create thumbnails, previews of pdfs
Related Posts
Php developer, blogger and Linux enthusiast.
6 Comments + Add Comment
First of all Thanks for this nice work,
i have one issue my one pdf convert to jpg but other is not can u please tell me what is the reason for this.
Very useful post.
I have following error
Fatal error: Uncaught exception ‘ImagickException’ with message ‘Postscript delegate failed `D:/Xmapp/xampp/htdocs/test1/pdf.pdf’: No such file or directory @ error/pdf.c/ReadPDFImage/645′ in D:Xmappxampphtdocstest1index.php:16 Stack trace: #0 D:Xmappxampphtdocstest1index.php(16): Imagick->__construct(‘D:/Xmapp/xampp/…’) #1
Use the following code to save the pdf page by page:
$img = new imagick($_SERVER[‘DOCUMENT_ROOT’].’/201307301103041375192984.pdf’);
//this must be called before reading the image, otherwise has no effect – “-density <$x_resolution>x<$y_resolution>”
//this is important to give good quality output, otherwise text might be unclear
// -flatten option, this is necessary for images with transparency, it will produce white background for transparent regions
//save image file
Great script helps a lot…..
The above code is worked fine, but only I can view a single page.