My output of my PDF page when it prints more than one page it does not print correctly. Please look at the example Here is an example of the PDF file expenses.pdf. If you notice where the page starts printing on page 2 it overwrites itself. Also it does not cut off the page on the first page above the category so it carry's the info onto the 2nd page. Also there are only 19 items in this list yet it prints 5 pages and repeats the items.
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();
This is what the script looks like now
$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();
Main cause is that you don't reinitialize the variable $tbl
, that holds the body of your table every loop.
foreach($result as $expense) {
$tbl = '';
should make your output more in the line that you want.
Now you've got solved the problem of
Also there are only 19 items in this list yet it prints 5 pages and repeats the items.
The overwriting occurs because you position your output absolut on the page with:
$pdf->SetXY(2, 20);
That's ok for the first time. Your next content (next loop) has to consider the actual Y position. You can get this with the `GetY()' method.
$pdf->SetXY(2, 20 + $pdf->GetY());
should take care of that.