2015-12-29 145 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')) }}