2016-11-28 81 views
-1

我跟着關於knpsnappybundle在symfony3中使用pdf的教程,但問題是我需要傳遞一個值的數組到PDF頁面,所以我可以得到像原始的一個完整的頁面無法生成PDF Symfony 3

這裏是控制器:

public function pdfAction(){ 
    $html = $this->renderView('@Recommendation/Recommendation/payment-successful.html.twig'); 

    $filename = sprintf('test.pdf', date('Y-m-d')); 

    return new Response(
     $this->get('knp_snappy.pdf')->getOutputFromHtml($html), 
     200, 
     [ 
      'Content-Type'  => 'application/pdf', 
      'Content-Disposition' => sprintf('inline; filename="%s"', $filename), 

     ] 
    ); 

} 

這裏與打印按鈕的代碼正常的頁面的圖像:

<a href="{{ path('payment_pdf') }}"><button>PRINT</button></a> 

和路由:

payment_pdf: 
path:  /payment_pdf 
defaults: { _controller: "RecommendationBundle:Recommendation:pdf"} 

一個我原來的頁面的圖片:現在生成的PDF格式的圖片 https://i.stack.imgur.com/3MBEt.png

和: https://i.stack.imgur.com/rqs2x.png

看到該表是空的!

恕無法顯示圖像

樹枝文件:

<head> 
 
    <script type="text/javascript" src="http://yourjavascript.com/12191816011/success.js"></script> 
 
    <link rel="shortcut icon" href="http://www.iconj.com/ico/f/k/fksvrhslsa.ico" type="image/x-icon" /> 
 
    <script type="text/javascript" src="http://yourjavascript.com/11396113712/jquery-ajax-deprecated-dimensions-effects-offset-wrap-min.js"></script> 
 

 
    <link href="https://dl.dropboxusercontent.com/s/6dgxerbf1p6p8zl/styles.css" rel="stylesheet" type="text/css"> 
 
</head> 
 

 
<html> 
 

 

 
<div class="pf-checkout-container pf-success-page"> 
 
    <h1 class="pf-green">Payment Successful</h1> 
 
    <p class="pf-processed-msg">Your payment has been processed! Details of the transaction are included below:</p> 
 
    <div class="pf-total-col"> 
 
     <span class="pf-total-paid">Amount Paid</span> 
 
     <div class="pf-total-wrapper"> 
 
      <span class="pf-total-label">Total:</span> 
 
      <span class="pf-total-amount pf-green" id="pf-total-amount">$00.00</span> 
 
     </div> 
 
    </div> 
 

 
    <div class="pf-success-details"> 
 
     <p>Thank you for your purchase. Be sure to check us out on Facebook & Twitter!</p> 
 
     <div class="pf-success-info"> 
 
      <ul> 
 
       <li> 
 
        <span class="pf-value-name">Name:</span> 
 
        <span id="pf-full-name" class="pf-value">{{ app.user.username }}</span> 
 
       </li> 
 

 
       <li> 
 
        <span class="pf-value-name">Auth Code:</span> 
 
        <span class="pf-value">{{app.user.salt}}</span> 
 
       </li> 
 
      </ul> 
 
      <br> 
 
      <br> 
 
      <table width="100%" border="1" cellpadding="10" style="font-family:'Myriad Pro' "> 
 

 
       <tr> 
 
        <th width="43%" align="left">Product Name</th> 
 
        <th width="10%">Unit Price</th> 
 
        <th width="10%">Quantity</th> 
 
        <th width="12%">Total</th> 
 
       </tr> 
 

 
       {% if livres is defined %} 
 
        {% for l in livres %} 
 
         <tr bgcolor="#FFFFFF" class=" product-detail"> 
 
          <td valign="top">{{ l.titre }}</td> 
 
          <td align="center" valign="top">{{ l.prix }} DT</td> 
 
          <td align="center" valign="top">{{ l.quantite}}</td> 
 
          <td align="center" valign="top">{{ l.prix}} DT</td> 
 
         </tr> 
 

 

 
        {% endfor %} 
 
       {% endif %} 
 

 
      </table> 
 
     </div> 
 
     <!-- | PUT YOUR COMPANY LOGO HERE | --> 
 

 
     <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> 
 
    </div> 
 
    <div class="pf-payment-buttons"> 
 
     <!-- | PUT YOUR LINK HERE | --> 
 
     <button>Back to Store</button> 
 

 
     <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> 
 
    </div> 
 

 
    <a href="{{ path('payment_pdf') }}"><button>PRINT</button></a> 
 

 
    <div class="pf-footer"> 
 

 
      <img src="https://stripe.com/img/v3/home/social.png" /> 
 

 
    </div> 
 
</div> 
 
</html>

我的全部控制器的文件:

class RecommendationController extends Controller 

