12

I’m attempting to convert PDF files into PNGs. It works great from the command line (I do have GhostScript 8.64 installed). But from PHP I’m having a problem:

code:

$im = new Imagick($pdf_file); // this is where it throws the exception below

output:

Fatal error: Uncaught exception ‘ImagickException’ with message ‘Postscript delegate failed `23_1235606503.pdf’: No such file or directory @ pdf.c/ReadPDFImage/612′ in get_thumbnail.php:93
Stack trace:
\#0 get_thumbnail.php(93): Imagick->__construct(’…’)

etc. etc.

I'm not sure what I'm doing wrong here, but I suspect it has something to do with my server configuration somewhere. I'm running: Apache 2.2.11 PHP 5.2.8 ImageMagick 6.4.8-9 GhostScript 8.64

Norman Ramsey
  • 198,648
  • 61
  • 360
  • 533
matt
  • 846
  • 2
  • 10
  • 13

3 Answers3

18

Finally figured this out. The GhostScript executable (gs) wasn't in Apache's environment path. It was in /usr/local/bin. Though I tried several ways to add /usr/local/bin to the path, I did not succeed. I ended up putting a symlink for gs in the /usr/bin directory. Now everything works perfectly.

Christian Specht
  • 35,843
  • 15
  • 128
  • 182
matt
  • 846
  • 2
  • 10
  • 13
  • 2
    How to know what is the apache environment path? – Arnold Roa Jun 19 '13 at 15:53
  • 2
    Excellent! just to add, create the symlink like this: sudo ln -s /usr/local/bin/gs /usr/bin/gs – Ondrej Rafaj Feb 07 '14 at 17:47
  • Kept on getting `Cannot read file error` and this fixed it. Thanks bro. – Anriëtte Myburgh Jun 02 '15 at 22:10
  • When using ImageMagick to convert a page of a PDF to JPEG through Apache and a programming language, I received the error message `convert: no images defined ...` even though the images existed and permissions were wide open. After uninstalling both GhostScript and ImageMagick, then reinstalling GS and IM with GS, adding the symlink finally resolved this issue. See also https://github.com/delphian/drupal-convert-file/wiki/Installing-ImageMagick-on-Mac-OSX-for-PHP-and-MAMP#manual-installation – Steve Piercy May 19 '16 at 06:30
5

I don't have the "reputation" on Stackoverflow to add a comment inline above, but there is an extra step I had to perform to get this working on my Mac with the latest Sierra update.

When you enter the command:

sudo ln -s /usr/local/bin/gs /usr/bin/gs

On the Mac, you may get the error, "Operation not Permitted".

Apparently Apple made a change that the "bin" directory is not editable, unless you disable SIP (System Integrity Protection).

So here are the steps to do that:

  1. Reboot your Mac into Recorvery Mode by restarting your computer and holding down "Command + R" until the Apple logo appears on your screen.
  2. Click Utilities > Terminal
  3. In the Terminal window, type in crutil disable and press "Enter"
  4. Restart your Mac.

I just went through these steps and now my Ghostscript works great and I successfully converted a PDF to JPG.

user3071502
  • 51
  • 1
  • 2
-4

I am successfully doing this. Here is the code that I am using to do the conversion. We are using this solution commercially. I know this question has been out there for awhile, but it may still help you.

//Convert PDF contract to image using ImageMagik and Ghostscript
// NOTE: This will need to be change if running on Linux
$source = $appDir."\\".$clientID."\\".$clientID.".pdf";
$dest = $appDir."\\".$clientID."\\".$clientID.".jpg";
//print("c:\\IM\\convert.exe $source $dest ");
exec("c:\\IM\\convert.exe $source $dest ");
Mark
  • 1,368
  • 5
  • 13
  • 26
  • @CarlosBarbosa, `convert.exe` is a binary included with the windows version of ImageMagick (just `convert` on *nix OSes). While I agree this isn't the best answer, it is a way to work around the problem the OP was having. – Andrew Ensley Jun 24 '14 at 21:32