Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 963
o139 Avatar asked Jun 20 '16 16:06

o139


2 Answers

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
like image 91
o139 Avatar answered Nov 08 '22 11:11

o139


Here is an better way of updating the Apache path variable on OS X which doesn't require symlinking (and therefore no need to disable SIP):

https://www.euperia.com/wrote/solved-php-imagick-unable-to-open-image-pdf/

like image 1
Matt Sims Avatar answered Nov 08 '22 11:11

Matt Sims