Symfony 2 KnpSnappy Bundle Error

时间:2015-07-21 16:18:38

标签: php symfony wkhtmltopdf

我正在尝试实现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"'
        )
    );

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

2 个答案:

答案 0 :(得分:0)

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

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

CSS文件夹必须直接位于/ web

答案 1 :(得分:0)

尝试使用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然后你可以将其作为回复返回。