2015-07-21 112 views
0

我米试图实现KNP活泼的捆绑,以显示一个HTML页面,从我的控制器的PDF文件。 我坚持这个错误:Symfony的2 KnpSnappy捆绑错误

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"' 
     ) 
    ); 

我EARD有问题在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"' 
     ) 
    ); 

它似乎不工作或者...有人有一个想法,为什么?

回答

0

这样做对你的枝杈所有的CSS文件:

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

的CSS文件夹必须是直属/网络

+0

THX的意见 – Razgort

0

尝试使用前(的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进行检查,然后您可以将其作为回复返回。

+0

胡,TY你的答案。我只是测试过,但它dooesnt似乎工作:当我这样做时,我只是得到一个空白页面,一段时间后,它最大的时间重新找到类似的东西,然后另一个空白页。 – Razgort

0

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

但在你的HTML渲染的你有相对URL,这也不是绝对的,以HTTP服务器。所以当wkhtmltopdf在你的HTML网址中看到'/dsasdaasd/sdaasd.wtf'时,它不知道它是你的网站。

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

反正没有绝对正确的HTML,所以你可以在未来类似这样的多个错误满足 - 即使在巨大的HTML会有唯一一个没有发现的图像。因此,尽量找到一些可以忽略这些错误的选项,因为在结果中pdf会生成好的。