【问题标题】:Display Table values in Pdf page with TCPDF使用 TCPDF 在 Pdf 页面中显示表值
【发布时间】:2017-05-11 16:14:57
【问题描述】:

我们使用下面的 php 和 javascript 代码来显示表格内的值。

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 页面中添加了“按钮”,一旦我们点击按钮,它应该以 pdf 格式显示表格。为此,我正在使用 TCPDF 并尝试以下代码。 一旦我单击“按钮”,它就会重定向到 Pdf 页面,但它不显示表中存在的值。

$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 新手,在这里发布问题之前尝试了很多。

【问题讨论】:

  • $order 中有什么?我认为它是空数组
  • @VijayaVigneshKumar 我们正在从 order_details 表中获取已付款状态、佣金和到期日期的值....请检查完整代码一次:pastebin.com/P5DinusU
  • 能否请您 print_r($order);并将结果粘贴到此处
  • 意思是我需要那个数组
  • 我有一个疑问,请您澄清一下

标签: javascript php pdf tcpdf


【解决方案1】:

在表单中创建一个隐藏的输入文件,并在其值中添加 html 结构。在提交时从帖子中获取它并将其添加到您正在创建的 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>'; 

【讨论】:

    【解决方案2】:

    试试这个

    <?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>";
       }?>
    

    【讨论】:

    • 你能把订单数组发给我吗
    • 当我在return json_encode($responce); 之前尝试print_r($order); 时,它隐藏了表格
    • $responce[] = array( $orderData-&gt;getIncrementId() , $orderData-&gt;getIncrementId() , $orderitemsarray[$k], $productdetail-&gt;getName() , $designerName, $orderData['status'], // magento [ pending / processing/complete] $orderData['grand_total'], // magento $orderData['customer_email'], // magento $commission, $paid_status, $delivery_status, $due_date, $sDate // magento );
    • 我试过你的代码,它显示的结果与here电子邮件:kidsdial1@gmail.com,密码:kidsdial1
    • 移除退出;和 print_r($order);
    【解决方案3】:
            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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-03
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 2015-09-10
      • 2019-09-26
      • 2015-09-28
      • 1970-01-01
      相关资源
      最近更新 更多