FPDF Table - Example 3 - Table Details¶
Preview¶
Result pdf: ~
name~
example-table-3-detailed.php¶
<?php
/**
* Pdf Advanced Table - Example
* Copyright (c), Interpid, http://www.interpid.eu
*/
require_once __DIR__ . "/../autoload.php";
use Interpid\PdfLib\Multicell;
use Interpid\PdfLib\Table;
use Interpid\PdfExamples\PdfFactory;
use Interpid\PdfExamples\PdfSettings;
$factory = new PdfFactory();
//get the PDF object
$pdf = PdfFactory::newPdf('table');
//define some background colors
$bgColor1 = [234, 255, 218];
$bgColor2 = [165, 250, 220];
$bgColor3 = [255, 252, 249];
$bgColor4 = [86, 155, 225];
$bgColor5 = [207, 247, 239];
$bgColor6 = [246, 211, 207];
$bgColor7 = [216, 243, 228];
/**
* Create the pdf Table object
* Alternative you can use the Singleton Instance
*
* @example : $table = Table::getInstance($pdf);
*/
$table = new Table($pdf);
// Set the styles for the advanced table
PdfSettings::setTableStyles($table);
//default text color
$pdf->SetTextColor(118, 0, 3);
//create an advanced multicell
$multicell = Multicell::getInstance($pdf);
$multicell->setStyle('s1', 8, '', '118,0,3', 'helvetica');
$multicell->setStyle('s2', 6, '', '0,49,159', 'helvetica');
$multicell->multiCell(100, 4, "<s1>Example 1 - Very Simple Table</s1>", 0);
$pdf->Ln(1);
require('table_example1.php');
$pdf->Ln(10);
$txt = "<s1>Example 2 - More detailed Table</s1>\n<s2>\t- Table Align = Center\n\t- The header has multiple lines\n\t- Colspanning Example\n\t- Rowspanning Example\n\t- Text Alignments\n\t- Properties overwriting</s2>";
$pdf->SetX(60);
$multicell->multiCell(100, 2.5, $txt, 0);
$pdf->Ln(1);
require('table_example2.php');
$pdf->Ln(10);
$txt = "<s1>Example 3 - Table split end of the page</s1>\n<s2>\t- This is the table from Example 2 at the end of the page\n\t- Splitting mode = ON, you can see that the cells are splitted</s2>";
$pdf->SetXY(60, 215);
$multicell->multiCell(100, 2.5, $txt, 0);
$pdf->Ln(1);
$tableSplitMode = true;
require('table_example2.php');
$pdf->Ln(10);
$txt = "<s1>Example 4 - Table split end of the page</s1>\n<s2>\t- This is the table from Example 2 at the end of the page\n\t- Splitting mode = OFF. In this case the cells are NOT splitted</s2>";
$pdf->SetXY(60, 215);
$multicell->multiCell(100, 2.5, $txt, 0);
$pdf->Ln(1);
$tableSplitMode = false;
require('table_example2.php');
//send the pdf to the browser
$pdf->Output();
table_example1.phptable_example1.php
<?php
$columns = 3;
//Initialize the table, 3 columns
$table->initialize([40, 50, 30]);
$header = [];
//Table Header
for ($i = 0; $i < $columns; $i++) {
$header[$i]['TEXT'] = "Header #" . ($i + 1);
}
//add the header
$table->addHeader($header);
for ($j = 1; $j < 5; $j++) {
$row = [];
$row[0]['TEXT'] = "Line $j Text 1"; //text for column 0
$row[0]['TEXT_ALIGN'] = 'L'; //text align
//$row[0]['LINE_SIZE'] = 7; //text align
$row[1]['TEXT'] = "Line $j Text 2"; //text for column 1
$row[2]['TEXT'] = "Line $j Text 3"; //text for column 2
$row[2]['TEXT_ALIGN'] = 'R'; //text align
//add the row
$table->addRow($row);
//break;
}
//close the table
$table->close();
table_example2.phptable_example2.php
<?php
$columns = 3;
//Initialize the table, 3 columns
$table->initialize([40, 50, 30]);
$header = [];
//Table Header
for ($i = 0; $i < $columns; $i++) {
$header[$i]['TEXT'] = "Header #" . ($i + 1);
}
$table->addHeader($header);
$table->addHeader($header);
$table->addHeader($header);
$table->setHeaderProperty(1, 'TEXT', 'Rowspan/Colspan can be made also in the header.');
$table->setHeaderProperty(1, 'ROWSPAN', 2);
$table->setHeaderProperty(1, 'COLSPAN', 2);
$table->setHeaderProperty(1, 'BACKGROUND_COLOR', $bgColor4);
$table->setHeaderProperty(1, 'TEXT_COLOR', [0, 0, 0]);
if (isset($tableSplitMode)) {
$table->setSplitMode($tableSplitMode);
}
for ($j = 1; $j <= 15; $j++) {
$row = [];
$row[0]['TEXT'] = "Line $j Text 1";
$row[1]['TEXT'] = "Line $j Text 2";
$row[2]['TEXT'] = "Line $j Text 3";
if ($j == 1) {
$row[0]['BACKGROUND_COLOR'] = $bgColor5;
$row[0]['TEXT'] = 'Colspan Example';
$row[0]['COLSPAN'] = 2;
}
if ($j == 2) {
$row[1]['BACKGROUND_COLOR'] = $bgColor6;
$row[1]['TEXT'] = 'Rowspan Example';
$row[1]['ROWSPAN'] = 2;
}
if ($j == 4) {
$row[1]['BACKGROUND_COLOR'] = $bgColor7;
$row[1]['TEXT'] = 'Rowspan and Colspan Example';
$row[1]['ROWSPAN'] = 2;
$row[1]['COLSPAN'] = 2;
}
if (($j >= 7) and ($j <= 9)) {
$row[0]['TEXT'] = "More lines...\nLine2\nLine3";
}
if ($j == 7) {
$row[1]['TEXT'] = "Top Left Align";
$row[1]['VERTICAL_ALIGN'] = 'T';
$row[1]['TEXT_ALIGN'] = 'L';
$row[2]['TEXT'] = "Bottom Right Align";
$row[2]['VERTICAL_ALIGN'] = 'B';
$row[2]['TEXT_ALIGN'] = 'R';
}
if ($j == 8) {
$row[1]['TEXT'] = "Top Center Align";
$row[1]['VERTICAL_ALIGN'] = 'T';
$row[1]['TEXT_ALIGN'] = 'C';
$row[2]['TEXT'] = "Bottom Center Align";
$row[2]['VERTICAL_ALIGN'] = 'B';
$row[2]['TEXT_ALIGN'] = 'C';
}
if ($j == 9) {
$table->setStyle('sd1', 6, '', '0,49,159', 'helvetica');
$table->setStyle('sd2', 5, '', '140,12,12', 'helvetica');
$table->setStyle('sd3', 6, '', '0,5,90', 'helvetica');
$row[1]['TEXT'] = "<sd1>This is just a longer text, justified align, middle vertical align to demonstrate some other capabilities. Test text. Test text.</sd1>
<sd3>\tSettings:</sd3>
<p size='15' > ~~~</p><sd2>- Rowspan=4</sd2>
<p size='15' > ~~~</p><sd2>- Colspan=2</sd2>
";
$row[1]['VERTICAL_ALIGN'] = 'M';
$row[1]['TEXT_ALIGN'] = 'J';
$row[1]['COLSPAN'] = 2;
$row[1]['ROWSPAN'] = 4;
$row[1]['LINE_SIZE'] = 2.3;
}
if ($j == 14) {
$row[1]['TEXT'] = "Cell Properties Overwriting Example";
$row[1]['TEXT_FONT'] = 'Times';
$row[1]['TEXT_SIZE'] = 7;
$row[1]['TEXT_TYPE'] = 'B';
$row[1]['BACKGROUND_COLOR'] = [240, 240, 209];
$row[1]['BORDER_COLOR'] = [100, 100, 200];
$row[1]['VERTICAL_ALIGN'] = 'T';
$row[1]['TEXT_ALIGN'] = 'C';
}
$table->addRow($row);
}
//close the table
$table->close();