I am trying to construct a PDF table with fPDF and PHP and it creates a table if it fits one page. However if the table does not fit in one page, although the first page is good, on the second page and on the other pages the table is not regular. Here is an example of my pdf output:
https://rapidshare.com/files/486723233/results.pdf
And here is my code for create this table:
//FILL THE TABLE
$max_number_of_lines = 0;
for ($index = 0; $index < count($subjects); $index++,$temp=0,$row_height = 15,$max_number_of_lines = 0) {
//Account height of row, all will be same
//Issue a page break first if needed
if($this->GetY()+$row_height > $this->PageBreakTrigger)
$this->AddPage($this->CurOrientation);
foreach ($subjects[$index] as $fields) {
$myString = mb_convert_encoding($fields, "iso-8859-9", "auto");
$number_of_lines = ceil(($this->GetStringWidth($fields))/$fieldLengthArray[$temp]);
if ($row_height < ceil($number_of_lines * 15)) {
$row_height = ceil($number_of_lines * 15);
$max_number_of_lines = $number_of_lines+1;
}
$temp++;
}
$temp=0;
foreach ($subjects[$index] as $myFields) {
//$this->SetAutoPageBreak(true,$row_height);
$this->SetY($currentY);
$this->SetX($currentX);
$myString = mb_convert_encoding($myFields, "iso-8859-9", "auto");
$number_of_lines = ceil(($this->GetStringWidth($myFields))/$fieldLengthArray[$temp]);
if ($number_of_lines < $max_number_of_lines-1) {
$this->Rect($currentX, $currentY, $fieldLengthArray[$temp], $row_height);
//Draw the border
//$this->Rect($x, $y, $w, $h);
$this->MultiCell($fieldLengthArray[$temp], ceil($row_height/$max_number_of_lines) ,
$myString,0, 'L', $fill);
}
else {
//Draw the border
//$this->Rect($x, $y, $w, $h);
$this->Rect($currentX, $currentY, $fieldLengthArray[$temp], $row_height);
$this->MultiCell($fieldLengthArray[$temp], ceil($row_height/$max_number_of_lines),
$myString,0, 'L', $fill);
}
$currentX += $fieldLengthArray[$temp];
$temp++;
//$this->Ln();
}
$this->Ln($row_height);
$this->SetY($currentY);
$currentX = $this->GetX();
$currentY += $row_height;
}
$this->SetFillColor(238);
$this->SetLineWidth(0.2);
$this->SetFont('arial_tr', '', 10);
$this->Ln();
}
It is part of my function, Generate table and $subjects
is the information i get from database.
$subjects = array(array())
I couldn't understand why it is not regular in the second and other pages when it is regular on first page. I also check for end of the page.
It's happening because with one cell you're triggering a page break in the page.
Either you disable in the fpdf the "auto page break" or you have to calculate in advance the height of the cells for one row.
As an alternative you can check out my fpdf table script: http://interpid.eu/fpdf-table