【问题标题】:Imagick don't open pdf files with "Failed to read the file" exceptionImagick 不打开带有“无法读取文件”异常的 pdf 文件
【发布时间】:2016-10-21 23:42:48
【问题描述】:

尝试从 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 脚本在哪里?你能给我们看看吗?
  • 希望对您有所帮助:$im = new Imagick(realpath('1.pdf'));
  • 请显示var_dump(realpath('1.pdf'));的输出。
  • @Danak, string(32) "/Users/username/Sites/test/1.pdf"
  • stackoverflow.com/questions/52861946/…。您可能需要编辑您的 policy.xml 文件。或者,您可能需要将 Ghostscript (gs) 的完整路径放在 PDF/PS/EPS 的 delegates.xml 文件中。

标签: php pdf imagemagick imagick php-7


【解决方案1】:

这个问题与 ghostscript 的路径有关。它位于“/usr/local/bin”中,但该路径对 Apache 不可用。 (phpinfo -> Apache 环境 -> PATH)

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

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

OS X El Capitan 注意! OS X 10.11+ 中的 /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”?
  • 如果您已完成此操作并遇到错误 /usr/bin/gs: Read-only file system,请确保在步骤中运行 sudo ln -s /usr/local/bin/gs /usr/bin/gs 之前运行 sudo mount -uw / (stackoverflow.com/questions/58480473/…)
【解决方案2】:

这是在 OS X 上更新 Apache 路径变量的更好方法,不需要符号链接(因此不需要禁用 SIP):

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-16
  • 1970-01-01
  • 2011-11-19
  • 1970-01-01
  • 2021-09-19
  • 1970-01-01
相关资源
最近更新 更多