我使用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"> </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> </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
答案 0 :(得分:1)
首先,我不知道您为什么使用此行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++; } ?>