Example E097 : html wrapped links

PDF

Page 1 of E097_html_wrapped_links.pdf

Source Code

<?php

declare(strict_types=1);

/**
 * E097_html_wrapped_links.php
 *
 * Visual inspection sheet for HTML links that wrap over several lines: a link made
 * of one text run, justified and centered links, a link that starts and ends
 * mid-line, a link with inline markup and a right-to-left link. Every line of each
 * link has its own link annotation, so every line is clickable. Each case states
 * the expected result.
 *
 * @category    Library
 * @package     Pdf
 * @author      Nicola Asuni <info@tecnick.com>
 * @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'));

$pdf = new \Com\Tecnick\Pdf\Tcpdf();

$pdf->setCreator('tc-lib-pdf');
$pdf->setAuthor('Nicola Asuni');
$pdf->setSubject('tc-lib-pdf example: 097');
$pdf->setTitle('HTML Wrapped Links');
$pdf->setKeywords('TCPDF tc-lib-pdf example html link annotation wrap');
$pdf->setPDFFilename('097_html_wrapped_links.pdf');

$selectFont = function (string $family = 'dejavusans', string $style = '', float $size = 10) use ($pdf): void {
    $font = $pdf->font->insert($pdf->pon, $family, $style, $size);
    $pdf->page->addContent($font['out']);
};

/**
 * Writes a note in absolute page coordinates.
 */
$note = function (string $txt, float $posx, float $posy, float $width, string $style = '', float $size = 8) use (
    $pdf,
    $selectFont,
): void {
    $selectFont('helvetica', $style, $size);
    $pdf->page->addContent($pdf->getTextCell(
        txt: $txt,
        posx: $posx,
        posy: $posy,
        width: $width,
        valign: \Com\Tecnick\Pdf\TextVAlign::Top,
        halign: \Com\Tecnick\Pdf\TextHAlign::Left,
    ));
};

$frameStyle = ['all' => ['lineColor' => '#999999', 'lineWidth' => 0.2]];

$pdf->addPage();
$note('HTML links over several lines', 15, 12, 180, 'B', 12);
$note(
    'Every line of each link has its own link annotation: move the pointer over each line,'
    . ' or click it, and the link target is offered. The gray frame is the HTML cell.',
    15,
    19,
    180,
);

$linkText = 'this link text is long enough to wrap over three lines of the cell';
$cases = [
    'One text run' => '<p><a href="https://tcpdf.org/one-run">' . $linkText . '</a></p>',
    'Justified' => '<p style="text-align:justify"><a href="https://tcpdf.org/justified">' . $linkText . '</a></p>',
    'Centered' => '<p style="text-align:center"><a href="https://tcpdf.org/centered">' . $linkText . '</a></p>',
    'Starts and ends mid-line' =>
        '<p>Text before the link, <a href="https://tcpdf.org/mid-line">' . $linkText . '</a>, text after the link.</p>',
    'Inline markup' =>
        '<p><a href="https://tcpdf.org/markup">this <b>link</b> has <i>inline markup</i> and'
            . ' wraps over <span style="color:#cc0000">several lines</span> of the cell</a></p>',
    'Right to left' =>
        '<p dir="rtl"><a href="https://tcpdf.org/rtl">'
            . 'זהו קישור ארוך מספיק כדי לעבור לשלוש שורות בתא הזה של הדוגמה'
            . '</a></p>',
];

$col = 0;
$posy = 32.0;
foreach ($cases as $title => $html) {
    $posx = $col === 0 ? 15.0 : 110.0;
    $note($title, $posx, $posy, 85, 'B', 9);
    $pdf->page->addContent($pdf->graph->getRect($posx, $posy + 6, 45, 28, 'S', $frameStyle));
    $selectFont();
    $pdf->addHTMLCell($html, $posx, $posy + 6, 45);
    $note('Expected: every line of the link is clickable.', $posx, $posy + 34, 85);

    $col = 1 - $col;
    if ($col === 0) {
        $posy += 48;
    }
}

// ----------

$rawpdf = $pdf->getOutPDFString();
$pdf->renderPDF(rawpdf: $rawpdf);