PHPackages                             voilab/tctable - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [PDF &amp; Document Generation](/categories/documents)
4. /
5. voilab/tctable

ActiveLibrary[PDF &amp; Document Generation](/categories/documents)

voilab/tctable
==============

Create quickly and efficiently tables without HTML formatting and with advanced page break options

2.0.0(3mo ago)512.0k↓22.5%4[2 issues](https://github.com/voilab/tctable/issues)[1 PRs](https://github.com/voilab/tctable/pulls)MITPHPPHP &gt;=8.0.0CI failing

Since Mar 23Pushed 3mo ago3 watchersCompare

[ Source](https://github.com/voilab/tctable)[ Packagist](https://packagist.org/packages/voilab/tctable)[ Docs](http://www.voilab.ch)[ RSS](/packages/voilab-tctable/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (3)Versions (26)Used By (0)

Voilab TcTable for TCPDF
========================

[](#voilab-tctable-for-tcpdf)

Install
-------

[](#install)

Via Composer

Create a composer.json file in your project root:

```
{
    "require": {
        "voilab/tctable": "1.*"
    }
}
```

```
$ composer require voilab/tctable
```

Usage
-----

[](#usage)

The goal of this library is not to replace a complex HTML table with row and cell spans. It's mainly useful if you want to display loads of lines (for an invoice, etc.) and be sure page breaks are in the right place. You can adapt the number of widow lines (minimum of lines that must appear on the last page), and use plugins to customize the flow (see below).

### Basic usage

[](#basic-usage)

```
$pdf = new \TCPDF();
$minRowHeight = 6; //mm

$tctable = new \voilab\tctable\TcTable($pdf, $minRowHeight);
$tctable->setColumns([
    'description' => [
        'isMultiLine' => true,
        'header' => 'Description',
        'width' => 100
        // check inline documentation to see what options are available.
        // Basically, it's everything TCPDF Cell and MultiCell can eat.
    ],
    'quantity' => [
        'header' => 'Quantity',
        'width' => 20,
        'align' => 'R'
    ],
    'price' => [
        'header' => 'Price',
        'width' => 20,
        'align' => 'R'
    ]
]);

// get rows data
$rows = getMyDatasAsMyObjs();

// add a page so the content can be printed on something
$pdf->AddPage();
// draw body
$tctable->addBody($rows, function (\voilab\tctable\TcTable $table, \MyObj $row) {
    $change_rate = 0.8;
    // map row data to TcTable column definitions
    return [
        'description' => $row->getDescription(),
        'quantity' => $row->getQuantity(),
        'price' => $row->getPrice() * $change_rate
    ];
});

$pdf->Output('tctable.pdf', 'I');
```

### Plugins

[](#plugins)

#### Have a column that fit the remaining page width

[](#have-a-column-that-fit-the-remaining-page-width)

```
$tctable
    ->addPlugin(new \voilab\tctable\plugin\FitColumn('text'))
    ->addColumn('text', [
        'isMultiLine' => true,
        'header' => 'Text'
        // no need to set width here, the plugin will calculate it for us,
        // depending on the other columns width
    ]);
```

#### Stripe rows

[](#stripe-rows)

```
$tctable
    // set true to have the first line with colored background
    ->addPlugin(new \voilab\tctable\plugin\StripeRows(true));
```

#### Widows management

[](#widows-management)

```
// set the minimum elements you want to see on the last page (if any)
$nb = 4;
// set a footer margin. Useful when you have lot of lines, and a total as the
// last one. If you want the total to appears at least with 4 lines, you have
// to add to the pageBreakTrigger margin this line height: the footer
$mFooter = 10; // i.e: mm

$tctable->addPlugin(new \voilab\tctable\plugin\Widows($nb, $mFooter));
```

#### Debug

[](#debug)

The TcTable comes with a debug plugin tool that display datas passed in each event.

```
// create the plugin. You can define which events to listen (default to rowadd,
// rowadded, rowskipped, headeradd, headeradded, pageadd and pageadded) and the
// printer object (default to an HTML output with )
$debug = new \voilab\tctable\plugin\Debug();
$debug
    ->setBounds($fromIndex = 0, $numberOfRows = 2, $dieWhenOutOfBounds = true);

// $dieWhenOutOfBounds will stop the script with die(). Usefull for quick
// debug

$tctable->addPlugin($debug);
```

You can extend the printer object by creating your own:

```
class MyDebugPrinter implements \voilab\tctable\plugin\debug\PrinterInterface {

    public function output(TcTable $table, array $data) {
        // do something, log, etc.
    }
}

// ... create an instance of debug plugin

// you can set printer the way below, or via the 2nd argument in plugin
// constructor.
$debug->setPrinter(new MyDebugPrinter());
```

#### Advanced plugin: draw a subtotal for a column at end of each page

[](#advanced-plugin-draw-a-subtotal-for-a-column-at-end-of-each-page)

We can go further by calculating a sum for a column, and display the current sum at the end of the page, and finally report it on the next page.

```
