Imagick don't open pdf files with "Failed to read the file" exception

oleg picture oleg · Jun 20, 2016 · Viewed 11.4k times · Source

Trying to open PDF file from php script drop into fault with error:

Uncaught ImagickException: Failed to read the file in ...

I'm running OS X, Brew, PHP 7.0, php70-imagick --HEAD, ImageMagick --with-ghostscript, GhostScript

Command line test works fine:

convert 1.pdf 1.jpg

File permission is ok. Try both, an URL and local file with realpath function, so filepath is ok too. Script works fine if open jpeg.

phpinfo() -> ImageMagick supported formats: EPDF, PDF, PDFA, etc.

UPDATE: Solved. Solution below.

Answer

oleg picture oleg · Jun 20, 2016

This problem was with path to ghostscript. It's locate in "/usr/local/bin", but this path is not available to Apache. (phpinfo -> Apache Environment -> PATH)

Solution is to symlink file to another path:

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

OS X El Capitan Note! /usr/bin/ in OS X 10.11+ is protected. You have to follow this steps:

1. Reboot to Recovery Mode. Reboot and hold "Cmd + R" after start sound.
2. In Recovery Mode go to Utilities -> Terminal.
3. Run: csrutil disable
4. Reboot in Normal Mode.
5. Do the "sudo ln -s /usr/local/bin/gs /usr/bin/gs" in terminal.
6. Do the 1 and 2 step. In terminal enable back csrutil by run: csrutil enable