TCPDF当它创建多个页面时,它不能正确打印下一页

时间:2014-04-26 07:16:13

标签: php mysql tcpdf

我的PDF页面输出打印多页时无法正确打印。请看一下这个例子 以下是PDF文件expenses.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();

这就是脚本现在的样子

$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();

1 个答案:

答案 0 :(得分:1)

主要原因是你没有重新初始化变量$tbl,它在每个循环中保存表的主体。

foreach($result as $expense) {
    $tbl = '';

应该使您的输出更符合您想要的行。

现在你已经解决了

的问题
  

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

发生覆盖是因为您将输出绝对位置放在页面上:

$pdf->SetXY(2, 20);

这是第一次没问题。您的下一个内容(下一个循环)必须考虑实际的Y位置。你可以用'GetY()&#39;来获得这个。方法

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

应该照顾好这一点。