【问题标题】:Symfony 2 KnpSnappy Bundle ErrorSymfony 2 KnpSnappy 捆绑包错误
【发布时间】:2015-07-21 16:18:37
【问题描述】:

我正在尝试实现 Knp SNappy Bundle 以将 html 页面显示为来自我的控制器的 pdf。 我被这个错误困住了:

The exit status code '1' says something went wrong:
stderr: "Loading pages (1/6)
[> ] 0%
[======> ] 10%
[==========> ] 18%
Warning: Failed to load file:///slick/slick.css (ignore)
Warning: Failed to load file:///slick/slick-theme.css (ignore)
Warning: Failed to load file:///clovis-app/style.css (ignore)
Warning: Failed to load file:///clovis-app/estimate.css (ignore)
Warning: Failed to load file:///clovis-app/res/clovis.png (ignore)
Warning: Failed to load file:///js/collection.js (ignore)
Warning: Failed to load file:///clovis-app/script.js (ignore)
Warning: Failed to load file:///clovis-app/estimate.js (ignore)
[============================================================] 100%
Counting pages (2/6)
[============================================================] Object 1 of 1
Resolving links (4/6)
[============================================================] Object 1 of 1
Loading headers and footers (5/6)
Printing pages (6/6)
[> ] Preparing
[===================> ] Page 1 of 3
[=======================================> ] Page 2 of 3
[============================================================] Page 3 of 3
Done
Exit with code 1 due to network error: ContentOperationNotPermittedError"    
stdout: ""

command: "C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe" --lowquality "C:\Users\Razgort\AppData\Local\Temp\knp_snappy55ae6cc4ad0353.17775155.html" "C:\Users\Razgort\AppData\Local\Temp\knp_snappy55ae6cc4ad41d0.64896673.pdf". 

这是我的配置文件:

knp_snappy:
pdf:
    enabled:    true
    binary:     "\"C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe\""
    options:    []
image:
    enabled:    true
    binary:     "\"C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltoimage.exe\""
    options:    []

这是我渲染视图的方式:

        $html =  $this->renderView('CompanyBundle:Estimate:edit.html.twig', array(
        'estimate' => $estimate,
        'estimateForm'   => $estimateForm->createView(),
        'articleForm' => $articleForm->createView(),
        'articleSelectForm' => $articleSelectForm->createView(),
        'manpowerForm' => $manpowerForm->createView(),
        'manpowerSelectForm' => $manpowerSelectForm->createView(),
        'workSelectForm' => $workSelectForm->createView(),
        'workForm' => $workForm->createView(),
        'bundleForm' => $bundleForm->createView(),
        'bundleSelectForm' => $bundleSelectForm->createView(),
    ));
    return new Response(
        $this->get('knp_snappy.pdf')->getOutputFromHtml($html),
        200,
        array(
            'Content-Type'          => 'application/pdf',
            'Content-Disposition'   => 'attachment; filename="file.pdf"'
        )
    );

我听说 MAMP 有问题,我使用 wamp,没有发现任何问题。 我真的很感激这方面的一些帮助 =)

编辑:

我修改了一些东西,以便首先生成一个 pdf。我做到了。但 pdf 仅包含我的登录页面。然后我添加了对我的会话的访问,它起作用了。但是当我尝试将它作为这样的响应发送时:

        $session = $this->get('session');
    $session->save();
    session_write_close();

    $pageUrl = $this->generateUrl('estimate_preview', array('id' => $id), true);
    return new Response(
        $this->get('knp_snappy.pdf')->getOutput($pageUrl, array('cookie' => array($session->getName() => $session->getId()))),
        200,
        array(
            'Content-Type'          => 'application/pdf',
            'Content-Disposition'   => 'attachment; filename="file.pdf"'
        )
    );

它似乎也不起作用......有人知道为什么吗?

【问题讨论】:

    标签: php symfony wkhtmltopdf


    【解决方案1】:

    尝试使用 forward (getContent()) 方法代替渲染。

    $html =  $this->forward('CompanyBundle:Estimate:edit.html.twig', array(
            'estimate' => $estimate,
            'estimateForm'   => $estimateForm->createView(),
            'articleForm' => $articleForm->createView(),
            'articleSelectForm' => $articleSelectForm->createView(),
            'manpowerForm' => $manpowerForm->createView(),
            'manpowerSelectForm' => $manpowerSelectForm->createView(),
            'workSelectForm' => $workSelectForm->createView(),
            'workForm' => $workForm->createView(),
            'bundleForm' => $bundleForm->createView(),
            'bundleSelectForm' => $bundleSelectForm->createView(),
        ))->getContent();
    
    $pdf =$this->get('knp_snappy.pdf')->getOutputFromHtml($html);
    

    首先通过打印 $pdf 进行检查,然后您可以将其作为响应返回。

    【讨论】:

    • 胡,你的答案。我刚刚测试过,但它似乎不起作用:当我这样做时,我只是得到一个空白页,过了一段时间它会穿上类似的东西的最大时间到达者,然后是另一个空白页。
    【解决方案2】:

    对 Twig 中的所有 CSS 文件执行此操作:

    <link rel="stylesheet" href="{{ app.request.scheme ~'://'~ app.request.httpHost ~ asset('css/my_custom_stylesheet.css') }}" />
    

    CSS 文件夹必须直接在 /web 下

    【讨论】:

      【解决方案3】:

      Knp SNappy Bundle 是一个包装器。在你的交响乐方面你做得很好。

      但是在您的 HTML-to-render 中,您有相对 URL,这对于 HTTP 服务器来说不是绝对的。所以当 wkh​​tmltopdf 在你的 HTML url 中看到“/dsasdaasd/sdaasd.wtf”时,它不知道这是你的网站。

      第一个选项。通过 URL 呈现 PDF(传递一些选项,如“使用打印样式”)。 第二:在您的 HTML 中使用资产的绝对路径。

      无论如何,没有绝对正确的 HTML,因此您将来可能会遇到更多这样的错误——即使在巨大的 HTML 中只有一个未找到的图像。所以尝试在 snappy 中找到一些选项来忽略这些错误,因为结果 pdf 将生成 ok。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-08-21
        • 1970-01-01
        • 2013-09-10
        • 2012-07-05
        • 2018-12-31
        • 2018-02-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多