tecnickcom / TCPDF

Official clone of PHP library to generate PDF documents and barcodes
https://tcpdf.org
Other
4.18k stars 1.51k forks source link

Table with different row heights across multiple pages #637

Open rodrigo1406 opened 1 year ago

rodrigo1406 commented 1 year ago

https://stackoverflow.com/questions/76917804/how-to-guarantee-tcpdf-table-will-have-equal-row-heights-across-multiple-pages

I need to create a PDF downloadable file with PHP. I'm using TCPDF, but when the table span multiple pages, the last row in each page does not have the same size as the others.

Sometimes it's shorter:

image

Sometimes it's taller:

image

Only in the last page (where it doesn't come close to the end of the page), the last row has the same height as the other rows.

Minimum reproducible version of my code:

require_once('TCPDF-master/tcpdf.php');
class MYPDF extends TCPDF {
    public function Header() {
    }
    public function Footer() {
    }
}
$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->AddPage();
$pdf->SetMargins(10, 10, $keepmargins=true);
$pdf->SetAutoPageBreak(TRUE, 10);
$cells = '';
$n = 0;
while ($n < 100) {
    if (++$n % 2 == 1) {
        $cells .= "<tr><td>Cell $n</td>";
    } else {
        $cells .= "<td>Cell $n</td></tr>";
    }
}
$pdf->writeHTML("<table border=\"1\" style=\"line-height:3cm;\">$cells</table>", true, false, true, false, '');
$pdf->Output('Test.pdf','F');

How can I guarantee that all the rows will have exactly the same height?

rodrigo1406 commented 1 year ago

Adding manual page breaks solved the issue:

$cells = '';
$n = 0;
while ($n < 100) {
    if (++$n % 2 == 1) {
        $cells .= "<tr><td>Cell $n</td>";
    } else {
        $cells .= "<td>Cell $n</td></tr>";
    }
    if ($n % 18 == 0) {
        $pdf->writeHTML("<table border=\"1\" style=\"line-height:3cm;\">$cells</table>", true, false, true, false, '');
        $pdf->AddPage();
        $pdf->SetMargins(10, 10, $keepmargins=true);
        $pdf->SetAutoPageBreak(TRUE, 10);
        $cells = '';
    }
}
if ($cells) {
    $pdf->writeHTML("<table border=\"1\" style=\"line-height:3cm;\">$cells</table>", true, false, true, false, '');
}