Convert PDF to Image using PHP

December 27th, 2015 PHP 4,401 views

Recently one of my friend asked me if there is a way to convert a PDF to Image file. I tried to do that few months ago by following some tutorial that was using combination of Ghostscript and Imagemagick tools. But I didn’t succeed at that time as there were so many version conflicts on Imagemagick with PHP on windows.

So this time I did some more research and found that this can be done just by using Ghostscript commands on PHP. On linux server this is very easy as we just have to run the exec() function and use the ghostscript command “gs” through it with passing necessary arguments. But on windows(local system), this is little tricky as you have to download and install Ghostscript. And add that ghostscript folder to system environment variable so that you can run the “gs” command from any directory. But then, how to do this on actualy Windows server?

To overcome this issue, I tried to do something funny. I copied the installation folder of Ghostscript to my project’s folder. And run the command using absolute path of my project directoy. And it worked perfectly. I tried running same source code on different machine too where I didn’t even install Ghostscript and there also it worked. Below is the example ghostscrit command.

Windows :

Linux :

Once I had both these commands working perfectly, I decided to optimize the code so that it can work on both Windows and Linux servers and can be useful to others too. Please follow below links :

Download Sourcecode | Demo

Leave your comments: