【问题标题】:PHP and Apache : shell_exec(wkhtmltopdf with xvfb) command doesn't workPHP 和 Apache:shell_exec(wkhtmltopdf with xvfb) 命令不起作用
【发布时间】:2012-09-18 18:12:34
【问题描述】:

我尝试在 apache (2.4.2) 上使用 php (5.4.2) 运行 wkhtmltopdf (0.11.0 rc1)。 当我尝试启动 wkhtmltopdf-i386 --use-xserver http://www.google.com google.pdf 2>&1 时,我可以找到我的 pdf。这是我的php代码

<?php
    $cmd= '/usr/bin/wkhtmltopdf-i386 http://www.google.com google.pdf 2>&1';
    $ret = shell_exec($cmd);
    echo $ret;
?>

它适用于 apache 和命令行 php test.php。 因为我的目标页面包含许多图像和一些“沉重”的 js 图表。当我尝试将 wkhtmltopdf 命令转换为 pdf 时,我遇到了分段错误。 使它工作的唯一方法是使用 xvfb 作为 X11 模拟器。代码如下所示:

<?php
    $cmd= '/usr/bin/xvfb-run  /usr/bin/wkhtmltopdf-i386 --use-xserver http://www.google.com google.pdf 2>&1';
    $ret = shell_exec($cmd);
    echo $ret;
?>

此脚本适用于命令行php test.php,但不适用于 apache。如果我用htop查看apache的进程,我可以看到有两个进程(php test.php):

  • xvfb
  • wkhtmltopdf

当我使用 apache 启动时,我只有 xvfb 进程。它在 apache 超时后结束,因为它正在等待 wkhtmltopdf 进程。

我可以让它与 apache (2.2.21) 和 php (5.3.10) 一起工作。

有什么我想念的吗?也许在 apache 的配置文件中有些东西?

【问题讨论】:

    标签: php apache wkhtmltopdf shell-exec xvfb


    【解决方案1】:

    我遇到了同样的问题。我使用的是 exec 函数,但同样适用于 shell_exec。函数执行在 php.ini 中被禁用。

    解决方案:从 php.ini 文件的 disable_functions 中删除 shell_exec 字符串。

    【讨论】:

      【解决方案2】:

      我不确定为什么你的第二个版本不能从 Apache 调用(一定不能使用相同的 shell,因为 shell_exec 使用 shell?),但作为一种解决方法,你可以(来自 Apache PHP)@987654323 @ 并得到你想要的结果?

      也许还可以尝试其他流程执行功能之一,例如pcntl_exec

      【讨论】:

      • 我试过这样做,但我仍然只启动了 xvfb 进程。
      【解决方案3】:

      主要是因为所有权和权限,试试

      su www-data (for debian)
      php test.php
      

      您可能会看到错误。

      【讨论】:

      • 已经试过了。它与 sudo -u www-data php test.php 完美配合
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-12
      • 2013-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多