TCPDF Table - Example 1 - Overview¶
Preview¶
Result pdf: ~
name~

example-table-1-overview.php¶
<?php
/**
* Pdf Advanced Table - Example
* Copyright (c), Interpid, http://www.interpid.eu
*/
require_once __DIR__ . '/../autoload.php';
use Interpid\PdfLib\Multicell;
use Interpid\PdfExamples\PdfFactory;
use Interpid\PdfExamples\PdfSettings;
$factory = new PdfFactory();
//get the PDF object
$pdf = PdfFactory::newPdf('table');
// Create the Advanced Multicell Object and inject the PDF object
$multicell = new Multicell($pdf);
// Set the styles for the advanced multicell
PdfSettings::setMulticellStyles($multicell);
//simple table
$multicell->multiCell(0, 5, "<p size='10' > ~~~Simple table:</p>");
require(__DIR__ . '/table/code-example1.php');
//cells can be multicells and images
$pdf->Ln(10);
$multicell->multiCell(0, 5, "<p size='10' > ~~~Cells can be <b>advanced multicells</b> and <b>images</b>:</p>");
require(__DIR__ . '/table/code-example2.php');
//example - Multiple header rows, rowspans, colspans
$pdf->Ln(10);
$multicell->multiCell(0, 5, "<p size='10' > ~~~Multiple header rows, rowspans, colspans:</p>");
require(__DIR__ . '/table/code-example3.php');
//example - Transparent background
$pdf->Ln(10);
$multicell->multiCell(0, 5, "<p size='10' > ~~~ Transparent Background:</p>");
require(__DIR__ . '/table/code-example-transparent.php');
//example - all parameters can be overwritten
$pdf->Ln(10);
$multicell->multiCell(0, 5, "<p size='10' > ~~~Different alignments:</p>");
require(__DIR__ . '/table/code-example-alignments.php');
//send the pdf to the browser
$pdf->Output();
code-example1.phpcode-example1.php
<?php
use Interpid\PdfExamples\MyPdf;
use Interpid\PdfLib\Table;
if (!isset($pdf)) {
$pdf = new MyPdf();
}
if (!isset($table)) {
$table = new Table($pdf);
}
$table->setStyle('p', 7, '', '130,0,30', 'helvetica');
$table->setStyle('b', 7, 'B', '130,0,30', 'helvetica');
$columns = 3;
/**
* Set the tag styles
*/
$table->initialize([20, 30, 50]);
$header = array(
['TEXT' => 'Header #1'],
['TEXT' => 'Header #2'],
['TEXT' => 'Header #3'],
);
//add the header row
$table->addHeader($header);
for ($j = 1; $j < 3; $j++) {
$row = [];
$row[0]['TEXT'] = "Line $j";
$row[1]['TEXT'] = "Lorem ipsum dolor sit amet...";
$row[2]['TEXT'] = "<p>Simple text\n<b>Bold text</b></p>";
//add the data row
$table->addRow($row);
}
//close the table
$table->close();
code-example2.phpcode-example2.php
<?php
use Interpid\PdfExamples\MyPdf;
use Interpid\PdfExamples\PdfSettings;
use Interpid\PdfLib\Table;
if (!isset($pdf)) {
$pdf = new MyPdf();
}
$table = new Table($pdf);
$table->setStyle('p', 7, '', '130,0,30', 'helvetica');
$table->setStyle('b', 7, 'B', '130,0,30', 'helvetica');
$table->setStyle('bi', 7, 'BI', '0,0,120', 'helvetica');
$columns = 3;
/**
* Set the tag styles
*/
$table->initialize([20, 30, 80]);
$header = [
['TEXT' => 'Header #1'],
['TEXT' => 'Header #2'],
['TEXT' => 'Header #3']
];
//add the header row
$table->addHeader($header);
$imageCell = [
'TYPE' => 'IMAGE',
'FILE' => PDF_RESOURCES_IMAGES . '/dice.jpg',
'WIDTH' => 10
];
//row 1 - add data as Array
$row = [];
$row[0]['TEXT'] = "Line <b>1</b>";
$row[1] = array(
'TYPE' => 'IMAGE',
'FILE' => PDF_RESOURCES_IMAGES . '/dice.jpg',
'WIDTH' => 10
);
$row[2]['TEXT'] = "<p>All <b>table cells</b> are fully functional <bi>Advanced Multicells</bi>\nDetails on <bi href='http://www.interpid.eu'>www.interpid.eu</bi></p>";
$row[2]['ALIGN'] = 'L';
//add the data row
$table->addRow($row);
//row 2 - add data as Objects
$row = [];
//alternatively you can create directly the cell object
$row[0] = new Table\Cell\Image($pdf, PDF_RESOURCES_IMAGES . '/blog.jpg', 10);
$row[1] = array(
'TEXT' => "<p><b>SVG Images</b> are supported\n<bi>(see right image >>>)</bi></p>",
'BACKGROUND_COLOR' => PdfSettings::$colors[0]
);
$row[2] = new Table\Cell\ImageSVG($pdf, PDF_RESOURCES_IMAGES . '/Tiger.svg', 35, 35);
//add the data row
$table->addRow($row);
//close the table
$table->close();
code-example3.phpcode-example3.php
<?php
use Interpid\PdfExamples\MyPdf;
use Interpid\PdfExamples\PdfSettings;
use Interpid\PdfLib\Table;
if (!isset($pdf)) {
$pdf = new MyPdf();
}
$table = new Table($pdf);
$table->setStyle('p', 6, '', '130,0,30', 'helvetica');
$table->setStyle('b', 6, 'B', '130,0,30', 'helvetica');
$table->setStyle('bi', 6, 'BI', '0,0,120', 'helvetica');
$table->setStyle('s1', 6, 'I', '0,0,120', 'helvetica');
$table->setStyle('s2', 7, '', '110,50,120', 'helvetica');
$nColumns = 5;
/**
* Set the tag styles
*/
$table->initialize([20, 30, 40, 50]);
$header1 = PdfSettings::headerRow();
$header1[2]['TEXT'] = 'Colspan in Header';
$header1[2]['COLSPAN'] = 2;
$header2 = PdfSettings::headerRow();
$header3 = PdfSettings::headerRow();
$header2[1]['TEXT'] = "Colspan/Rowspan in Header";
$header2[1]['COLSPAN'] = 2;
$header2[1]['ROWSPAN'] = 2;
$table->addHeader($header1);
$table->addHeader($header2);
$table->addHeader($header3);
for ($i = 0; $i < 8; $i++) {
$row = PdfSettings::dataRow();
if (0 == $i) {
$row[1]['COLSPAN'] = 2;
}
if (1 == $i) {
$row[1]['COLSPAN'] = 3;
}
if (2 == $i) {
$row[1]['TEXT'] = PdfSettings::$textExtraLong . "\n\n" . PdfSettings::$textSubSuperscript;
$row[1]['ALIGN'] = 'J';
$row[1]['COLSPAN'] = 3;
$row[1]['ROWSPAN'] = 3;
}
if (3 == $i) {
$row[0] = PdfSettings::$imageCell;
}
if (5 == $i) {
$row[1] = PdfSettings::$imageCell;
$row[1]['COLSPAN'] = 2;
$row[1]['ROWSPAN'] = 2;
}
$table->addRow($row);
}
//close the table
$table->close();
code-example-transparent.phpcode-example-transparent.php
<?php
use Interpid\PdfExamples\MyPdf;
use Interpid\PdfLib\Table;
use Interpid\PdfExamples\PdfSettings;
if (!isset($pdf)) {
$pdf = new MyPdf();
}
$y = $pdf->GetY();
$pdf->SetX(50);
$pdf->Image(PDF_RESOURCES_IMAGES . "/sample-pdf.jpg");
$pdf->SetY($y);
$table = new Table($pdf);
$table->setStyle('p', 7, '', '130,0,30', 'helvetica');
$table->setStyle('b', 7, 'B', '130,0,30', 'helvetica');
$table->setStyle('bi', 7, 'BI', '0,0,120', 'helvetica');
$columns = 3;
/**
* Set the tag styles
*/
$table->initialize([20, 30, 80]);
$table->setRowConfig(array(
'BACKGROUND_COLOR' => false
));
$header = array(
array(
'TEXT' => 'Header #1'
),
array(
'TEXT' => 'Header #2'
),
array(
'TEXT' => 'Header #3'
)
);
//add the header row
$table->addHeader($header);
PdfSettings::$imageCell = array(
'TYPE' => 'IMAGE',
'FILE' => PDF_RESOURCES_IMAGES . '/dice.jpg',
'WIDTH' => 10
);
//row 1 - add data as Array
$row = [];
$row[0]['TEXT'] = "Line <b>1</b>";
$row[1] = array(
'TYPE' => 'IMAGE',
'FILE' => PDF_RESOURCES_IMAGES . '/dice.jpg',
'WIDTH' => 10
);
$row[2]['TEXT'] = "<p>All <b>table cells</b> are fully functional <bi>Advanced Multicells</bi>\nDetails on <bi href='http://www.interpid.eu'>www.interpid.eu</bi></p>";
$row[2]['ALIGN'] = 'L';
//add the data row
$table->addRow($row);
//row 2 - add data as Objects
$row = [];
//alternatively you can create directly the cell object
$row[0] = new \Interpid\PdfLib\Table\Cell\Image($pdf, PDF_RESOURCES_IMAGES . '/blog.jpg', 10);
$row[1] = new \Interpid\PdfLib\Table\Cell\Multicell($pdf, "<p>This is another <b>Multicell</b></p>");
$row[2]['TEXT'] = "<p>All <b>table cells</b> are fully functional <bi>Advanced Multicells</bi>\nDetails on <bi href='http://www.interpid.eu'>www.interpid.eu</bi></p>";
$row[2]['BACKGROUND_COLOR'] = PdfSettings::$colors[1];
//add the data row
$table->addRow($row);
//close the table
$table->close();
code-example-alignments.phpcode-example-alignments.php
<?php
use Interpid\PdfExamples\MyPdf;
use Interpid\PdfLib\Table;
use Interpid\PdfLib\Tools;
use Interpid\PdfExamples\PdfSettings;
if (!isset($pdf)) {
$pdf = new myPdf();
}
$table = new Table($pdf);
$table->setStyle('p', 6, '', '130,0,30', 'helvetica');
$table->setStyle('b', 6, 'B', '130,0,30', 'helvetica');
$table->setStyle('bi', 6, 'BI', '0,0,120', 'helvetica');
$columns = 5;
/**
* Set the tag styles
*/
$table->initialize([20, 30, 40, 50]);
$table->addHeader(PdfSettings::headerRow());
for ($i = 0; $i < 6; $i++) {
$row = PdfSettings::dataRow();
if ($i >= 0 and $i < 3) {
$row[0]['TEXT'] = "Forced\nLine\nBreaks";
$align = Tools::getNextValue(PdfSettings::$alignments, $k);
$row[1]['TEXT'] = "Align: <b>$align</b>";
$row[1]['ALIGN'] = "$align";
$align = Tools::getNextValue(PdfSettings::$alignments, $k);
$row[2]['TEXT'] = "Align: <b>$align</b>";
$row[2]['ALIGN'] = "$align";
$align = Tools::getNextValue(PdfSettings::$alignments, $k);
$row[3]['TEXT'] = "Align: <b>$align</b>";
$row[3]['ALIGN'] = "$align";
}
if ($i >= 3 and $i <= 5) {
$row[0]['TEXT'] = "Forced\nLine\nForced\nLine\nForced\nLine";
$row[1] = PdfSettings::$imageCell;
$row[1]['ALIGN'] = Tools::getNextValue(PdfSettings::$alignments, $k);
$row[2] = PdfSettings::$imageCell;
$row[2]['ALIGN'] = Tools::getNextValue(PdfSettings::$alignments, $k);
$row[3] = PdfSettings::$imageCell;
$row[3]['ALIGN'] = Tools::getNextValue(PdfSettings::$alignments, $k);
}
$table->addRow($row);
}
//close the table
$table->close();