* @copyright 2002-2026 Nicola Asuni - Tecnick.com LTD * @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE) * @link https://github.com/tecnickcom/tc-lib-pdf * * This file is part of tc-lib-pdf software library. */ // NOTE: local file reads (images, fonts, attachments) are restricted to an allowlist of // trusted paths that covers this package tree, so run the examples in place. To read assets // from other locations, list them in the 'allowedPaths' entry of the fileOptions constructor // parameter (see E047_remote_resources_security.php). // NOTE: run make fonts in the project root to generate the dependencies and example fonts. // autoloader when using Composer require __DIR__ . '/../vendor/autoload.php'; \define('OUTPUT_FILE', \realpath(__DIR__ . '/../target') . '/example.pdf'); // define fonts directory \define('K_PATH_FONTS', \realpath(__DIR__ . '/../vendor/tecnickcom/tc-lib-pdf-font/target/fonts')); // autoloader when using RPM or DEB package installation //require ('/usr/share/php/Com/Tecnick/Pdf/autoload.php'); // main TCPDF object $pdf = new \Com\Tecnick\Pdf\Tcpdf( unit: \Com\Tecnick\Pdf\Page\Unit::Millimeter, isunicode: true, subsetfont: false, compress: true, mode: \Com\Tecnick\Pdf\PdfConformance::None, objEncrypt: null, ); // ---------- $pdf->setCreator('tc-lib-pdf'); $pdf->setAuthor('Nicola Asuni'); $pdf->setSubject('tc-lib-pdf example: 043'); $pdf->setTitle('HTML Tables'); $pdf->setKeywords('TCPDF tc-lib-pdf html tables thead tbody cell borders layout'); $pdf->setPDFFilename('043_html_tables.pdf'); $pdf->setViewerPreferences(['DisplayDocTitle' => true]); $pdf->enableDefaultPageContent(); // ---------- // Insert fonts $bfont1 = $pdf->font->insert($pdf->pon, 'dejavusans', '', 10); // test images directory $imgdir = \realpath(__DIR__ . '/../vendor/tecnickcom/tc-lib-pdf-image/test/images/'); // ---------- $page1 = $pdf->addPage(); $subtable = '
ab
cd
'; $html1 = '

HTML TABLES (A)

# RIGHT align LEFT align 4A
1 A1 example link column span. One two tree four five six seven eight nine ten.
line after br
small text normal subscript normal superscript normal bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
  1. first
    1. sublist
    2. sublist
  2. second
small small small small small small small small small small small small small small small small small small small small
4B
' . $subtable . ' A2 € € € & è è
A2 € € € & è è
Red Yellow BG 4C
1A 2AA
2AB
2AC
4D
1B 4E
1C 2C 3C 4F
'; $pdf->addHTMLCell(html: $html1, posx: 20, posy: 10, width: 170); // ---------- $page2 = $pdf->addPage(); $html2 = <<

HTML TABLES (B)

No. XXXX XXXX XXXX XXXX XXXX
1. BRDD
XXXX
XXXX
XXXX
XXXX
XXXX
XXXX
XXXX
XXXX
XXXX
XXXX
XXXX
XXXX XXXX
XXXX
2. XXXX
XXXX
XXXX
XXXX
XXXX
XXXX
XXXX
XXXX
XXXX
XXXX
XXXX
XXXX
XXXX
XXXX
XXXX
XXXX
XXXX
XXXX
XXXX
XXXX
XXXX
XXXX
XXXX
XXXX
XXXX
XXXX
XXXX
XXXX
3. XXXX
XXXX
XXXX
XXXX
XXXX
XXXX
4. XXXX
XXXX
XXXX
XXXX
XXXX
XXXX
XXXX
XXXX
EOF; $pdf->addHTMLCell(html: $html2, posx: 20, posy: 10, width: 180); // ---------- $page3 = $pdf->addPage(); $html3 = <<HTML TABLES (C)
COL 1 - ROW 1
COLSPAN 3
COL 2 - ROW 1 COL 3 - ROW 1
COL 2 - ROW 2 - COLSPAN 2
text line
text line
text line
text line
COL 3 - ROW 2
COL 3 - ROW 3

COL 1 - ROW 1
COLSPAN 3
text line
text line
text line
text line
text line
text line
COL 2 - ROW 1 COL 3 - ROW 1
COL 2 - ROW 2 - COLSPAN 2
text line
text line
text line
text line
COL 3 - ROW 2
COL 3 - ROW 3

