2013-11-10 52 views
1

我正在使用KNP Snappy和Symfony2。當我使用從控制器我得到以下響應的響應:控制器的Symfony2 - 使用KNP生成PDF從控制器中快速生成404

The exit status code '2' says something went wrong: 
stderr: "Loading pages (1/6) 
[> ] 0% 
[======> ] 10% 
[============================> ] 48% 
[============================================================] 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 2 due to http error: 404 Page not found 
" 
stdout: "" 
command: /usr/bin/wkhtmltopdf --lowquality --no-outline  '/tmp/knp_snappy527f3354ad21d2.78976975.html' '/tmp/knp_snappy527f3354ad6e37.45993260.pdf'. 

部分看起來是這樣的:產生

$html = $this->renderView('MyBundle:Visit:print.html.twig', array(
     'visit' => $visit, 
     'themes' => $themes 
    )); 

    return new Response(
     $this->get('knp_snappy.pdf')->getOutputFromHtml($html), 
     200, 
     array(
      'Content-Type'   => 'application/pdf', 
      'Content-Disposition' => 'attachment; filename="file.pdf"' 
     ) 
    ); 

的文件,它看起來很好。但是,迴應是錯誤的。

有誰知道問題可能是什麼?

+0

嘗試,而無需創建PDF,如果你得到任何錯誤檢查返回渲染的模板 - 缺少變量,語法錯誤,... – nifr

回答

2

這是一個已知的問題,通常發生在資源缺少,即使頁面本身被發現,返回200據報道here - 關於它的超級可怕的是,它並不總是 2時東西是404.這就是爲什麼我(並且經常看到其他人)將退出代碼2和0視爲相同的原因。

我也開始an issue以獲得記錄的退出代碼,但Antialize和其他人沒有時間更新項目。 (雖然有分叉和0.12的傳言,但它們並不是真正無用的。)

因此,我要檢查的第一件事是,是否有任何可能導致404(或實際上不是2xx的任何其他內容)的內容這一頁。像缺少的CSS,JavaScript或圖像文件。

+0

是的,這事實確實如此!大!問題在於樣式表沒有被加載,因爲我設置了路徑錯誤。謝謝你的幫助。 – apfz

-1

將symfony2加載到發送到pdf的樹枝文件的資源似乎有問題。爲了解決這個問題,添加所有的資產在樹枝文件中像這樣的:

{{ app.request.scheme ~'://' ~ app.request.httpHost ~ asset('MY_ASSET') }}