使用 TCPDF 在 Pdf 页面中显示 Table 个值

Display Table values in Pdf page with TCPDF

我们使用下面的 php & javascript 代码来显示 table.

中的值

php

function getDesignerCollection(){
    $stmts = $user_home->runQuery("SELECT * FROM tbl_users WHERE userID=:uid");
    $stmts->execute(array(
        ":uid" => $_SESSION['userSession']
    ));
    $rows = $stmts->fetch(PDO::FETCH_ASSOC);

    $stmt = $user_home->runQuery("SELECT * FROM order_details");  
    $stmt->execute(array(":uid" => $_SESSION['userSession']));
    $row = $stmt->fetch(PDO::FETCH_ASSOC);
    $stmt->execute();

    $accountType = $rows['type'];
    if ($accountType == "admin"){
        $is_admin = true;
        $order = Mage::getModel('sales/order')->getCollection()->addAttributeToFilter('designer_id', array(
            'nin' => '0'
        ));
    }
    /* Fetching magento db details end */       

    $i = 0; 
    foreach($order as $orderData){ 
        $k = 0; 
        $orderitems = $orderData['dproduct_id']; 
        $orderitemsarray = explode(",", $orderitems); 

        while ($k < count($orderitemsarray)){ 
            if ($orderitemsarray[$k] != '0'){ 
                $stmtorders = $user_home->runQuery("SELECT * FROM order_details"); 
                $stmtorders->execute(array(":dorder_id" => $orderData['entity_id'])); 
                $roworders = $stmtorders->fetch(PDO::FETCH_ASSOC); 

                if($accountType == "admin"){ 
                    $designerName = getDesignerName($productdetail->getDesignerID()) . " -(" . $productdetail->getDesignerID() . ")"; 

                    while($datas = $stmt1->fetch()){
                        $paid_status=$datas['dpaid_status'];
                        $delivery_status=$datas['delivery_status'];
                    }
                    $responce[] = array( 
                        $orderData->getIncrementId() , 
                        $orderitemsarray[$k], 
                        $productdetail->getName() , 
                        $designerName, 
                        $orderData['status'],  
                        $orderData['grand_total'],
                        $orderData['customer_email'], 
                        $commission, 
                        $paid_status, 
                        $delivery_status,
                        $due_date,
                        $sDate
                    ); 
                }
                $k++; 
                $i++; 

            }   
            return json_encode($responce);  
        }

脚本

var __TEST_DATA__=eval('<?php
echo getDesignerCollection(); ?>');
var grid_demo_id = "myGrid" ;

    var colsOption = [
       {id: '' , header: "" , width :"15",renderer : my_renderId , width :"60"}, 
        {id: 'created_at' , header: "Order Date" , width :"100"},
        .
        .
       {id: 'delivery_status' , header: "Delivery Status" , width :"110"},
       {id: 'due_date' , header: "Payment Due Date" , width :"140"},

    ];

现在我在 php 页面中添加了 "button",一旦我们点击按钮,它应该以 pdf 格式显示 table。为此,我正在使用 TCPDF 并尝试使用以下代码。 一旦我点击 "button" 它重定向到 Pdf 页面,但它不显示 table.

中的值

$html = <<<EOD
<h1>Welcome</h1>
EOD;

$html='<table border="0" cellspacing="0" cellpadding="0" style="border:1px solid #e1e1e1">';
        $html.='<tr bgcolor="#cccccc" ><th  height="20" >Commission</th><th>Paid status</th><th>Delivery date</th><th>Due date</th></tr>';
        foreach ($order as $orderData)
        {
            if(is_array($order[0]))
            {

                foreach ($order as $orderData)
                {
                    if($i%2==0){ $html.='<tr bgcolor="#d3d6ff">';}else{ $html.='<tr>';}

                    $html.='<td  height="20" >&nbsp;'.$commission.'</td>';
                    $html.='<td>'.$paid_status.'</td>';           
                    $html.='<td>'.$delivery_status.'</td>';
                    $html.='<td>'.$due_date.'</td>';                      
                    $i++;             
                    $html.='</tr>';          
                } 
            }
            else
            {
                if($i%2==0){ $html.='<tr bgcolor="#d3d6ff">';}else{ $html.='<tr>';}

                    $html.='<td  height="20" >&nbsp;'.$commission.'</td>';
                    $html.='<td>'.$paid_status.'</td>';           
                    $html.='<td>'.$delivery_status.'</td>';
                    $html.='<td>'.$due_date.'</td>';                   
                $i++;             
                $html.='</tr>';
                }                
        }

        $html.='</table>';


    // Print text using writeHTMLCell()
    $pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);

完整代码:http://pastebin.com/P5DinusU,我是 php 的新手,在此处发布问题之前尝试了很多。

试试这个

<?php
    $html="<table border='0' cellspacing='0' cellpadding='0' style='border:1px solid #e1e1e1'>";
    $html.="<tr bgcolor='#cccccc'><th  height='20'>Commission</th><th>Paid status</th><th>Delivery date</th><th>Due date</th></tr>";
    $i=0;
    if (count($order)>0) {
        foreach ($order as $orderData)
        {
            $bgcolor=($i%2==0)?"#d3d6ff":"";
            $status=($orderData['status']==0)?"Inactive":"Active";
            $tbl="<tr bgcolor='".$bgcolor."'>";                    
            $tbl.="<td height='20'>&nbsp;".$commission."</td>";
            $tbl.="<td>".$paid_status."</td>";           
            $tbl.="<td>".$delivery_status."</td>";
            $tbl.="<td>".$due_date."</td>";                      
            $tbl.="</tr>";          
            $html.=$tbl;
            $i++;
        }
        $html.="</table>";
   }?>
        if(is_array($order))
        {

            foreach ($order as $orderData)
            {
                if($i%2==0){ $html.='<tr bgcolor="#d3d6ff">';}else{ $html.='<tr>';}

                $html.='<td  height="20" >&nbsp;'.$commission.'</td>';
                $html.='<td>'.$paid_status.'</td>';           
                $html.='<td>'.$delivery_status.'</td>';
                $html.='<td>'.$due_date.'</td>';                      
                $i++;             
                $html.='</tr>';          
            } 
        }
        else
        {
            if($i%2==0){ $html.='<tr bgcolor="#d3d6ff">';}else{ $html.='<tr>';}

                $html.='<td  height="20" >&nbsp;'.$commission.'</td>';
                $html.='<td>'.$paid_status.'</td>';           
                $html.='<td>'.$delivery_status.'</td>';
                $html.='<td>'.$due_date.'</td>';                   
            $i++;             
            $html.='</tr>';
         }                

    $html.='</table>';


// Print text using writeHTMLCell()
$pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);

在您的表单中创建一个隐藏的输入字段,并在其值中添加您的 html 结构。提交时从 post 获取它并将其添加到您正在创建的 PDF html 中。 为此,您需要将此代码添加到您的导出表单中

<form action="#" name="pdfForm" method="POST">
<?php
     $order=json_decode(getDesignerCollection(),true);
     $html="";
     foreach ($order as $orderData)
     {
         $html.='<tr>';
         $html.='<td  >'.$orderData[8].'</td>';
         $html.='<td>'.$orderData[9].'</td>';
         $html.='<td>'.$orderData[10].'</td>';
         $html.='<td>'.$orderData[11].'</td>'; 
         $html.='<td>'.$orderData[12].'</td>';
         $html.='<td>'.$orderData[1].'</td>';
         $html.='<td>'.$orderData[3].'</td>';
         $html.='</tr>';
     }
   ?> 
  <input name="order_data" type="hidden" value='<?php echo ($html); ?>'/>
  <button type="submit1" name="submit1" value="Submit"> Export To PDF    </button>
   </form>

添加后,只需将其从您的 PDF 代码中删除并使其与它一样

$html='<table border="0" cellspacing="0" cellpadding="0" style="border:1px solid #e1e1e1">';
    $html.='<tr bgcolor="#cccccc" >
    <th>Commission</th>
    <th>Paid status</th>
    <th>Delivery date</th>
    <th>Due date</th>
    <th>order date</th>
    <th>order id</th>
    <th>product name</th></tr>';

     $html .= $_POST['order_data'];
  $html.='</table>';