如何在以下场景中将表格从php文件打印到TCPDF?

时间:2013-10-22 12:50:20

标签: php html mysql sql tcpdf

我有一个php文件(add_member.php)。我正在通过执行SQL查询来创建动态表。该文件的代码如下:

$sql = SELECT * FROM member_details WHERE lname='ABC';

$n = new data();

$res = $n -> querySend($sql);

?>

<table border="1" cellpadding="3" cellspacing="1">
   <tr>
      <td align="center"></td>
      <td align="center">First Name</td>
      <td align="center">Last Name</td>
      <td align="center">Caste</td>
      <td align="center">Residence address</td>
      <td align="center">Education</td>
   </tr>
   <?php

       $i=1;

       while($row = mysql_fetch_array($res))
       { 
           $member_no = $row['member_no'];

           $total_member = "SELECT COUNT(*) AS total_member FROM family_member_details WHERE member_no =" .$member_no;

           $total_res = $n -> querySend($total_member);

           $row_total = mysql_fetch_array($total_res);        

          ?>

           <tr>
               <td align="center" rowspan="<?php echo $row_total['total_member']+1;?>"><?php echo $i;?></td>
               <td align="center"><?php echo $row['fname'];?></td>
               <td align="center"><?php echo $row['lname'];?></td>
               <td align="center"><?php echo $row['caste'];?></td>
               <td align="center"><?php echo $row['residence_addr'];?></td>
               <td align="center"><?php echo $row['education'];?></td>
           </tr>
           <?php 
                $family_sql = "SELECT * from family_member_details WHERE member_no = $member_no";
                $family_res = $n -> querySend($family_sql);

                while($row1 = mysql_fetch_array($family_res))
                {
                   ?>
                    <tr>
                        <td align="center"><?php echo $row1['name']?></td>
                        <td align="center"><?php echo $row1['name']?></td>
                        <td align="center"><?php echo $row1['name']?></td>
                        <td align="center"><?php echo $row1['name']?></td>
                        <td align="center"><?php echo $row1['name']?></td>
                    </tr>   
         <?php  }
                $i++; 
          } ?>
    </table>

现在点击一个按钮,我想在PDF文件中创建相同的表格。为此我决定使用TCPDF库。但是为此我要向TCPDF文件提供HTML内容。现在我的问题是如何从PHP文件中获取动态生成的表的HTML并将此内容写入文本文件?有人可以帮我这方面吗?任何帮助将受到高度赞赏。

1 个答案:

答案 0 :(得分:2)

不是直接将表显示到浏览器页面,只需将文本存储在变量中并回显它...然后您可以将var发送到TCPDF库。

$dyn_table = '<table border="1" cellpadding="3" cellspacing="1"><tr><td align="center">/td><th align="center">First Name</th><th align="center">Last Name</th><th align="center">Caste</th><th align="center">Residence address</th><th align="center">Education</th></tr>';

$i = 1;
while ($row = mysql_fetch_array($res)) {
    $member_no = $row['member_no'];
    $total_member = "SELECT COUNT(*) AS total_member FROM family_member_details WHERE member_no =" . $member_no;

    $total_res = $n->querySend($total_member);

    $row_total = mysql_fetch_array($total_res);

    $dyn_table .= '<tr><td align="center" rowspan="' . $row_total['total_member'] + 1 . '">' . $i . '</td><td align="center">' . $row['fname'] . '</td><td align="center">' . $row['lname'] . '</td><td align="center">' . $row['caste'] . '</td><td align="center">' . $row['residence_addr'] . '</td><td align="center">' . $row['education'] . '</td></tr>';

    $family_sql = "SELECT * from family_member_details WHERE member_no = $member_no";
    $family_res = $n->querySend($family_sql);

    while ($row1 = mysql_fetch_array($family_res)) {
        $dyn_table .= '<tr><td align="center">' . $row1['name'] . '</td><td align="center">' . $row1['name'] . '</td><td align="center">' . $row1['name'] . '</td><td align="center">' . $row1['name'] . '</td><td align="center">' . $row1['name'] . '</td></tr>';
    }
    $i++;
}
$dyn_table .= '</table>';
echo $dyn_table;

修改

为了将这个html发布到你的TCPDF库,我会使用AJAX来防止另一个页面请求/加载。我更喜欢使用JQuery,因为它极大地简化了这个过程。这是你可以做到的一种方式:

<input type="button" name="TCPDF" id="submitToTCPDF" />
<script type="text/javascript">
    var url = 'php/script/to/handle/post';
    var data = {'table_html': '<? echo $dyn_table; ?>'};

    $('#TCPDF').click(function(){
        $.ajax({
            type: "POST",
            url: url,
            data: data,
            success: function($result){
                // Do whatever after html is submitted


            }
        });
    });
</script>

您可以在Jquery's AJAX post method中详细了解this StackOverflow question