KnpSnappyBundle PDF生成超过了最长执行时间

时间:2016-01-12 14:33:47

标签: php symfony pdf twig

我试图在Symfony中使用KnpSnappyBundle生成PDF,但每当我尝试执行此操作时,它都会超过PHP中最长60秒的执行时间。

以下是行动:

/**
* @Route("/download-agreement", name="download_agreement")
*/
public function downloadAgreementAction()
{
    $session = new Session();
    $html = $this->renderView('client-representation.html.twig', array(
        'clientAgreementData'  => $session->get("sessionClientAgreementData"),
        "pdfStatus" => true
        ));

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

当我在树枝模板中使用绝对网址时,它似乎也超过了执行时间,例如absolute_url(asset('css/agreement.css'))。如果我使用相对网址,那么css将被忽略并且PDF将生成,但当然我需要样式。

有什么想法吗?

编辑:对于有这个问题的人,使用绝对网址应该在生产服务器上运行;然而,在localhost上,你可能会遇到我遇到的问题。感谢您chalasr

1 个答案:

答案 0 :(得分:1)

  

好的,解决方案实际上非常简单,问题是它不能在开发(本地)环境中工作,因为由于某种原因,wkhtmltopdf不喜欢localhost:8000 ......无论如何。

From this issue(和许多其他人)在laravel包中为这个问题打开了 另一个在捆绑https://github.com/KnpLabs/KnpSnappyBundle/issues/66

我第一次使用KnpSnappyBundle时,我使用了很多替代品,但未成功。

尝试使用包含一个或多个绝对URL的视图(对于Pdf类的所有相关方法都相同)生成pdf时,会发生'超出超时'。

为了处理这个错误,我使用了一个仅用于生成Pdf的特定模板,我将css直接放在<style></style>块中。
像这样,风格被正确应用。

相关问题