{

public function afficheAction() 
{ 


    $em = $this->getDoctrine()->getManager(); 
    $recommendation = $em->getRepository("MainBundle:Recommendation")->findAll(); 
    return $this->render("RecommendationBundle:Recommendation:book_detail.html.twig", array('recom' => $recommendation)); 
} 

public function afficheparidAction($id) 
{ 
    $em = $this->getDoctrine()->getManager(); 
    $em1 = $this->getDoctrine()->getManager(); 


    $livre = $em->getRepository('MainBundle:Livre')->find($id); 
    $recommendation = $em1->getRepository('MainBundle:Recommendation')->findAll(); 


    return $this->render('RecommendationBundle:Recommendation:book_detail.html.twig', array(
     'livre' => $livre, 'recom' => $recommendation 
    )); 
} 


public function AjoutAction($id) 
{ 
    $user = $this->getUser(); 
    $em = $this->getDoctrine()->getManager(); 
    $livre = $em->getRepository('MainBundle:Livre')->find($id); 
    $rec = new Recommendation(); 
    if (isset($_POST['review'])) { 
     $rec->setIdUtilisateur($user); 
     $rec->setContenu($_POST['review']); 
     $rec->setIdLivre($livre); 
     $em->persist($rec); 
     $em->flush(); 

// return $ this-> redirectToRoute(「recommendation_bookid」); return $ this-> redirect($ this-> generateUrl('recommendation_new',array('id'=> $ id)));

} 
    $em1 = $this->getDoctrine()->getManager(); 
    $recommendation = $em1->getRepository('MainBundle:Recommendation')->findAll(); 
    return $this->render("RecommendationBundle:Recommendation:new_rec.html.twig", array('livre' => $livre, 'id' => $id, 'recom' => $recommendation)); 


} 


public function DeleteAction($id_r, $id_l) 
{ 
    $em = $this->getDoctrine()->getManager(); 
    $rec = $em->getRepository('MainBundle:Recommendation')->find($id_r); 
    $livre = $em->getRepository('MainBundle:Livre')->find($id_l); 
    $em->remove($rec); 
    $em->flush(); 
    $em1 = $this->getDoctrine()->getManager(); 

    return $this->redirect($this->generateUrl('recommendation_new', array('livre' => $livre, 'id' => $livre->getId()))); 
} 


public function UpdateAction($id_r,$id_l) 
{ 
    $user = $this->getUser(); 
    $em = $this->getDoctrine()->getManager(); 
    $rec = $em->getRepository('MainBundle:Recommendation')->find($id_r); 
    $em->remove($rec); 
    $em->flush(); 
    $livre = $em->getRepository('MainBundle:Livre')->find($id_l); 


    if (isset($_POST['review'])) { 
     $rec->setId($id_r); 
     $rec->setIdUtilisateur($user); 
     $rec->setContenu($_POST['review']); 
     $rec->setIdLivre($livre); 
     $em->persist($rec); 
     $em->flush(); 

     return $this->redirect($this->generateUrl('recommendation_new', array('id' => $id_l))); 
} 
} 


public function FindAction($id_l,$id_r) 
{ 

    $em = $this->getDoctrine()->getManager(); 
    $em1 = $this->getDoctrine()->getManager(); 


    $rec = $em->getRepository('MainBundle:Recommendation')->find($id_r); 
    $livre = $em1->getRepository('MainBundle:Livre')->find($id_l); 


    return $this->render('RecommendationBundle:Recommendation:edit_rec.html.twig', array('rec' => $rec, 'livre' =>$livre)); 

} 

public function bookAction() 
{ 
    return $this->render('RecommendationBundle:Recommendation:book_detail.html.twig'); 
} 




public function cartAction($id) 
{if (!isset($_SESSION['cart'])){ 
    $_SESSION['cart']=array();} 

    $em = $this->getDoctrine()->getManager(); 
    $livre = $em->getRepository('MainBundle:Livre')->find($id); 
    // if (isset($_SESSION['book'])) { 
     $_SESSION['cart'][$id] = $livre; 


    $cart=$_SESSION['cart']; 

返回$這 - >渲染( 'RecommendationBundle:建議:cart.html.twig',陣列( 'ID'=> $ ID, '裏弗'=> $購物車));

} 

public function emptycartAction() 
{ 
    //if (isset($_SESSION['cart'])) { 
    $_SESSION['cart'] = array(); 
    $cart=$_SESSION['cart']; 


    return $this->render('RecommendationBundle:Recommendation:cart.html.twig',array('livres'=>$cart)); 

}

public function removebookcartAction($id) 
{ 
    // {if (isset($_SESSION['cart'])){ 


    unset($_SESSION['cart'][$id]); 
    $cart=$_SESSION['cart']; 
    // return $this->redirect($this->generateUrl('recommendation_cart_affiche', array('livres' => $cart))); 

返回$這 - > redirectToRoute( 'recommendation_cart_show',陣列( '裏弗'=> $購物車)); //返回$ this-> render('RecommendationBundle:建議:cart.html。數組('id'=> 1,'livres'=> $ cart));

} 

public function showAction(){ 
    // if(!empty($_SESSION['cart'])) 
    $livres=$_SESSION['cart']; 
    return $this->render('RecommendationBundle:Recommendation:cart.html.twig',array('livres'=>$livres)); 


} 
public function okAction($cart){ 
    $cart=$_SESSION['cart']; 


    return $this->render('RecommendationBundle:Recommendation:payment-successful.html.twig',array('livres'=>$cart)); 

}

public function pdfAction($cart){ 
    // $cart=$_SESSION['cart']; 
    $html = $this->renderView('@Recommendation/Recommendation/payment_pdf.html.twig',array('livres'=>$cart)); 

    $filename = sprintf('test.pdf', date('Y-m-d')); 

    return new Response(
     $this->get('knp_snappy.pdf')->getOutputFromHtml($html), 
     200, 
     [ 
      'Content-Type'  => 'application/pdf', 
      'Content-Disposition' => sprintf('inline; filename="%s"', $filename), 
     ] 
    ); 

} 

我的路線:

recommendation_bookid: 路徑:/ book_detail/{ID} 默認:{_controller: 「RecommendationBundle:建議:afficheparid」} recommendation_book: path:/ book_detail 默認值:{_controller:「RecommendationBundle:Recom mendation:企業公告」} recommendation_new: 路徑:/ book_detail/{ID} /新 默認:{_controller: 「RecommendationBundle:建議:Ajout」} recommendation_delete: 路徑:/ book_detail/{id_l}/{id_r} /刪除 默認值:{_controller: 「RecommendationBundle:建議:刪除」} recommendation_edit: 路徑:/ book_detail/{id_l}/{id_r} /編輯 默認值:{_controller: 「RecommendationBundle:建議:更新」} recommendation_find: path:/ book_detail/{id_l}/{id_r}/find 默認值:{_controller:「RecommendationBundle:Recommendation:Find」} recommended_cart: 路徑:/ book_detail/{ID} /購物車 默認:{_controller: 「RecommendationBundle:建議:購物車」} recommendation_empty: 路徑:/ emptycart 默認:{_controller: 「RecommendationBundle:建議:emptycart」} recommendation_cart_remove: 路徑:/ remove_book/{ID} 默認:{_controller: 「RecommendationBundle:建議:removebookcart」} recommendation_cart_show: 路徑:/ remove_book 默認:{_controller: 「RecommendationBundle:建議:節目」} payment_ok: 路徑:/ payment_success 默認值:{_controller:「RecommendationBundle:Recommendati上:OK「}

payment_pdf: 路徑:/ payment_pdf 默認值:{_controller: 」RecommendationBundle:建議:PDF「}

+0

單擊'PRINT'顯示什麼? –

+0

它顯示了pdf頁面 它將我帶到路線payment_pdf,它具有產生如上所述的pdf頁面的動作 –

+0

您可以在做出更改之前和之後顯示您的樹枝文件'payment-successful.html.twig'。或者,如果你沒有舊的那個,就是現在的那個。 –

回答

0

好吧,我想你需要做的是通過你的livres來自控制器的變量。我不知道它叫什麼,也許$livres

所以這樣的:

public function pdfAction(){ 
    $html = $this->renderView('@Recommendation/Recommendation/payment-successful.html.twig', array(
     'livres' => $livres 
    )); 

    $filename = sprintf('test.pdf', date('Y-m-d')); 

    return new Response(
     $this->get('knp_snappy.pdf')->getOutputFromHtml($html), 
     200, 
     [ 
      'Content-Type'  => 'application/pdf', 
      'Content-Disposition' => sprintf('inline; filename="%s"', $filename), 
     ] 
    ); 
} 

我認爲應該這樣做。

+0

對不起,您可能需要將'livres'作爲參數傳遞給路徑'pdfAction'。我不確定你想要你的控制器工作。 –

+0

我沒有通過裏弗的車是這樣的: 並在控制器我做什麼ü告訴我: 公共職能pdfAction($車){$ HTML = $這個 - >的RenderView('@推薦/推薦/payment-successful.html.twig',array( 'livres'=> $ cart )); 對不對? 但它不起作用,我像往常一樣得到一個空表 –

+0

然後你可以嘗試調試URL,通過追加'app_dev.php'到URL。還要在twig中做這樣一個'livres'的轉儲:{{dump(livres)}}並查看是否爲空。轉儲會給生產中的500個錯誤 - 只是讓你知道。 –

0

閱讀本: https://symfony.com/doc/current/components/http_foundation/sessions.html

這是怎麼了會議,應當Symfony的完成。你需要改變一些東西...

+0

我對會話沒有任何問題,$ _session數組很有趣!但是這個pdfaction沒有發送任何東西!在發送值的控制器中沒有命令,它只獲取視圖 –

+0

但$ cart是空的!所以別的是錯的。建議你弄明白!直到你嘗試了一些東西,我纔會幫忙。 –

+0

Okey非常感謝你:) –

-1

FIXED:

我並不需要數組作爲參數傳遞! 我剛剛聲明瞭一個像這樣的變量:$ cart = $ _SESSION ['cart']($ _SESSION ['cart']是我用來存儲我的變量的數組),並直接傳入: $ html = $ this-> renderView('@ Recommendation/Recommendation/payment-pdf.html.twig',array( 'livres'=> $ cart ));

我正在使用一個SESSION數組,因此它被認爲像一個全局變量:)