Php TCPDF创建多个页面时,无法正确打印下一页

Php TCPDF创建多个页面时,无法正确打印下一页,php,mysql,tcpdf,Php,Mysql,Tcpdf,我的PDF页面的输出当它打印多个页面时,它无法正确打印。请看这个例子 下面是PDF文件的一个示例。 如果您注意到页面在第2页上开始打印的位置,它会覆盖自身。 此外,它不会切断类别上方第一页的页面,因此它会将信息带到第二页。 此外,该列表中只有19个项目,但它打印5页并重复这些项目 class PDF extends tcpdf { // Page header function Header() { $this->SetFont('Times', 'BI', 20, '', 'fa

我的PDF页面的输出当它打印多个页面时,它无法正确打印。请看这个例子 下面是PDF文件的一个示例。 如果您注意到页面在第2页上开始打印的位置,它会覆盖自身。 此外,它不会切断类别上方第一页的页面,因此它会将信息带到第二页。 此外,该列表中只有19个项目,但它打印5页并重复这些项目

class PDF extends tcpdf
{
// Page header
function Header()
{
    $this->SetFont('Times', 'BI', 20, '', 'false');
    // Move to the right
   $this->Ln(5);
   $this->Cell(60);
   $this->Cell($w, $h=0, $txt='EXPENCE REPORT', $border=0, $ln=0, $align='', $fill=false, $link='', $stretch=0,   
   $ignore_min_height=false, $calign='T', $valign='M');
   $this->Line (0, 13, 210, 13, $style=array());
   // Line break
   $this->Ln(5);
}
 // Page footer
    public function Footer() {
        // Position at 15 mm from bottom
        $this->SetY(-15);
        // Set font
        $this->SetFont('helvetica', 'I', 8);
        // Page number
        $this->Cell(0, 10, 'Page '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
    }
}
$pdf = new PDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->AddPage ($orientation='', $format='', $keepmargins=false, $tocpage=false);

$pdf->SetAutoPageBreak ($auto, $margin=16);
$pdf->SetPrintHeader(true);
$pdf->SetPrintFooter(true);   
$pdf->setFontSubsetting(true);
// set margins
$pdf->SetMargins(10, PDF_MARGIN_TOP, 10);
// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

$pdf->SetFont('helvetica', '', 14, '', true);

$tbl_header = '<table cellspacing="3" style="width: 100%; text-align: center; font-size: 8pt;">';
$tbl_footer = '</table>';
$tbl = '';

$sql = "SELECT `expensetype`.`typeid`, `expensetype`.`exptype`, `expenses`.`expid`,`expenses`.`expdate`, `expenses`.`checktype`, `expenses`.`payee`,`expenses`.`details`, `expenses`.`amount` FROM `expensetype` INNER JOIN `expenses` ON
`expensetype`.`typeid` = `expenses`.`typeid`WHERE (`expenses`.`pid` = " . $pid . ")";
$result = $db->query($sql);
$expensetype = null;
foreach($result as $expense) {
if($expensetype != $expense['typeid']) {
   $exptype = $expense['exptype'];

$tbl .= ' 
<tr>
<td height="10" style="width: 100%;"></td>
</tr> 
<tr style="background-color:#E1E0E0">
<td height="20" style="width: 25%; text-align: left">Category:</td>
<td height="20" style="width: 75%; text-align: left">'.$exptype.'</td>
</tr>';

$tbl .= '
          <tr style="background-color:#CEC7C7">
            <th style="width: 20%">Date:</th>
            <th style="width: 30%">Payee:</th>
            <th style="width: 30%">Detalls:</th>
            <th style="width: 20%">Amount:</th>
            </tr>';

$pdf->SetXY(2, 20);
$pdf->writeHTML($tbl_header . $tbl . $tbl_footer, true, false, false, false, '');
 }

   $expdate=phpdate($expense['expdate']);

$tbl .= '
<tr>
<td style="width: 20%; text-align: center">'.$expdate.'</td> 
<td style="width: 30%; text-align: center">'.$expense['payee'].'</td>
<td style="width: 30%; text-align: center">'.$expense['details'].'</td>
<td style="width: 20%; text-align: center">'.$expense['amount'].'</td>
</tr>';

if($expensetype != $expense['typeid']) {
   $expensetype = $expense['typeid'];
    }
} 
$pdf->SetXY(2, 20);
$pdf->writeHTML($tbl_header . $tbl . $tbl_footer, true, false, false, false, '');
// $tbl .= '
//<tr>
//<th style="width: 85%; text-align: right;">Total Amount:'  .$totalamount.' </th>
//</tr>';    
ob_end_clean();
$pdf->Output();
classpdf扩展了tcpdf
{
//页眉
函数头()
{
$this->SetFont('Times','BI',20','false');
//向右移动
$this->Ln(5);
$this->Cell(60);
$this->Cell($w,$h=0,$txt='EXPENCE REPORT',$border=0,$ln=0,$align='',$fill=false,$link='',$stretch=0,
$ignore_min_height=false,$calign='T',$valign='M');
$this->Line(0,13210,13,$style=array());
//断线
$this->Ln(5);
}
//页脚
公共函数页脚(){
//放置在距离底部15 mm处
$this->SetY(-15);
//设置字体
$this->SetFont('helvetica','I',8);
//页码
$this->Cell(0,10,'Page'.$this->getAliasNumPage()./'.。$this->getAliasNbPages(),0,false,'C',0',false,'T','M');
}
}
$pdf=新pdf(pdf页面方向,pdf单元,pdf页面格式,真,'UTF-8',假);
$pdf->AddPage($orientation='',$format='',$keepmargins=false,$tocpage=false);
$pdf->SetAutoPageBreak($auto,$margin=16);
$pdf->SetPrintHeader(true);
$pdf->SetPrintFooter(true);
$pdf->setFontSubsetting(真);
//设置边距
$pdf->SetMargins(10,pdf\u MARGIN\u TOP,10);
//设置自动分页符
$pdf->SetAutoPageBreak(TRUE,pdf\u MARGIN\u BOTTOM);
$pdf->SetFont('helvetica','',14',,true);
$tbl_标题=“”;
$tbl_页脚=“”;
$tbl='';
$sql=“选择`expensetype`.`typeid`、`expensetype`.`exptype`、`expid`、`expenses`.`expdate`、`expenses`.`checktype`、`expenses`.`收款人`、`expenses`.`details`、`expensetype`.`amount`从上的`expensetype`内部联接`expenses`
`expensetype`.`typeid`=`expenses`.`typeid`其中(`expenses`.`pid`='$pid.');
$result=$db->query($sql);
$expensetype=null;
foreach($结果为$费用){
如果($expensetype!=$expense['typeid'])){
$exptype=$expense['exptype'];
$tbl='
类别:
“.$exptype。”
';
$tbl='
日期:
收款人:
详情:
数量:
';
$pdf->SetXY(2,20);
$pdf->writeHTML($tbl_页眉。$tbl.$tbl_页脚,真,假,假,'');
}
$expdate=phpdate($expense['expdate']);
$tbl='
“.$expdate。”
“.$费用[‘收款人’]”
“.$费用['details']”
“.$费用['金额].”
';
如果($expensetype!=$expense['typeid'])){
$expensetype=$expense['typeid'];
}
} 
$pdf->SetXY(2,20);
$pdf->writeHTML($tbl_页眉。$tbl.$tbl_页脚,真,假,假,'');
//$tbl='
//
//总额:'.$totalamount'
//';    
ob_end_clean();
$pdf->Output();
这就是脚本现在的样子

$sql = "SELECT `expensetype`.`typeid`, `expensetype`.`exptype`, `expenses`.`expid`,`expenses`.`expdate`, `expenses`.`checktype`, `expenses`.`payee`,`expenses`.`details`, `expenses`.`amount` FROM `expensetype` INNER JOIN `expenses` ON
`expensetype`.`typeid` = `expenses`.`typeid`WHERE (`expenses`.`pid` = " . $pid . ")";
$result = $db->query($sql);
$expensetype = null;

foreach($result as $expense) {
$tbl = '';    
if($expensetype != $expense['typeid']) {
$exptype = $expense['exptype'];

$tbl .= ' 
<tr>
<td height="10" style="width: 100%;"></td>
</tr> 
<tr style="background-color:#E1E0E0">
<td height="20" style="width: 25%; text-align: left">Category:</td>
<td height="20" style="width: 75%; text-align: left">'.$exptype.'</td>
</tr>';

$tbl .= '
          <tr style="background-color:#CEC7C7">
            <th style="width: 20%">Date:</th>
            <th style="width: 30%">Payee:</th>
            <th style="width: 30%">Detalls:</th>
            <th style="width: 20%">Amount:</th>
            </tr>';

$pdf->SetXY(2, 20 + $pdf->GetY());
$pdf->writeHTML($tbl_header . $tbl . $tbl_footer, true, false, false, false, '');
 }

   $expdate=phpdate($expense['expdate']);
$tbl = '';
$tbl .= '
<tr>
<td style="width: 20%; text-align: center">'.$expdate.'</td> 
<td style="width: 30%; text-align: center">'.$expense['payee'].'</td>
<td style="width: 30%; text-align: center">'.$expense['details'].'</td>
<td style="width: 20%; text-align: center">'.$expense['amount'].'</td>
</tr>';



$pdf->SetXY(2, 20 + $pdf->GetY());
$pdf->writeHTML($tbl_header . $tbl . $tbl_footer, true, false, false, false, '');
if($expensetype != $expense['typeid']) {
   $expensetype = $expense['typeid'];
    }
}

$pdf->Output();
$sql=“选择`expensetype`.`typeid`、`expensetype`.`exptype`、`expid`、`expenses`.`expdate`、`expenses`.`checktype`、`expenses`.`payee`.`details`、`expensetype`.`expensetype`.`expensetype`、`expid`、`expensetype`.`amount`来自于上的`expensetype`内部联接`expenses`
`expensetype`.`typeid`=`expenses`.`typeid`其中(`expenses`.`pid`='$pid.');
$result=$db->query($sql);
$expensetype=null;
foreach($结果为$费用){
$tbl='';
如果($expensetype!=$expense['typeid'])){
$exptype=$expense['exptype'];
$tbl='
类别:
“.$exptype。”
';
$tbl='
日期:
收款人:
详情:
数量:
';
$pdf->SetXY(2,20+$pdf->GetY());
$pdf->writeHTML($tbl_页眉。$tbl.$tbl_页脚,真,假,假,'');
}
$expdate=phpdate($expense['expdate']);
$tbl='';
$tbl='
“.$expdate。”
“.$费用[‘收款人’]”
“.$费用['details']”
“.$费用['金额].”
';
$pdf->SetXY(2,20+$pdf->GetY());
$pdf->writeHTML($tbl_页眉。$tbl.$tbl_页脚,真,假,假,'');
如果($expensetype!=$expense['typeid'])){
$expensetype=$expense['typeid'];
}
}
$pdf->Output();

主要原因是您没有重新初始化变量
$tbl
,该变量保存每个循环的表体

foreach($result as $expense) {
    $tbl = '';
应该使您的输出更符合您的要求

现在你已经解决了这个问题

此外,该列表中只有19项,但它打印了5页和 重复这些项目

class PDF extends tcpdf
{
// Page header
function Header()
{
    $this->SetFont('Times', 'BI', 20, '', 'false');
    // Move to the right
   $this->Ln(5);
   $this->Cell(60);
   $this->Cell($w, $h=0, $txt='EXPENCE REPORT', $border=0, $ln=0, $align='', $fill=false, $link='', $stretch=0,   
   $ignore_min_height=false, $calign='T', $valign='M');
   $this->Line (0, 13, 210, 13, $style=array());
   // Line break
   $this->Ln(5);
}
 // Page footer
    public function Footer() {
        // Position at 15 mm from bottom
        $this->SetY(-15);
        // Set font
        $this->SetFont('helvetica', 'I', 8);
        // Page number
        $this->Cell(0, 10, 'Page '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
    }
}
$pdf = new PDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->AddPage ($orientation='', $format='', $keepmargins=false, $tocpage=false);

$pdf->SetAutoPageBreak ($auto, $margin=16);
$pdf->SetPrintHeader(true);
$pdf->SetPrintFooter(true);   
$pdf->setFontSubsetting(true);
// set margins
$pdf->SetMargins(10, PDF_MARGIN_TOP, 10);
// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

$pdf->SetFont('helvetica', '', 14, '', true);

$tbl_header = '<table cellspacing="3" style="width: 100%; text-align: center; font-size: 8pt;">';
$tbl_footer = '</table>';
$tbl = '';

$sql = "SELECT `expensetype`.`typeid`, `expensetype`.`exptype`, `expenses`.`expid`,`expenses`.`expdate`, `expenses`.`checktype`, `expenses`.`payee`,`expenses`.`details`, `expenses`.`amount` FROM `expensetype` INNER JOIN `expenses` ON
`expensetype`.`typeid` = `expenses`.`typeid`WHERE (`expenses`.`pid` = " . $pid . ")";
$result = $db->query($sql);
$expensetype = null;
foreach($result as $expense) {
if($expensetype != $expense['typeid']) {
   $exptype = $expense['exptype'];

$tbl .= ' 
<tr>
<td height="10" style="width: 100%;"></td>
</tr> 
<tr style="background-color:#E1E0E0">
<td height="20" style="width: 25%; text-align: left">Category:</td>
<td height="20" style="width: 75%; text-align: left">'.$exptype.'</td>
</tr>';

$tbl .= '
          <tr style="background-color:#CEC7C7">
            <th style="width: 20%">Date:</th>
            <th style="width: 30%">Payee:</th>
            <th style="width: 30%">Detalls:</th>
            <th style="width: 20%">Amount:</th>
            </tr>';

$pdf->SetXY(2, 20);
$pdf->writeHTML($tbl_header . $tbl . $tbl_footer, true, false, false, false, '');
 }

   $expdate=phpdate($expense['expdate']);

$tbl .= '
<tr>
<td style="width: 20%; text-align: center">'.$expdate.'</td> 
<td style="width: 30%; text-align: center">'.$expense['payee'].'</td>
<td style="width: 30%; text-align: center">'.$expense['details'].'</td>
<td style="width: 20%; text-align: center">'.$expense['amount'].'</td>
</tr>';

if($expensetype != $expense['typeid']) {
   $expensetype = $expense['typeid'];
    }
} 
$pdf->SetXY(2, 20);
$pdf->writeHTML($tbl_header . $tbl . $tbl_footer, true, false, false, false, '');
// $tbl .= '
//<tr>
//<th style="width: 85%; text-align: right;">Total Amount:'  .$totalamount.' </th>
//</tr>';    
ob_end_clean();
$pdf->Output();
之所以会发生覆盖,是因为您将输出绝对值放置在页面上:

$pdf->SetXY(2, 20);
第一次没关系。下一个内容(下一个循环)必须考虑实际Y位置。您可以通过“GetY()”方法获得此信息

$pdf->SetXY(2, 20 + $pdf->GetY());

我应该处理好这件事

我添加了你显示的内容,但它只打印了一行检查此链接我已经有$tbl='';添加到脚本的正上方sql@user3326386:我已经编辑了我的答案,并给出了如何解决剩余问题的提示。我查看了GetY()的功能,这应该可以工作,但我遇到了一个致命错误:调用未定义的函数GetY()。这没有意义,因为它是TCPDF中的一个方法。任何Ideas@user3326386:我不小心:它必须是
$pdf->GetY()
,请参见编辑的代码。