Project

General

Profile

Fpdf Table User Manual

Overview

In order to create a PDF Advanced Table the following steps are required:

  1. have a valid Pdf object
  2. create the Ppf Advanced Table and pass the Ppf object as a parameter to the constructor
  3. set the table styles(optional)
  4. initialize the table
  5. add header rows(optional)
  6. add table(body) rows
  7. close the table

Some of these steps are optional.

Create the PDF object

Please refer to the www.fpdf.org class manual in order to get this done correctly.

// create new PDF document
$oPdf = new FPDF();
$oPdf->Open();
$oPdf->SetMargins(20, 20, 20);

//set default font/colors
$oPdf->SetFont('arial', '', 11);
$oPdf->SetTextColor(200, 10, 10);
$oPdf->SetFillColor(254, 255, 245);

//add the page
$oPdf->AddPage();
$oPdf->AliasNbPages();

Create the Advanced Table object

Just call the advanced table constructor and pass the Pdf Object as a parameter to the constructor.

//Create the Advanced Table object
$oTable = new FpdfTable($oFpdf);

Set the tag styles

All the table cells(header and rows) are fully functional Fpdf Avanced Multicells. This adds a lot of flexibility when creating your table.

You can define your own styles that defines how the text in the specified tags will look like.

//Set the tag styles
$oTable->setStyle("p","times","",10,"130,0,30");
$oTable->setStyle("b","times","",9,"80,80,260");
$oTable->setStyle("h1","arial","",10,"0,151,200");
$oTable->setStyle("h2","times","BI",12,"0,0,120");

Initialize the table

Simple initialization

Just specify an array with the column widths. The size of the array will be used as the number of columns.

//Initialize the table class, the table will have 3 columns with the specified widths
$oTable->initialize(array(40, 40, 50));

Table configuration

Table configuration file

The default table configuration file can be found in: table.config.php file. The values specified in this file are used by default. Change these values to your own needs.

The configuration file content looks like the following and it contains all the values that you can specify for a header/row.

/**
* Default configuration values for the PDF Advanced table
*/

    $aDefaultConfiguration = array(

        'TABLE' => array(
                'TABLE_ALIGN'       => 'C',                 //table align on page
                'TABLE_LEFT_MARGIN' => 0,                   //space to the left margin
                'BORDER_COLOR'      => array(0,92,177),     //border color
                'BORDER_SIZE'       => '0.3',               //border size
                'BORDER_TYPE'       => '1',                 //border type, can be: 0, 1
        ),

        'HEADER' => array(
                'TEXT_COLOR'        => array(220,230,240),  //text color
                'TEXT_SIZE'         => 8,                   //font size
                'TEXT_FONT'         => 'Arial',             //font family
                'TEXT_ALIGN'        => 'C',                 //horizontal alignment, possible values: LRC (left, right, center)
                'VERTICAL_ALIGN'    => 'M',                 //vertical alignment, possible values: TMB(top, middle, bottom)
                'TEXT_TYPE'         => 'B',                 //font type
                'LINE_SIZE'         => 4,                   //line size for one row
                'BACKGROUND_COLOR'  => array(41, 80, 132),  //background color
                'BORDER_COLOR'      => array(0,92,177),     //border color
                'BORDER_SIZE'       => 0.2,                 //border size
                'BORDER_TYPE'       => '1',                 //border type, can be: 0, 1 or a combination of: "LRTB" 
                'TEXT'              => ' ',                 //default text
                //padding
                'PADDING_TOP'       => 0,                   //padding top
                'PADDING_RIGHT'     => 1,                   //padding right
                'PADDING_LEFT'      => 1,                   //padding left
                'PADDING_BOTTOM'    => 0,                   //padding bottom
        ),

        'ROW' => array(
                'TEXT_COLOR'        => array(0,0,0),        //text color
                'TEXT_SIZE'         => 6,                   //font size
                'TEXT_FONT'         => 'Arial',             //font family
                'TEXT_ALIGN'        => 'C',                 //horizontal alignment, possible values: LRC (left, right, center)
                'VERTICAL_ALIGN'    => 'M',                 //vertical alignment, possible values: TMB(top, middle, bottom)
                'TEXT_TYPE'         => '',                  //font type
                'LINE_SIZE'         => 4,                   //line size for one row
                'BACKGROUND_COLOR'  => array(255,255,255),  //background color
                'BORDER_COLOR'      => array(0,92,177),     //border color
                'BORDER_SIZE'       => 0.1,                 //border size
                'BORDER_TYPE'       => '1',                 //border type, can be: 0, 1 or a combination of: "LRTB" 
                'TEXT'              => ' ',                 //default text
                //padding
                'PADDING_TOP'       => 0,
                'PADDING_RIGHT'     => 1,
                'PADDING_LEFT'      => 1,
                'PADDING_BOTTOM'    => 0,
        ),
    );

Overwriting the default configuration values

When creating the table object the second parameter of the constructor can be an array similar to the one from the Table Configuration File. This array does not need to contain the complete configuration structure, only the values that are overwritten.

A simple configuration array:

