2015-03-13 30 views
0

我是一個begginer,我想從一個樹枝視圖生成pdf。我安裝了knpsnappy,然後我不知道該怎麼做。我創建了一個新的控制器,我在github上講了什麼。使用knp snappy bundle生成pdf symfony2

應用程序/配置/ config.yml

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: [] 

我在AppKernel正確添加此行:

new Knp\Bundle\SnappyBundle\KnpSnappyBundle(), 

SA \ UserBundle \控制器\ PdfController

<?php 
namespace SA\UserBundle\Controller; 
use Symfony\Component\HttpFoundation\Response; 
use Symfony\Bundle\FrameworkBundle\Controller\Controller; 

class PdfController extends Controller 
{ 
    public function generateAction() 
    { 
     $html = $this->renderView('FOSUserBundle:Profile:show.html.twig', 
      array('form' => $form->createView(), 
     )); 

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

資源\ config \ routing.yml

user_folder: 
    defaults: { _controller: SAUserBundle:Pdf:generate } 
    path: /profile 

,我加入這一行我樹枝: 的src \ SA \ UserBundle \資源\意見\資料\ show.html.twig

<form action=" {{ path('user_folder') }} "><input type="submit" value="PDF" onClick=" {{ path('user_folder') }} "></form> 

沒有錯誤,但是當我按一下按鈕當然,這是行不通的。也許我不明白如何使用這個軟件包。我需要你的幫助!

回答

0

您的控制器和配置看起來沒問題。嘗試使用一個鏈接,而不是形式的測試PDF生成:

<a href="{{ path('user_folder') }}">Click</a> 

,並根據您使用

+0

感謝您的快速回復。我已經嘗試過了。它不起作用:/ – peanut 2015-03-13 15:02:15

+0

什麼是日誌中的錯誤?使用app_dev.php以開發模式訪問頁面,並檢查app/logs/dev.log文件是否有任何錯誤。 – d23 2015-03-14 16:50:39

0

什麼樣的環境目標是有其他的檢查應用程序/ dev.log或應用程序/ prod.log包含你的var的內容的視圖。

pdf一般是隨數據庫中的內容而來的。因此,您必須獲取用戶的內容,並創建第二個視圖調用「content_user.html.twig」,在其中包含用戶的內容,然後生成此視圖。

你的控制器:

<?php 
namespace SA\UserBundle\Controller; 
use Symfony\Component\HttpFoundation\Response; 
use Symfony\Bundle\FrameworkBundle\Controller\Controller; 

class PdfController extends Controller 
{ 
    public function generateAction() 
    { 
     $html = $this->renderView('FOSUserBundle:Profile:content_user.html.twig', 
      array('user' => $this->getUser(), 
     )); 

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

而且在content_user.html.twig,您在樹枝管理您的數據:

Username : {{user.username}} 
Firstname: {{user.firstname}} 
相關問題