* @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 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: 031'); $pdf->setTitle('HTML Features Example'); $pdf->setKeywords('TCPDF tc-lib-pdf example HTML selectors forms table tags'); $pdf->setPDFFilename('031_html_features.pdf'); $pdf->setViewerPreferences(['DisplayDocTitle' => true]); $pdf->enableDefaultPageContent(); // ---------- // Insert fonts $bfont = $pdf->font->insert($pdf->pon, 'helvetica', '', 10); $page01 = $pdf->addPage(); $pdf->setBookmark( name: 'HTML feature showcase', link: '', level: 0, page: -1, posx: 0, posy: 0, fstyle: 'B', color: '', ); $pdf->page->addContent($bfont['out']); // NOTE: // The HTML parser expects input already cleaned to XHTML-like markup (e.g. via tidyHTML), // and attribute extraction is intentionally based on double-quoted values. // Unsupported interactive selectors intentionally remain no-op (for example :hover). // For table-structure tags, this example demonstrates current support for // caption/colgroup/col/tfoot; unsupported styling on colgroup/col remains // intentionally a no-op in this conservative implementation. $html = << .demo { font-size: 10pt; color: #222222; } .demo h2 { color: #003366; margin: 0 0 3mm 0; } .demo .selector-list li:first-child { color: #0b6e4f; font-weight: bold; } .demo .selector-list li:last-child { color: #8a1c7c; } .demo .selector-list li:nth-child(2n+1) { background-color: #f4f7ff; } .demo .selector-list li:nth-child(-n+2) { text-decoration: underline; } .demo .group > p + a.target { color: #0066cc; } .demo .group > p ~ a.target { font-weight: bold; } .demo .generated::before { content: "[PRE] "; color: #b03030; } .demo .generated::after { content: " [POST]"; font-weight: bold; } .demo p:empty { background-color: #fff2cc; border: 0.2mm solid #c7a600; height: 4mm; } .demo a:link { text-decoration: underline; } .demo table { border: 0.2mm solid #666666; } .demo th { background-color: #dddddd; } .demo input, .demo select, .demo textarea { border: 0.2mm solid #777777; } .note { font-size: 8pt; color: #555555; }

HTML Feature Showcase

This example demonstrates currently implemented selector, table, and form behavior in the HTML engine.

1) Pseudo-class selectors

2) Combinators (+ and ~)

Adjacent sibling trigger paragraph

Adjacent + general sibling target link

3) Empty pseudo-class

4) Pseudo-elements (::before / ::after, text-only content)

Generated marker sample

5) Stable table rendering with selectors applied

Key Value
first-child Supported
last-child Supported
nth-child Supports n, odd/even, and an+b (for example 2n+1, -n+2)
empty Supported
link Supported for anchors with href

6) Table structure tags (caption, colgroup, col, tfoot)

Quarterly feature status
Area State Notes
caption Rendered Displayed as a block-level table title.
colgroup/col width hints Applied Used as precomputed column width hints before first-row fallback.
tfoot Parsed Footer row contributes to table row/column bookkeeping.

7) Form semantics (labels, field flags, select precedence)


Visible fallback text: Handled by support queue

Unsupported selector examples intentionally remain unmatched: :hover, :focus, :active, and :visited.

Pseudo-elements are intentionally limited to text-only `content:"..."` behavior in this conservative implementation.

Unsupported styling on structural tags like colgroup/col is intentionally ignored in this conservative HTML engine pass.

This form section is focused on parser/field-mapping behavior; it is not intended to replicate browser form UX.

HTML; $pdf->addHTMLCell(html: $html, posx: 15, posy: 20, width: 180); // ---------- // HTML B $pageV02 = $pdf->addPage(); $rect = $pdf->graph->getRect(160, 36, 30, 50, 'DF'); $pdf->page->addContent($rect); $textcolors = '

Text

'; $bgcolors = '

Background

'; $csscolortypes = '

CSS Color Types

' . '' . '' . '' . '' . '' . '' . '' . '' . '' . '' . '' . '' . '' . '
TypeExample
namedbackground-color: navy
graybackground-color: g(50%)
hex shortbackground-color: #0f0
hex longbackground-color: #1e90ff
rgbbackground-color: rgb(255, 99, 71)
rgbabackground-color: rgba(30, 144, 255, 0.25)
hslbackground-color: hsl(120, 100%, 25%)
hslabackground-color: hsla(300, 100%, 50%, 0.20)
labbackground-color: lab(54.29% -19.04 38.25)
lab alphabackground-color: lab(54.29% -19.04 38.25 / 0.35)
cmykbackground-color: cmyk(67, 33, 0, 25)
'; foreach ($pdf->color::WEBHEX as $k => $v) { $textcolors .= '' . $k . ' '; $bgcolors .= '' . $k . ' '; } $html_02 = '

