How to install, test, convert, resize PDF using ImageMagick, Ghostscript, Windows Vista/7 x64

Ben picture Ben · Jul 14, 2010 · Viewed 23.6k times · Source

I'm having trouble with getting ImageMagick and Ghostscript to work together - everything seems difficult, from installation, to testing, to actually using the software. Does anyone have any tips?

Answer

Ben picture Ben · Nov 30, 2011

I've just spent the last three hours getting ImageMagick to play nicely with PHP for a PDF to JPG convert. Instead of documenting it myself locally, I figured I'd write it up here, with all the buzzwords and things I screwed up, so the next noob can save some time.

Tested on two OSs,Windows 7 x64 and Vista x64, with PHP 5.2.8 and Apache 2.2.11.

You'll need Ghostscript and ImageMagick. I got gs871w64.exe and ImageMagick-6.6.3-0-Q16-windows-x64-dll.exe.

Do a basic install of ImageMagick (I'll call it IM now). Double check the system path variable to ensure you can IM from anywhere. It should point to the IM directory. Test the installation with this line in cmd: convert test.gif test.jpg (assuming you have test.gif). Works OK.

Test a PDF convert (convert test.pdf test.jpg). You should get a (long) message about "gswin32c.exe" and "Postscript delegate failed" and "missing an image filename". That means IM can't find a PDF 'translator'. You need Ghostscript.

Install Ghostscript (I'll call it GS now). I had to do it twice for some reason before it took. Add the 'bin' address in the GS folder to the system variable. Test the installation with gswin32 test.pdf and you should see your file pop up in the GS viewer. Important note: gs doesn't work in Windows ('gs' is not recognized as an internal or external command, operable program, or batch file.). You need to use gswin32.

Back to the IM test: convert test.pdf test.jpg should work now.

Now over to PHP - write up this script:

$out = shell_exec("convert blah.pdf blah2.jpg 2>&1");
echo $out;

The 2>&1 will move all errors into the standard output so you can better diagnose stuff :). Now, your PHP script may run in the cmd prompt, but nothing happens when you run it in a browser. Smack your forehead and restart Apache.

pant Exhaustive and probably elementary, I know. But a hopefully helpful summary of approximately 20 pages of semi-helpful clues.