How to put column headings of a table on each page in tcpdf?

This may be a bit late but I came up with a simple way to have headers on each page. Get the count of $data and use the array_slice function to populate a page worth of data rows. Output the table and then loop back to the next page. I use 54 rows per page with a font size of 9.

//Data loading
$data = $pdf->LoadData('text.txt');
$datacount = count($data);
$i = 0;
while ($i < $datacount) {
    $dataout = array_slice($data, $i, 54, false);
    $pdf->AddPage();
    // print colored table      
    $pdf->ColoredTable($header, $dataout);
    $i = $i + 54;
}


Two methods come to my mind.

Method 1: Create your own custom header function (like in TCPDF example 3) that automatically adds the header cells to each page. In this scenario, you would move the header cell placement code into a Header method in your class and draw the headers to a fixed position on the page. Since the Header method is called automatically when a new page is added, your table headers would then be added as soon as the page is created. This is certainly faster and less intensive than method 2, but it can be a little tricky.

Method 2: Use TCPDF transactions to detect when a row is about to break the page, and then rollback. You would then AddPage yourself; draw the headers again; and continue with display. You can do this by getting the number of pages before you make the row, and comparing it to the number of pages after. An example of how you might do it this way as been provided:

    //Separated Header Drawing into it's own function for reuse.
    public function DrawHeader($header, $w) {
        // Colors, line width and bold font
        // Header
        $this->SetFillColor(233, 136, 64);
        $this->SetTextColor(255);
        $this->SetDrawColor(128, 0, 0);
        $this->SetLineWidth(0.3);
        $this->SetFont('', 'B');        
        $num_headers = count($header);
        for($i = 0; $i < $num_headers; ++$i) {
            $this->Cell($w[$i], 7, $header[$i], 1, 0, 'C', 1);
        }
        $this->Ln();
        // Color and font restoration
        $this->SetFillColor(224, 235, 255);
        $this->SetTextColor(0);
        $this->SetFont('');
    }

    // Colored table
    public function ColoredTable($header,$data) {
        $w = array(10, 40, 20, 20, 20, 20, 20);
        $this->DrawHeader($header, $w);

        // Data
        $fill = 0;
        foreach($data as $row) {
            //Get current number of pages.
            $num_pages = $this->getNumPages();
            $this->startTransaction();
            $this->Cell($w[0], 6, $row[0], 'LR', 0, 'C', $fill);
            $this->Cell($w[1], 6, $row[1], 'LR', 0, 'L', $fill);
            $this->Cell($w[2], 6, $row[2], 'LR', 0, 'C', $fill);
            $this->Cell($w[3], 6, $row[3], 'LR', 0, 'C', $fill);
            $this->Cell($w[4], 6, $row[4], 'LR', 0, 'C', $fill);
            $this->Cell($w[5], 6, $row[5], 'LR', 0, 'C', $fill);
            $this->Cell($w[6], 6, $row[6], 'LR', 0, 'C', $fill);
            $this->Ln();
            //If old number of pages is less than the new number of pages,
            //we hit an automatic page break, and need to rollback.
            if($num_pages < $this->getNumPages())
            {
                //Undo adding the row.
                $this->rollbackTransaction(true);
                //Adds a bottom line onto the current page. 
                //Note: May cause page break itself.
                $this->Cell(array_sum($w), 0, '', 'T');
                //Add a new page.
                $this->AddPage();
                //Draw the header.
                $this->DrawHeader($header, $w);
                //Re-do the row.
                $this->Cell($w[0], 6, $row[0], 'LR', 0, 'C', $fill);
                $this->Cell($w[1], 6, $row[1], 'LR', 0, 'L', $fill);
                $this->Cell($w[2], 6, $row[2], 'LR', 0, 'C', $fill);
                $this->Cell($w[3], 6, $row[3], 'LR', 0, 'C', $fill);
                $this->Cell($w[4], 6, $row[4], 'LR', 0, 'C', $fill);
                $this->Cell($w[5], 6, $row[5], 'LR', 0, 'C', $fill);
                $this->Cell($w[6], 6, $row[6], 'LR', 0, 'C', $fill);
                $this->Ln();
            }
            else
            {
                //Otherwise we are fine with this row, discard undo history.
                $this->commitTransaction();
            }
            $fill=!$fill;
        }
        $this->Cell(array_sum($w), 0, '', 'T');
    }

Another method would be to simply use THEAD tag in html.

<table>
<thead>
    <tr><th>Heading</th></tr>
</thead>
<tbody>
    <tr><td>Many rows...</td></tr>
    <tr><td>of data</td></tr>
</tbody>
</table>

It appears TCPDF repeats the heading on every page.

Tags:

Php

Tcpdf