//changing header text color and row text color and size
$aCustomConfiguration = array(
        'HEADER' => array(
                'TEXT_COLOR'        => array(25, 60, 170),  //text color
        ),
        'ROW' => array(
                'TEXT_COLOR'        => array(225, 20, 0),   //text color
                'TEXT_SIZE'         => 6,                   //font size
        ),
);

A more complex configuration array:

//change multiple values
$aCustomConfiguration = array(
        'TABLE' => array(
                'TABLE_ALIGN'       => 'L',                 //left align
                'BORDER_COLOR'      => array(0, 0, 0),      //border color
                'BORDER_SIZE'       => '0.5',               //border size
        ),

        'HEADER' => array(
                'TEXT_COLOR'        => array(25, 60, 170),  //text color
                'TEXT_SIZE'         => 9,                   //font size
                'LINE_SIZE'         => 6,                   //line size for one row
                'BACKGROUND_COLOR'  => array(182, 240, 0),  //background color
                'BORDER_SIZE'       => 0.5,                 //border size
                'BORDER_TYPE'       => 'B',                 //border type, can be: 0, 1 or a combination of: "LRTB" 
                'BORDER_COLOR'      => array(0, 0, 0),      //border color
        ),
        'ROW' => array(
                'TEXT_COLOR'        => array(225, 20, 0),   //text color
                'TEXT_SIZE'         => 6,                   //font size
                'BACKGROUND_COLOR'  => array(232, 255, 209),  //background color
                'BORDER_COLOR'      => array(150, 255, 56),   //border color
        ),
);

Passing the configuration array to the constructor

//Initialize the table class, 3 columns and the custom configuration array
$oTable->initialize(array(40, 50, 30), $aCustomConfiguration);

Table configuration values

All the possible configuration values for the table configuration can be found in the Table Configuration File. See the TABLE section.

Header row(s)

You can add none, one or multiple header rows to a table. The header rows are added to the table on every new page.

No header

Just don't add any header row. The table will have no header.

Single header row

$aHeader = array(
    array('TEXT'  => 'Header 1'),
    array('TEXT'  => 'Header 2'),
    array('TEXT'  => 'Header 3'),
);
//add the header line
$oTable->addHeader($aHeader);

Multiple header rows

//header row configuration
$aHeader = array(
    array('TEXT'  => 'Header 1'),
    array('TEXT'  => 'Header 2'),
    array('TEXT'  => 'Header 3'),
);

//add the first header row
$oTable->addHeader($aHeader);

//header row configuration
$aHeader = array(
    array( 'TEXT'     => 'Header Row 2'),
    array( 'TEXT'     => 'Header Row 2 / 2-3',
           'COLSPAN'  => 2,//colspan
          ),
    //due to the colspan, the third header can be ignored
);

//add the second header row
$oTable->addHeader($aHeader);

//you can even add the same header again...
$oTable->addHeader($aHeader);

//add an empty header line
$oTable->addHeader();

Header configuration values

All the possible configuration values for the header can be found in the Table Configuration File. See the HEADER section.

All configuration parameters can be overwritten in the configuration array

$aHeader = array(
    array('TEXT'        => 'Header 1', //header text
          'PADDING_TOP' => 5, //add some padding,
          'TEXT_TYPE'   => 'B', //bold text
          ),
    array('TEXT'  => 'Header 2'),
    array('TEXT'  => 'Header 3'),
);

Table row(s)

Adding table rows

//add some table rows
for ($j=1; $j<5; $j++)
{
    $aRow = Array();
    $aRow[0]['TEXT'] = "Line $j Text 1";    //text for column 0
    $aRow[1]['TEXT'] = "Line $j Text 2";    //text for column 1
    $aRow[2]['TEXT'] = "Line $j Text 3";    //text for column 2

    //override some settings for row 2
    if (2 == $j){
        $aRow[1]['TEXT_ALIGN'] = 'L';
        $aRow[1]['TEXT'] = "<pp>This is a <b>Multicell</b></pp>";
    }
    //add the row
    $oTable->addRow($aRow);
}

Simple row

$aRow = array(
    array('TEXT'  => 'I am cell 1'),
    array('TEXT'  => 'I am cell 2'),
    array('TEXT'  => 'I am cell 3'),
);

//add the row to the table
$oTable->addRow($aRow);

A more complex row

$aRow = array(
    array('TEXT'  => 'I am cell 1',
          'TEXT_ALIGN' => 'L'),
    array('TEXT'  => 'I am cell 2'),
    array('TEXT'  => 'I am cell 3'),
);

//add the row to the table
$oTable->addRow($aRow);

Row cell as multicell

$aRow = array(
    array('TEXT'  => '<t>This is a <b>Multicell</b></t>',
          'TEXT_ALIGN' => 'L'),
    array('TEXT'  => '<t>This is a <b>Multicell</b></t>'),
    array('TEXT'  => '<t>This is a <b>Multicell</b></t>'),
);

//add the row to the table
$oTable->addRow($aRow);

Row configuration values

All the possible configuration values for the rows can be found in the Table Configuration File. See the ROW section.

All configuration parameters can be overwritten in the row configuration array.

Close the table

This is the action that actually adds the table content to the PDF document.

//close the table
$oTable->close();