12

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.

o139
  • 854
  • 2
  • 8
  • 20
  • And where is this PHP script? Would you show us? – Peter Gordon Jun 20 '16 at 16:17
  • Hope, it's help you: $im = new Imagick(realpath('1.pdf')); – o139 Jun 20 '16 at 16:27
  • show the output of `var_dump(realpath('1.pdf'));` please. – Danack Jun 20 '16 at 19:12
  • @Danak, string(32) "/Users/username/Sites/test/1.pdf" – o139 Jun 20 '16 at 19:48
  • See https://stackoverflow.com/questions/52861946/imagemagick-not-authorized-to-convert-pdf-to-an-image/52863413#52863413. You may need to edit your policy.xml file. Alternately, you may need to put the full path to Ghostscript (gs) in the delegates.xml file for PDF/PS/EPS. – fmw42 Jun 13 '19 at 05:40

2 Answers2

20

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
o139
  • 854
  • 2
  • 8
  • 20
  • I'm stuck at this for the last three hours. Why don't they say "Ghostscript not found"? – Lucas Bustamante Dec 04 '19 at 05:22
  • If you've done this and hit the error `/usr/bin/gs: Read-only file system` then make sure you run `sudo mount -uw /` before you run `sudo ln -s /usr/local/bin/gs /usr/bin/gs` in the steps (https://stackoverflow.com/questions/58480473/how-to-solve-read-only-file-system-problem) – Jason Mayo Aug 19 '20 at 08:47
1

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/

Matt Sims
  • 553
  • 1
  • 5
  • 14
  • That link has changed and is now available at: https://www.euperia.com/wrote/solved-php-imagick-unable-to-open-image-pdf/ – iAugur Nov 29 '19 at 14:25