phpyii2mpdfkartik-vtable-footer

mPDF - Show table footer (tfoot) only once


I am creating a PDF file from HTML table using Kartik's mPDF extension for Yii2. The table footer is visible in all pages, but I want it to be shown only at the end of the entire table (in the last page).

How can I achieve this? I am using GridView and ArrayDataProvider to create the table.

display: table-row-group; is not supported by mPDF for tfoot element and therefore is not working inside the PDF.


Solution

  • I have come to a solution that requires the table to be changed.

    Instead of using tfoot element for my footer I am just adding one row with footer data inside tbody and adding styling to that. Since mPDF doesn't support :last-child selector, I had to use the supported :nth-child() selector:

    $pdf->cssInline = 'tbody tr:nth-child(' . $row_count . ') td { font-weight: bold; }';
    

    I set $row_count after adding the additional row by calling count() on the data array. To show correct total item count in the GridView, I added custom layout with the $row_count value minus 1:

    $pdf->content = GridView::widget([
        'dataProvider' => $dataProvider,
        'columns' => $this->columns,
        'showFooter' => false,
        'layout' => '
            <div>' . Yii::t('app', 'Total <strong>{count}</strong> items.', ['count' => ($this->row_count - 1)]) . '</div>
            {items}
        '
    ]);
    

    The resulting PDF file has correct item count before the table and properly styled "footer" only at the very end of the table.

    I hope my solution is helpful for someone else. Please post an answer if you have found a better solution.