1

I have been trying to use ghostscript in PHP to convert pdf files to images (png, jpg).

I don't really care as long as they are images. This is the code that I used.

exec("gs -sDEVICE=jpeg -sOutputFile=/Applications/Mamp/htdocs/cover.jpg -r144 /Applications/Mamp/htdocs/test.pdf"); 

When I enter the exact same thing, without the exec and quotes obviously, into the command line it does exactly what I want. However, when I run the php file nothing happens. I am using a MAMP server and the server seems to work fine, whenever I run another file with it I have no issues. Anyone have any ideas why it might not execute right?

3
  • Have you tried entering the full location of the gs executable? It's probably not in PHP's path. Oct 21, 2012 at 3:54
  • Thank you so much, I was trying to figure this out for hours, I asked all my friends, I have a meeting tomorrow and my boss was going to kill me if I didn't have this done. Such a simple fix, thank you Oct 21, 2012 at 4:25
  • Excellent, I'll give it a proper answer. Oct 21, 2012 at 4:26

1 Answer 1

5

The gs executable is likely not in PHP's path, so it is unable to run it by using exec(). Entering the full path to the command will allow it to be run within PHP.

To find the full path you can run which gs from the command line, since it is executable to you, and then copy that path into the exec() method.

> which gs
/path/to/gs

Update exec() in PHP

exec("/path/to/gs -sDEVICE=jpeg -sOutputFile=/Applications/Mamp/htdocs/cover.jpg -r144 /Applications/Mamp/htdocs/test.pdf"); 
1
  • Thanks for this. This is what I'm looking for
    – marknt15
    Nov 14, 2016 at 5:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.