HTML Colors

' . $csscolortypes . '
' . $textcolors . '
' . $bgcolors; $pdf->addHTMLCell(html: $html_02, posx: 20, posy: 10, width: 180); // ---------- // HTML D - Transparency Showcase $pageV02T = $pdf->addPage(); $pdf->page->addContent($bfont['out']); $pdf->setBookmark( name: 'Transparency color showcase', link: '', level: 0, page: -1, posx: 0, posy: 0, fstyle: 'B', color: '', ); $baseRectStyle = [ 'lineWidth' => 0, 'lineCap' => 'butt', 'lineJoin' => 'miter', 'miterLimit' => 10, 'dashArray' => [], 'dashPhase' => 0, 'lineColor' => 'none', 'fillColor' => 'rgb(0, 64, 128)', ]; $pdf->page->addContent($pdf->graph->getRect(20, 24, 170, 96, 'F', ['all' => $baseRectStyle])); $html_transparency = << .alpha-demo { font-size: 10pt; color: #ffffff; } .alpha-demo h2 { margin: 0 0 2mm 0; font-size: 16pt; color: rgba(255, 255, 255, 0.62); } .alpha-demo .line-a { font-size: 12pt; color: rgba(255, 255, 255, 0.48); margin: 0 0 2mm 0; } .alpha-demo .line-b { font-size: 12pt; color: hsla(60, 100%, 86%, 0.58); margin: 0 0 3mm 0; } .alpha-demo .bg-strip { margin-top: 1mm; padding: 1.3mm 1.8mm; background-color: rgba(0, 0, 0, 0.30); color: rgba(255, 255, 255, 0.9); } .alpha-demo .swatch { display: inline-block; margin-right: 2mm; padding: 0.7mm 1.6mm; color: rgba(0, 0, 0, 0.86); font-weight: bold; } .alpha-demo .swatch-rgba { background-color: rgba(30, 144, 255, 0.32); } .alpha-demo .swatch-hsla { background-color: hsla(300, 100%, 60%, 0.30); } .alpha-demo .swatch-rgba2 { background-color: rgba(255, 255, 255, 0.45); }

Transparency Color Showcase

RGBA text: white alpha at 0.48 over a solid blue rectangle

HSLA text: warm yellow alpha at 0.58 over the same blue base

Background object with alpha: rgba(0, 0, 0, 0.30)

RGBA bg 0.32 HSLA bg 0.30 RGBA bg 0.45

HTML; $pdf->addHTMLCell(html: $html_transparency, posx: 24, posy: 28, width: 162); // ---------- // HTML C $pageV03 = $pdf->addPage(); $html_03 = '

Various HTML Tests

thisisaverylongword thisisanotherverylongword thisisaverylongword thisisanotherverylongword thisisaverylongword thisisaverylongword thisisanotherverylongword thisisaverylongword thisisanotherverylongword thisisaverylongword thisisaverylongword thisisanotherverylongword thisisaverylongword thisisanotherverylongword thisisaverylongword thisisaverylongword thisisanotherverylongword thisisaverylongword thisisanotherverylongword thisisaverylongword thisisaverylongword thisisanotherverylongword thisisaverylongword thisisanotherverylongword thisisaverylongword'; // Test fonts nesting $htmlt1 = 'Default Courier Helvetica Times dejavusans Times Helvetica Courier Default'; $htmlt2 = 'small text normal small text normal subscript normal superscript normal'; $htmlt3 = 'The quick brown fox jumps over the lazy dog.'; $html_03 .= '
' . $htmlt1 . '
' . $htmlt2 . '
' . $htmlt3 . '
' . $htmlt3 . '
' . $htmlt2 . '
'; $html_03 .= '
#1a
'; $html_03 .= <<

Div Blocks

Hello World!
Hello
    int main() {
        printf("HelloWorld");
        return 0;
    }
    
Monospace font, normal font, monospace font, normal font.
DIV LEVEL 1
DIV LEVEL 2
DIV LEVEL 1

