TCPDF如何在HTML内编写PHP代码

时间:2019-12-26 08:41:14

标签: php tcpdf

我正在使用TCPDF创建库存清单打印。我在将while循环放入HTML标记中时遇到问题,我该如何正确编写会出错。我尝试过的,所以我在下面写道。日期和最后插入的以及总计的工资余额都显示得很好。只有我需要多次尝试的while循环,但是我不能

<?php
require_once('lib/master/tcpdf.php');
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, [80, 230], true, 'UTF-8', false);

$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$pdf->SetFont('dejavusans', '', 10);
$pdf->AddPage();
include("db.php");
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
    $last_insert_id = 51;
    $sql = "select i.sales_id,i.product_id,i.sell_price,i.qty,i.discount,i.total,s.date,s.grand_total,s.pay,s.balance,pr.p_name from sales_product i, sales s,product pr where s.id = i.sales_id and i.product_id = pr.barcode and  i.sales_id =  $last_insert_id ";
    $orderResult = $conn->query($sql);
    $orderData = $orderResult->fetch_array();
    $sales_id = $orderData[0];
    $product_id = $orderData[1];
    $price = $orderData[2];
    $qty = $orderData[3];
    $discount = $orderData[4];
    $total = $orderData[5];
    $date = $orderData[6];
    $grand_total = $orderData[7];
    $pay = $orderData[8];
    $balance = $orderData[9];
    $product_name = $orderData[10];
// create some HTML content
    $html = '<p align="center"><b style="font-size: 14px">Tutus Funny</b><br>
<i>Computer best lessons online</i>
</p>
                    <p align="left">
                    Order #  '.$last_insert_id.' </b>
                        <br>
                         Date # <b>'.$date.' </b>
                    </p>


                            <table class="table table-condensed">
                                <thead>
                                <tr>

                                    <td align="center"><strong>Pname</strong></td>
                                    <td align="center"><strong>Qty</strong></td>
                                    <td align="center"><strong>Price</strong></td>
                                    <td align="center"><strong>Total</strong></td>
                                </tr>
                                </thead>

 '.
                                $x = 1;
                                $last_insert_id = $_GET['last_id']; //
                                $orderResult = $conn->query($sql);
                                while($row = $orderResult->fetch_array()){
                                    ?>




                                    <tr>
                                     <td align="center">.$pname.</td>
                                     <td align="center">.$price.</td>
                                     <td align="center">.$qty.</td>
                                      <td align="center">.$total.</td>
                                    </tr>
        $x++;   } .'
                            </table>
<br>

                    <div  align="right">
                        Sub Total &nbsp;&nbsp;<b>'.$grand_total.'</b><br>
                          Pay  &nbsp;&nbsp; <b> '.$pay.'</b><br>
                           Due &nbsp;&nbsp;   <b>'.$balance.'</b>
                    </div>

                    <div align="center">
                        <i>60 b bank road badulla</i>
                    </div>
                </div>
           </div>


                ';

// output the HTML content
    $pdf->writeHTML($html, true, false, true, false, '');


// - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

// reset pointer to the last page
    $pdf->lastPage();
    $pdf->Output();
}
?>

1 个答案:

答案 0 :(得分:1)

尝试此代码

  <?php
require_once('lib/master/tcpdf.php');
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, [80, 230], true, 'UTF-8', false);

$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$pdf->SetFont('dejavusans', '', 10);
$pdf->AddPage();
include("db.php");
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
    $last_insert_id = 51;
    $sql = "select i.sales_id,i.product_id,i.sell_price,i.qty,i.discount,i.total,s.date,s.grand_total,s.pay,s.balance,pr.p_name from sales_product i, sales s,product pr where s.id = i.sales_id and i.product_id = pr.barcode and  i.sales_id =  $last_insert_id ";
    $orderResult = $conn->query($sql);
    $orderData = $orderResult->fetch_array();
    $sales_id = $orderData[0];
    $product_id = $orderData[1];
    $price = $orderData[2];
    $qty = $orderData[3];
    $discount = $orderData[4];
    $total = $orderData[5];
    $date = $orderData[6];
    $grand_total = $orderData[7];
    $pay = $orderData[8];
    $balance = $orderData[9];
    $product_name = $orderData[10];
// create some HTML content
    $html = '<p align="center"><b style="font-size: 14px">Tutus Funny</b><br><i>Computer best lessons online</i></p><p align="left">Order #  '.$last_insert_id.' </b><br>Date # <b>'.$date.' </b>
         </p><table class="table table-condensed"><thead>
                                <tr><td align="center"><strong>Pname</strong></td>
                                    <td align="center"><strong>Qty</strong></td>
                                    <td align="center"><strong>Price</strong></td>
                                    <td align="center"><strong>Total</strong></td>
                                </tr>
                                </thead>';
                                $x = 1;
                                $last_insert_id = $_GET['last_id']; //
                                $orderResult = $conn->query($sql);
                                while($row = $orderResult->fetch_array())
                                {





                                    $html.='<tr>
                                     <td align="center">'.$pname.'</td>
                                     <td align="center">'.$price.'</td>
                                     <td align="center">'.$qty.'</td>
                                      <td align="center">'.$total.'</td>
                                    </tr>';
        $x++;   } 

                  $html.='</table>
<br>

                    <div  align="right">
                        Sub Total &nbsp;&nbsp;<b>'.$grand_total.'</b><br>
                          Pay  &nbsp;&nbsp; <b> '.$pay.'</b><br>
                           Due &nbsp;&nbsp;   <b>'.$balance.'</b>
                    </div>

                    <div align="center">
                        <i>60 b bank road badulla</i>
                    </div>
                </div>
           </div>';

// output the HTML content
    $pdf->writeHTML($html, true, false, true, false, '');


// - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

// reset pointer to the last page
    $pdf->lastPage();
    $pdf->Output();
}
相关问题