COL 1 - ROW 1
COLSPAN 3
text line
text line
text line
text line
text line
text line
COL 2 - ROW 1 COL 3 - ROW 1
COL 2 - ROW 2 - COLSPAN 2
text line
text line
text line
text line
COL 3 - ROW 2
text line
text line
COL 3 - ROW 3

Left column Heading Column Span 5 Heading Column Span 9
Rowspan 2
This is some text that fills the table cell.
span 2 span 2 2 rows Colspan 8
1a 2a 1b 2b 1 2 3 4 5 6 7 8
EOD; $pdf->addHTMLCell(html: $html3, posx: 20, posy: 10, width: 180); // ---------- $page4 = $pdf->addPage(); $html4 = '

HTML TABLES (D)

1L Alfa Bravo Charlie Delta Echo Foxtrot Golf Hotel India Juliett Kilo Lima Mike November Oscar Papa Quebec Romeo Sierra Tango Uniform Victor Whiskey Xray Yankee Zulu
1C Alfa Bravo Charlie Delta Echo Foxtrot Golf Hotel India Juliett Kilo Lima Mike November Oscar Papa Quebec Romeo Sierra Tango Uniform Victor Whiskey Xray Yankee Zulu
1R Alfa Bravo Charlie Delta Echo Foxtrot Golf Hotel India Juliett Kilo Lima Mike November Oscar Papa Quebec Romeo Sierra Tango Uniform Victor Whiskey Xray Yankee Zulu
2L A1 example link column span. Alfa Bravo Charlie Delta Echo Foxtrot Golf Hotel India Juliett Kilo Lima Mike November Oscar Papa Quebec Romeo Sierra Tango Uniform Victor Whiskey Xray Yankee Zulu.
2C A1 example link column span. Alfa Bravo Charlie Delta Echo Foxtrot Golf Hotel India Juliett Kilo Lima Mike November Oscar Papa Quebec Romeo Sierra Tango Uniform Victor Whiskey Xray Yankee Zulu.
2R A1 example link column span. Alfa Bravo Charlie Delta Echo Foxtrot Golf Hotel India Juliett Kilo Lima Mike November Oscar Papa Quebec Romeo Sierra Tango Uniform Victor Whiskey Xray Yankee Zulu.
3L small text Alfa Bravo Charlie Delta Echo Foxtrot Golf Hotel India Juliett Kilo Lima Mike November Oscar Papa Quebec Romeo Sierra Tango Uniform Victor Whiskey Xray Yankee Zulu
3C small text Alfa Bravo Charlie Delta Echo Foxtrot Golf Hotel India Juliett Kilo Lima Mike November Oscar Papa Quebec Romeo Sierra Tango Uniform Victor Whiskey Xray Yankee Zulu
3R small text Alfa Bravo Charlie Delta Echo Foxtrot Golf Hotel India Juliett Kilo Lima Mike November Oscar Papa Quebec Romeo Sierra Tango Uniform Victor Whiskey Xray Yankee Zulu

TCPDF logo
TCPDF logo
TCPDF logo
'; $pdf->addHTMLCell(html: $html4, posx: 20, posy: 10, width: 180); // ---------- $html5A = '
TEST
'; $pdf->addHTMLCell(html: $html5A, posx: 20, posy: 160, width: 180); $html5B = '
TEST
'; $pdf->addHTMLCell(html: $html5B, posx: 20, posy: 160, width: 180); // ---------- $pdf->page->addContent($pdf->graph->getLine(0, 190, 210, 190, ['lineWidth' => 6, 'lineColor' => 'rgb(255, 0, 0)'])); $pdf->addHTMLCell( '
', 0, 195, ); // ---------- $page5 = $pdf->addPage(); // Percentage widths are resolved against the containing element: // an image with width:100% fills its fixed-width table cell (issue #247). $html6 = '

HTML TABLES (E): PERCENTAGE WIDTHS

25% column 75% column
TCPDF logo TCPDF logo

TCPDF logo The image on the left fills the width of its 40mm cell (width:100%).

The horizontal rule above spans 50% of the container width.

'; $pdf->addHTMLCell(html: $html6, posx: 20, posy: 10, width: 180); // ============================================================= // ---------- // get PDF document as raw string $rawpdf = $pdf->getOutPDFString(); // ---------- // Various output modes: //$pdf->savePDF(\dirname(__DIR__).'/target', $rawpdf); $pdf->renderPDF(rawpdf: $rawpdf); //$pdf->downloadPDF($rawpdf); //echo $pdf->getMIMEAttachmentPDF($rawpdf);