【问题标题】:Execute wkhtmltopdf from PHP从 PHP 执行 wkhtmltopdf
【发布时间】:2011-05-27 14:12:50
【问题描述】:

我在 Linux 命令行中可以正常工作:

wkhtmltopdf entry.html output.pdf

但以下内容不适用于 PHP 代码:

exec('wkhtmltopdf entry.html output.pdf');

有趣的是,我搜索了很多未经检查的解决方案,但没有解释为什么这是一个问题。 谢谢,如果你有好的。

【问题讨论】:

  • 您可能需要指定可执行文件的绝对路径,例如exec(/usr/bin/wkhtmltopdf ...),如果 PHP 调用的子 shell 最终具有与标准默认 shell 不同的 PATH 设置。同样,如果您在基于 Web 的脚本中执行 exec,那么权限也将是一个因素。
  • 您是否收到任何 PHP 错误消息?直接在 shell 中输入会发生什么?
  • 查看apache错误日志,或许你会在那里找到答案(像我一样)

标签: php wkhtmltopdf html2pdf


【解决方案1】:

有同样的问题,我认为其他人不应该浪费 > 3 小时:

解决方案在这里:wkhtmltopdf error in apache log

您只需要安装 xvfp “模拟 x 环境”

exec("xvfb-run -a wkhtmltopdf test.html output.pdf")

【讨论】:

    【解决方案2】:

    wkhtmltopdf 有bindings,其中之一是用于 PHP。你可以试一试。

    【讨论】:

    • 有趣,刚刚浏览过。但是,该命令对我来说最方便,因为我想即时进行转换。那么,为什么exec调用没有被执行?
    【解决方案3】:

    这里是 wkhtmltopdf 的 PHP 包装器 http://mikehaertl.github.com/phpwkhtmltopdf/ 很简单

    【讨论】:

      【解决方案4】:

      也许wkhtmltopdf 不在 www-data 的 PATH 变量中。

      whereis wkhtmltopdf
      

      会告诉你二进制文件的位置;二进制文件通常驻留在 *nix 机器上的 /usr/bin/... 中。然后在你的命令中用 e.g. /usr/bin/wkhtmltopdf 替换 wkhtmltopdf

      /usr/bin/wkhtmltopdf entry.html output.pdf
      

      【讨论】:

      • type -P 'wkhtmltopdf'whereis 更可靠。
      • 我也试过这个:$input = getcwd()。 "/entry.html"; $输出 = getcwd() 。 "/输出.pdf"; exec ('/usr/bin/wkhtmltopdf $entry $output');但结果相同。
      • @user238831,然后尝试getcwd() (function.getcwd) 验证脚本是否在正确的目录中执行。 -- 看看getcwd() 返回了什么。
      • @Alix Axel -- 请详细说明,或提供来源。
      • @user238831 -- 当然exec 语句会被执行,如果你没有注释掉的话。使用$output = passthru('/usr/local/bin/wkhtml2pdf /tmp/foo.html /bar/foo.pdf'); echo $output; 这样你就可以看到wkhtml2pdf 的命令行执行是否有任何问题。
      【解决方案5】:

      刚刚遇到这个问题 - 就我而言,解决方案很简单:我没有意识到 PHP 处于安全模式。关闭安全模式,它工作正常!

      【讨论】:

        【解决方案6】:

        我也在为同样的问题苦苦挣扎。

        我在带有PHP 5.4Windows 2008 R2 服务器上的解决方案:

        exec('C:\inetpub\wwwroot\mywebsite\subdir\wkhtmltopdf input.html output.pdf');
        

        就是这样(在超过 5 小时的网络搜索包括这个)一个名为 output.txt 的新文件,将其重命名为 output.pdf 并授予用户“每个人”的权限。

        这些是我的试训:

          exec(C:\inetpub\wwwroot\mywebsite\wkhtmltopdf );
          echo(exec(wkhtmltopdf.exe cache.html output.pdf));
          exec("xvfb-run -a wkhtmltopdf test.html output.pdf")
          $execute = "xvfb-run -a wkhtmltopdf cache.html output.pdf";
          $out = shell_exec("/path/to/wkhtmlto­pdf --version"); echo($out); 
          $out = passthru('/usr/local/bin/wkhtml2pdf
        

        希望这些对其他人有用

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-04-05
          • 1970-01-01
          • 2014-04-28
          • 2023-03-18
          • 2014-11-04
          • 1970-01-01
          • 2012-10-03
          相关资源
          最近更新 更多