SPAN LEVEL 1 SPAN LEVEL 2 SPAN LEVEL 1 EOF; $pdf->addHTMLCell(html: $html_03, posx: 20, posy: 10, width: 180); // ---------- // HTML E-A $pageV05A = $pdf->addPage(); $html_05A = '

HTML Text Alignment (A)

Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?

A + B = C    ->    C - B = A    ->    C - A = B ->    A + B = C    ->    C - B = A    ->    C - A = B ->    A + B = C    ->    C - B = A    ->    C - A = B ->    A + B = C    ->    C - B = A    ->    C - A = B    ->    A + B = C    ->    C - B = A    ->    C - A = B ->    A + B = C    ->    C - B = A    ->    C - A = B ->    A + B = C    ->    C - B = A    ->    C - A = B ->    A + B = C    ->    C - B = A    ->    C - A = B

BoldItalicUnderlined BoldItalicUnderlined BoldItalicUnderlined BoldItalicUnderlined BoldItalicUnderlined BoldItalicUnderlined BoldItalicUnderlined BoldItalicUnderlined BoldItalicUnderlined BoldItalicUnderlined BoldItalicUnderlined BoldItalicUnderlined BoldItalicUnderlined'; $pdf->addHTMLCell(html: $html_05A, posx: 20, posy: 10, width: 180); // ---------- // HTML E-L $html_05L = '

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. 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. 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.

'; $pdf->addHTMLCell(html: $html_05L, posx: 20, posy: 100, width: 180); // ---------- // HTML E-B $pageV05B = $pdf->addPage(); $html_05B = '

HTML Text Alignment (B)

JUSTIFY: 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 Alfa Bravo Charlie Delta Echo Foxtrot Golf Hotel India Juliett Kilo Lima Mike November Oscar Papa Quebec Romeo

LEFT: 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

CENTER: 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

RIGHT: 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

JUSTIFY: 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

JUSTIFY: Alfa Bravo Charlie Delta Echo TCPDF logoTCPDF box Foxtrot Golf Hotel India Juliett Kilo Lima Mike November Oscar Papa Quebec Romeo Sierra Tango Uniform Victor Whiskey Xray Yankee Zulu

LEFT: Alfa Bravo Charlie Delta Echo TCPDF logoTCPDF box Foxtrot Golf Hotel India Juliett Kilo Lima Mike November Oscar Papa Quebec Romeo Sierra Tango Uniform Victor Whiskey Xray Yankee Zulu

CENTER: Alfa Bravo Charlie Delta Echo TCPDF logoTCPDF box Foxtrot Golf Hotel India Juliett Kilo Lima Mike November Oscar Papa Quebec Romeo Sierra Tango Uniform Victor Whiskey Xray Yankee Zulu

RIGHT: Alfa Bravo Charlie Delta Echo TCPDF logoTCPDF box Foxtrot Golf Hotel India Juliett Kilo Lima Mike November Oscar Papa Quebec Romeo Sierra Tango Uniform Victor Whiskey Xray Yankee Zulu


a abc abcdefghijkl (abcdef) abcdefg abcdefghi a ((abc)) abcd TCPDF logo abcdef abcdefg abcdefghi a abc abcd abcdef abcdefg abcdefghi a abc abcd abcdef abcdefg abcdefghi a abc abcd abcdef abcdefg abcdefghi a abc \(abcd\) abcdef abcdefg abcdefghi a abc \\\(abcd\\\) abcdef abcdefg abcdefghi a abc abcd abcdef abcdefg abcdefghi a abc abcd abcdef abcdefg abcdefghi a abc abcd abcdef abcdefg abcdefghi a abc abcd abcdef abcdefg start a abc before yellow color after a abc abcd abcdef abcdefg abcdefghi a abc abcd end abcdefg abcdefghi a abc abcd abcdef abcdefg abcdefghi a abc abcd abcdef abcdefg abcdefghi a abc abcd abcdef abcdefg abcdefghi a abc abcd abcdef abcdefg abcdefghi a abc abcd abcdef abcdefg abcdefghi a abc abcd abcdef abcdefg abcdefghi a abc abcd abcdef abcdefg abcdefghi
abcd abcdef abcdefg abcdefghi
abcd abcde abcdef

'; $pdf->addHTMLCell(html: $html_05B, posx: 20, posy: 10, width: 180); // ---------- // HTML F $pageV06 = $pdf->addPage(); $html_06 = <<

