2015-07-21 110 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會生成好的。