简体   繁体   中英

Display Table values in Pdf page with TCPDF

We are using below php & javascript code to display values inside 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);  
        }

script

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"},

    ];

Now i added "button" in php page , once we click on button, it should display table in pdf format. for that i am using TCPDF & trying below code. once i click on "button" its redirecting to Pdf page, but its not displaying values present in 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);

full code : http://pastebin.com/P5DinusU , i am new to php & tried lot before posting question here.

Try this

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

Create a hidden input filed in your form and add you html structure in its value. On submit get it from post and add it into your PDF html you are creating. For which You need to add this code into your export form

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

After adding it just remove that from your PDF code and make that like it

$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>'; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM