Imagick无法打开PDF文件,抛出“Failed to read the file”异常。

12

尝试从PHP脚本打开PDF文件时出现错误:

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

我正在运行OS X,Brew,PHP 7.0,php70-imagick --HEAD,ImageMagick --with-ghostscript,GhostScript

命令行测试正常运作:

convert 1.pdf 1.jpg

文件权限正常。尝试使用realpath函数同时测试URL和本地文件的路径,因此文件路径也正确。如果打开jpeg,则脚本运行良好。

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

更新:问题已解决。请看下面的解决方案。


这个 PHP 脚本在哪里?你能展示给我们看吗? - Peter Gordon
希望这能帮到你:$im = new Imagick(realpath('1.pdf')); - o139
请展示 var_dump(realpath('1.pdf')); 的输出。 - Danack
@Danak,字符串(32) "/Users/username/Sites/test/1.pdf" - o139
请参考 https://dev59.com/pVQJ5IYBdhLWcg3w5aIc#52863413。您可能需要编辑 policy.xml 文件。或者,您可能需要在 delegates.xml 文件中为 PDF/PS/EPS 放置 Ghostscript (gs) 的完整路径。 - fmw42
2个回答

20

这个问题出在Ghostscript的路径上。它位于"/usr/local/bin",但该路径对Apache不可用。(phpinfo -> Apache环境 -> PATH)

解决方法是将文件进行符号链接到另一个路径:

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

注意!OS X El Capitan中,/usr/bin目录是受保护的。您需要按照以下步骤进行操作:

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

我已经卡在这里三个小时了。为什么他们不说“找不到Ghostscript”? - Lucas Bustamante
如果您执行了此操作并遇到错误“/usr/bin/gs: 只读文件系统”,请确保在执行步骤(https://stackoverflow.com/questions/58480473/how-to-solve-read-only-file-system-problem)中运行“sudo ln -s /usr/local/bin/gs /usr/bin/gs”之前先运行“sudo mount -uw /”。 - Jason Mayo

1

该链接已更改,现在可在以下网址找到: https://www.euperia.com/wrote/solved-php-imagick-unable-to-open-image-pdf/ - iAugur

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接