2015-12-29 142 views
2

我正在使用knp snappy包来从我的应用生成报告。但不工作,当我把我的控制器生成PDF抛出下一个错误:退出状态代码'1'表示出错了[knp snappy]

The exit status code '1' says something went wrong: 
stderr: "Loading pages (1/6) 
[> ] 0% 
[======> ] 10% 
[=======> ] 12% 
Warning: Failed to load file:///gestionresiduospel/web/bundles/usuario/css/bootstrap.css (ignore) 
Warning: Failed to load file:///gestionresiduospel/web/bundles/usuario/js/bootstrap.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 1 
Done 
Exit with code 1 due to network error: ContentNotFoundError 
" 
stdout: "" 
command: "C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe" --lowquality "C:\Users\rodrigo\AppData\Local\Temp\knp_snappy5682878973ade7.09664058.html" "C:\Users\rodrigo\AppData\Local\Temp\knp_snappy56828789746968.46649162.pdf". 

我的控制器是这样的:

public function historicosDetallePdfAction($id) 
    { 
     $em = $this->getDoctrine()->getManager(); 
     //gets residuos peligrosos. 
     $solicitudRetiro= $em->getRepository('SolicitudRetiroBundle:SolicitudRetiro')->find($id); 
     //gets residuos no peligrosos 
     $residuosPRetirados = $em->getRepository('SolicitudRetiroBundle:RetiroResiduo')->findResiduos($id); 
     //retorna los residuos no peligrosos en la solicitud 
     $residuosNPRetirados = $em->getRepository('SolicitudRetiroBundle:RetiroResiduoNoPeligroso')->findResiduosNoPeligrosos($id); 
     // view generator 
     $html = $this->renderView('SolicitudRetiroBundle:Pdf:resumenSolicitudResiduoRetirado.html.twig', array(
      'solicitudRetiro' => $solicitudRetiro, 
      'residuosRetirados' => $residuosPRetirados, 
      'residuosNoPel' => $residuosNPRetirados)); 
     // pdf generator 
     return new Response(
      $this->get('knp_snappy.pdf')->getOutputFromHtml($html), 
      200, 
      array(
       'Content-Type' => 'application/pdf', 
       'Content-Disposition' => 'attachment; filename="historico.pdf"' 
       ) 
      ); 
    } 

我复制了我的网址,并运行到控制台[CMD]与wkhtmltopdf.exe。在控制台中,我创建了pdf,但是当我看到该文件只显示我的登录屏幕。所以我认为可能是一个关于权限的问题进入我的控制器,但我不知道我是如何通过这个。另一方面,如果你知道另一个生成symfony的pdf生成器(来自html),这也可以帮助我。

我下symfony2.7

回答

4

工作这意味着的是,在你的HTML文件中的一个资源不能被wkhtmltopdf加载,请检查你的HTML文件,如果所有的资源都绝对URL这样:http://www.yourserver.com/images/myimage.jpg

如果不是的情况下尝试设置它:

{{ app.request.schemeAndHttpHost ~ 'resource relative url' }} 

如果您加载与assetic资产{% stylesheets %}{% javascript %}{% image %}你也可以做

{{ app.request.schemeAndHttpHost ~ asset_url }} 
+0

谢谢。现在它工作了! –

+0

我和wkhtmltopdfin laravel 5有同样的问题和错误信息,这就是问题所在。我拥有抛弃错误的CDN资产。一旦我使用本地版本,PDF生成按预期运行。 – RoboBear

0

我有两个样的问题在这里一次:

第一个是因为KPN活泼尝试转换* .js文件时失败。所以我必须在我的情况下带* .js文件。

第二个是因为路径。我不得不使用绝对文件系统路径。通过这个,我的意思是,这样的:/home/ubuntu/workspace/bootstrap/css/bootstrap.css这用于* html.twig文件中使用的href标记,然后通过sn​​appy转换为pdf。

通过这样做,我使用了一些树枝全局变量。

twig: 
    form: 
     resources: 
      - 'MyBundle:Form:fields.html.twig' 
      - 'MyUploaderBundle:Form:fields.html.twig' 
    debug:   "%kernel.debug%" 
    strict_variables: "%kernel.debug%" 
    globals: 
     pathToWeb: "%kernel.root_dir%/../web" 
     bootstrapCss: "/home/ubuntu/workspace/MyApp/web/bootstrap/css/bootstrap.css" 
     mainCss: "/home/ubuntu/workspace/MyApp/web/css/main.css" 
     layoutCss: "/home/ubuntu/workspace/MyApp/web/css/layout.css" 

在树枝

<link rel="stylesheet" type="text/css" href="{{ bootstrapCss }}"> 
<link rel="stylesheet" type="text/css" href="{{ layoutCss }}"> 
<link rel="stylesheet" type="text/css" href="{{ mainCss }}"> 
0

可以使用absolute_url()树枝功能{{ absolute_url(asset('assets/image.jpg')) }}