Example of XHTML + CSS

Example of paragraph with class selector. Lorem ipsum dolor sit amet, consectetur adipiscing elit. In sed imperdiet lectus. Phasellus quis velit velit, non condimentum quam. Sed neque urna, ultrices ac volutpat vel, laoreet vitae augue. Sed vel velit erat. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Cras eget velit nulla, eu sagittis elit. Nunc ac arcu est, in lobortis tellus. Praesent condimentum rhoncus sodales. In hac habitasse platea dictumst. Proin porta eros pharetra enim tincidunt dignissim nec vel dolor. Cras sapien elit, ornare ac dignissim eu, ultricies ac eros. Maecenas augue magna, ultrices a congue in, mollis eu nulla. Nunc venenatis massa at est eleifend faucibus. Vivamus sed risus lectus, nec interdum nunc.

Example of paragraph with ID selector. Fusce et felis vitae diam lobortis sollicitudin. Aenean tincidunt accumsan nisi, id vehicula quam laoreet elementum. Phasellus egestas interdum erat, et viverra ipsum ultricies ac. Praesent sagittis augue at augue volutpat eleifend. Cras nec orci neque. Mauris bibendum posuere blandit. Donec feugiat mollis dui sit amet pellentesque. Sed a enim justo. Donec tincidunt, nisl eget elementum aliquam, odio ipsum ultrices quam, eu porttitor ligula urna at lorem. Donec varius, eros et convallis laoreet, ligula tellus consequat felis, ut ornare metus tellus sodales velit. Duis sed diam ante. Ut rutrum malesuada massa, vitae consectetur ipsum rhoncus sed. Suspendisse potenti. Pellentesque a congue massa.

example of DIV with border and fill.
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
text-transform LOWERCASE Lorem ipsum dolor sit amet, consectetur adipiscing elit.
text-transform uppercase Lorem ipsum dolor sit amet, consectetur adipiscing elit.
text-transform cAPITALIZE Lorem ipsum dolor sit amet, consectetur adipiscing elit.
EOF; $pdf->addHTMLCell(html: $html_06, posx: 20, posy: 10, width: 180); // ---------- // HTML A $pageV01 = $pdf->addPage(); $bfont6 = $pdf->font->insert($pdf->pon, 'dejavusans', '', 10); $pdf->page->addContent($bfont6['out']); $style_cell = [ 'all' => [ 'lineWidth' => 1, 'lineCap' => 'round', 'lineJoin' => 'round', 'miterLimit' => 1, 'dashArray' => [], 'dashPhase' => 0, 'lineColor' => 'green', 'fillColor' => '#fdfca5', ], ]; $html_01 = '

HTML Example A

Some special characters: < € € € & è è © > \\slash \\\\double-slash \\\\\\triple-slash

List

List example:
  1. bold text
  2. italic text
  3. underlined text
  4. bbibiubib
  5. link to https://tcpdf.org
  6. Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo.
    Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt.
  7. SUBLIST
    1. row one
      • sublist
    2. row two
  8. TEST line through
  9. font + 3
  10. small text normal small text normal subscript normal superscript normal
Coffee
Black hot drink
Milk
White cold drink
The words “מזל [mazel] טוב [tov]” mean “Congratulations!”

This is just an example of html code to demonstrate some supported CSS inline styles. bold text line-trough underline and line-trough color background color bold xx-small x-small small medium large x-large xx-large

'; $pdf->addHTMLCell(html: $html_01, posx: 20, posy: 10, width: 150, height: 0, cell: null, styles: $style_cell); // ---------- $pageVC = $pdf->addPage(); $pdf->page->addContent($bfont6['out']); $html_vc1 = << .vc-box { width: 150mm; height: 90mm; border: 0.6mm solid #003366; background-color: #e9f2ff; } .vc-fill { width: 100%; height: 100%; } .vc-content { vertical-align: middle; text-align: center; color: #002244; }

Vertically Centered Block

This content is centered in the middle of the box.

HTML: headings, paragraphs, and inline formatting.

HTML; $pdf->addHTMLCell(html: $html_vc1, posx: 20, posy: 20); // ---------- $html_vc2 = <<

Vertically Centered Block

This content is centered in the middle of the box.

HTML: headings, paragraphs, and inline formatting.

HTML; $pdf->addHTMLCell(html: $html_vc2, posx: 20, posy: 120, width: 150, height: 100); // ---------- // 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);