错误! Symfony2与KNP Snappy Bundle& WKHTML2PDF

时间:2012-10-29 17:13:43

标签: php symfony runtime wkhtmltopdf

当我转到/fileDownload时,我收到500内部服务器错误 - RuntimeException:

  

由于“0”信号,该过程停止。

控制器操作:

public function fileAction()
{
    $html = $this->render('MyBundle:Downloads:file.html.twig', array(
    'fileNumber'  => '1234'
    ));

    return new Response(
        $this->get('knp_snappy.pdf')->getOutputFromHtml($html),
        200,
        array(
            'Content-Type'          => 'application/pdf',
            'Content-Disposition'   => 'attachment; filename="file.pdf"'
        )
    );
}

我已经为WKHTMLTOPDF使用了终端命令,并且它已经成功生成了PDF。它只适用于Symfony2应用程序。

在我的config.yml中:

knp_snappy:
    pdf:
        enabled:    true
        binary:     /usr/local/bin/wkhtmltopdf
        options:    []

5 个答案:

答案 0 :(得分:0)

我假设Symfony在某个阶段发布了一个exec()。您需要获得返回的确切命令行错误。它在终端会话中适用于您的事实并不一定意味着它将在其他用户/进程运行时工作。

检查wkhtmltopdf上的权限,即apache或运行Web服务器的任何人都有权运行该命令。

另外,请在wkhtmltopdf: cannot connect to X server以及此处的第一篇文章中查看此问题:http://geekisland.org/index.php?m=05&y=11&entry=entry110518-114630

X Server需要运行wkhtmltopdf的某些版本,并且在通过cron或在apache进程中运行时不存在。如果是这种情况,您需要在上面的第一个链接中使用bash包装器。

答案 1 :(得分:0)

确保wkhtmltopdf确实位于您指定的文件夹中,并具有正确的权限:/ usr / local / bin / wkhtmltopdf

我成功地将KNP Snappy Bundle与Symfony 2.0一起使用,在生成html时尝试使用RenderView而不是渲染(检查我的代码是否正常工作):

控制器:

$html = $this->renderView('YOPYourOwnPoetBundle:thePoet:poemPDF.html.twig', array(
                'poem'  => $customizedPoem,
                'fontType' => $fontType,
                'fontSize' => $formData['fontSize'],
                'fontWeight' => $fontWeight,
                'fontStyle' => $fontStyle,
            ));
return new Response(
                $this->get('knp_snappy.pdf')->getOutputFromHtml($html),
                200,
                array(
                    'Content-Type'          => 'application/pdf',
                    'Content-Disposition'   => 'attachment; filename="'.$session->get('poemTitle').'.pdf"',
                )
            );

答案 2 :(得分:0)

我有同样的问题,我终于解决了,我的建议是,如果您从操作系统回购安装了wkhtmltopdf lib,然后将其删除,并从谷歌代码网站下载静态版本,而不是使用版本11rc,使用版本9静态库,它是与我合作的那个。

http://code.google.com/p/wkhtmltopdf/downloads/list

答案 3 :(得分:0)

看起来selinux正在阻止你的命令,我有同样的问题,但是我通过禁用SELinux解决了它,现在我能够使用wkhtmltopdf + php生成pdf文件。

exec("/usr/local/bin/wkhtmltopdf http://www.google.com /var/www/html/google.pdf");

要禁用selinux run setenforce 0,  再试一次,看看它是否有效。 使用setenforce 1

重新启用它

但是禁用selinux不是最好的解决方案,你需要使用audit2allow来扩展它,它会自动创建一个自定义策略模块来解决这个问题, 首先安装audit2allo(如果尚未安装)

yum -y install policycoreutils-python
grep httpd_t /var/log/audit/audit.log | audit2allow -m httpdlocal > httpd.te
checkmodule -M -m -o httpdlocal.mod httpd.te
semodule_package -o httpdlocal.pp -m httpdlocal.mod
semodule -i httpdlocal.pp

再试一次,看看它是否有效

答案 4 :(得分:0)

我在wkhtmltopdf的相同场景中遇到了同样的问题。我摆脱了以下命令:

setsebool httpd_execmem