无法生成PDF Symfony 3

时间:2016-11-28 23:02:43

标签: php pdf twig symfony

我遵循knpsnappybundle的教程在symfony3中使用pdf,但问题是我需要将一个值数组传递给pdf页面,这样我就能获得像原始页面一样的完整页面

这是控制器:

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),

        ]
    );

}

这是普通页面的图片,带有PRINT按钮代码:

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

和路线:

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

我原始页面的图片: https://i.stack.imgur.com/3MBEt.png

现在生成的pdf图片: 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-&gt; redirectToRoute(“recommendation_bookid”);             返回$ this-&gt;重定向($ this-&gt; generateUrl('recommendation_new',array('id'=&gt; $ 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'];

返回$ this-&gt; render('RecommendationBundle:推荐:cart.html.twig',数组('id'=&gt; $ id,'livres'=&gt; $ cart));

}

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)));

返回$ this-&gt; redirectToRoute('recommendation_cart_show',array('livres'=&gt; $ cart)); //返回$ this-&gt; render('RecommendationBundle:推荐:cart.html.twig',数组('id'=&gt; 1,'livres'=&gt; $ 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}       默认值:{_ control:“RecommendationBundle:Recommendation:afficheparid”}   recommendation_book:       路径:/ book_detail       默认值:{_ control:“RecommendationBundle:Recommendation:affiche”}   recommendation_new:       路径:/ book_detail / {id} / new       默认值:{_ control:“RecommendationBundle:Recommendation:Ajout”}   recommendation_delete:       路径:/ book_detail / {id_l} / {id_r} / delete       默认值:{_ control:“RecommendationBundle:Recommendation:Delete”}   recommendation_edit:       路径:/ book_detail / {id_l} / {id_r} /编辑       默认值:{_ control:“RecommendationBundle:Recommendation:Update”}   recommendation_find:       路径:/ book_detail / {id_l} / {id_r} / find       默认值:{_ control:“RecommendationBundle:Recommendation:Find”}   recommendation_cart:       路径:/ book_detail / {id} / cart       默认值:{_ control:“RecommendationBundle:Recommendation:cart”}   recommendation_empty:       路径:/ emptycart       默认值:{_ control:“RecommendationBundle:Recommendation:emptycart”}   recommendation_cart_remove:       路径:/ remove_book / {id}       默认值:{_ control:“RecommendationBundle:Recommendation:removebookcart”}   recommendation_cart_show:       路径:/ remove_book       默认值:{_ control:“RecommendationBundle:Recommendation:show”}   payment_ok:       路径:/ payment_success       默认值:{_ control:“RecommendationBundle:Recommendation:ok”}

payment_pdf:     路径:/ payment_pdf     默认值:{_ control:“RecommendationBundle:Recommendation:pdf”}

3 个答案:

答案 0 :(得分: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),
        ]
    );
}

我认为应该这样做。

答案 1 :(得分:0)

阅读本文: https://symfony.com/doc/current/components/http_foundation/sessions.html

这就是在Symfony中完成会话的方式。你需要改变一些事情......

答案 2 :(得分:-1)

<强> FIXED:

我不需要将数组作为参数传递! 我只是声明了一个这样的变量:$ cart = $ _SESSION ['cart']($ _SESSION ['cart']是我用来存储我的变量的数组)在我的pdfController中并直接传递它:  $ html = $ this-&gt; renderView('@ Recommendation / Recommendation / payment-pdf.html.twig',array(         'livres'=&gt; $车     ));

我正在使用SESSION数组,因此它被视为全局变量:)