首页 > 解决方案 > 为什么pdf只生成第一行?

问题描述

我使用 mpdf 类生成 pdf。生成第一行数据。

<?php
                    $host="localhost"; 
                    $db_user="root"; 
                    $db_password=""; 
                    $dbname="test"; 


                    $connect=mysql_connect($host,$db_user,$db_password) or die(mysql_error());
                    $select_db=mysql_select_db($dbname);
                    require_once('mpdf.php'); 
                    ob_start(); 
                    ?>
                    <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">

                    <style type="text/css">
                    <!--
                    @page rotated { size: portrait; }
                    .style1 {
                      font-family: "Times New roman";
                      font-size: 18pt;
                      font-weight: bold;
                    }
                    .style2 {
                      font-family: "Times New roman";
                      font-size: 16pt;
                      font-weight: bold;
                    }
                    .style3 {
                      font-family: "Times New roman";
                      font-size: 16pt;

                    }
                    .style5 {cursor: hand; font-weight: normal; color: #000000;}
                    .style9 {font-family: Tahoma; font-size: 12px; }
                    .style11 {font-size: 12px}
                    .style13 {font-size: 9}
                    .style16 {font-size: 9; font-weight: bold; }
                    .style17 {font-size: 12px; font-weight: bold; }
                    -->
                    </style>
                    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
                    <html xmlns="http://www.w3.org/1999/xhtml">
                    <html>
                    <head>
                    <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
                    </head>
                    <body>
                    <div class=Section2>
                    <table width="704" border="0" align="center" cellpadding="0" cellspacing="0">
                      <tr>
                      <td width="291" align="center"><span class="style2">Heading1</span></td>

                      </tr>
                      <tr>
                        <td height="27" align="center"><span class="style2">Heading2  </span></td>
                      </tr>

                    </table>
                    <table width="200" border="0" align="center">
                      <tbody>
                        <tr>
                          <td align="center">&nbsp;</td>
                        </tr>
                      </tbody>
                    </table>
                    <table bordercolor="#424242" width="1141" height="78" border="1"  align="center" cellpadding="0" cellspacing="0" class="style3">
                      <tr align="center">
                        <td width="44" height="23" align="center" bgcolor="#D5D5D5"><strong>aaa</strong></td>
                        <td width="44" height="23" align="center" bgcolor="#D5D5D5"><strong>bbbb</strong></td>
                        <td width="178" align="center" bgcolor="#D5D5D5"><strong>bbbbb</strong></td>
                        <td width="123" align="center" bgcolor="#D5D5D5"><strong>vvvvvvvvv</strong></td>
                        <td width="123" align="center" bgcolor="#D5D5D5"><strong>vvvvvvvvv</strong></td>

                        </tr>

                        <?php
                    $objConnect = mysql_connect("localhost","root","") or die("Error Connect to Database");
                    $objDB = mysql_select_db("test");
                    //mysql_query("set NAMES'UTF8'");
                    $strSQL = "SELECT * FROM main_details";
                    $objQuery = mysql_query($strSQL);
                    $resultData = array();
                    while($data = mysql_fetch_array($objQuery)){
                       array_push($resultData,$data);
                    ?>
                      <tr>
                         <td align="right" class="style3"><?php echo $result['$i'];?></td>
                        <td align="right" class="style3"><?php echo $result['batch'];?></td>
                        <td align="right" class="style3"><?php echo $result['center']; ?>  
                        <td align="right" class="style3"><?php echo $result['code']; ?></td>
                         <td align="right" class="style3"><?php echo $result['created_at']; ?></td>

                        </tr>

                      <?php } ?>

                    </table>
                    <table width="200" border="0">
                      <tbody>
                        <tr>
                          <td>&nbsp;</td>
                        </tr>
                      </tbody>
                    </table>

                    </div>
                    </body>
                    </html>
                    <?Php
                    $html = ob_get_contents();
                    ob_end_clean();
                    $pdf = new mPDF('th', 'A4','0',''); 
                    $pdf->SetAutoFont();
                    $pdf->SetDisplayMode('fullpage');
                    $pdf->WriteHTML($html, 2);
                    $pdf->Output();
                    ?>

为什么我的 pdf 只生成第一行。我找不到错误。如何正确获取 pdf

标签: phphtmlpdf-generation

解决方案


首先,我不知道你为什么使用这一行array_push($resultData,$data);,因为你没有在任何你提供的代码中使用它。

然后,您遇到的第二个问题是您对不存在的数组使用 echo ,这可能就是为什么您只得到第一行,我相信这是标题。

所以请更改这些行:

<tr>
                         <td align="right" class="style3"><?php echo $result['$i'];?></td>
                        <td align="right" class="style3"><?php echo $result['batch'];?></td>
                        <td align="right" class="style3"><?php echo $result['center']; ?>  
                        <td align="right" class="style3"><?php echo $result['code']; ?></td>
                         <td align="right" class="style3"><?php echo $result['created_at']; ?></td>

                        </tr>

<tr>
                             <td align="right" class="style3"><?php echo $data['$i'];?></td>
                            <td align="right" class="style3"><?php echo $data['batch'];?></td>
                            <td align="right" class="style3"><?php echo $data['center']; ?> </td> 
                            <td align="right" class="style3"><?php echo $data['code']; ?></td>
                             <td align="right" class="style3"><?php echo $data['created_at']; ?></td>

                            </tr>

我想你应该没问题。更新:至于在每页上显示标题,这是一个解决方案,它通过设置一个计数器并每次递增它来工作,一旦达到 50(或每页的最大行数 - 由您设置),它将打印再次标题。这是代码:

<?php $linesPerPageCount =0;
while($data = mysql_fetch_array($objQuery)){

                    ?>
                    <?php if($linesPerCount == 50){
                    $linesPerCount = 0;
                    ?>
                    <tr align="center">
                        <td width="44" height="23" align="center" bgcolor="#D5D5D5"><strong>aaa</strong></td>
                        <td width="44" height="23" align="center" bgcolor="#D5D5D5"><strong>bbbb</strong></td>
                        <td width="178" align="center" bgcolor="#D5D5D5"><strong>bbbbb</strong></td>
                        <td width="123" align="center" bgcolor="#D5D5D5"><strong>vvvvvvvvv</strong></td>
                        <td width="123" align="center" bgcolor="#D5D5D5"><strong>vvvvvvvvv</strong></td>

                        </tr>
                    <?php
                    }
                      <tr>
                         <td align="right" class="style3"><?php echo $result['$i'];?></td>
                        <td align="right" class="style3"><?php echo $result['batch'];?></td>
                        <td align="right" class="style3"><?php echo $result['center']; ?>  
                        <td align="right" class="style3"><?php echo $result['code']; ?></td>
                         <td align="right" class="style3"><?php echo $result['created_at']; ?></td>

                        </tr>

                      <?php $linesPerCount++; } ?>  

推荐阅读