Example E042 : html form

Form controls written as HTML markup, with labels, field flags, and select precedence, mapped to AcroForm widgets.

Form controls written as HTML markup, with labels, field flags, and select precedence, mapped to AcroForm widgets.

PDF

Page 1 of E042_html_form.pdf

Source Code

<?php

declare(strict_types=1);

/**
 * E042_html_form.php
 *
 * @since       2017-05-08
 * @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('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: 042');
$pdf->setTitle('HTML Form Elements');
$pdf->setKeywords('TCPDF tc-lib-pdf html forms input select textarea rendering');
$pdf->setPDFFilename('042_html_form.pdf');

$pdf->setViewerPreferences(['DisplayDocTitle' => true]);

$pdf->enableDefaultPageContent();

// ----------
// Insert fonts

$bfont1 = $pdf->font->insert($pdf->pon, 'helvetica', '', 12);

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

// HTML G

$pageV07 = $pdf->addPage();

$html_07 = <<<EOD
    <h1>XHTML Form Example</h1>
    <form method="post" action="http://localhost/printvars.php" enctype="multipart/form-data">
    <label for="name">name:</label> <input type="text" name="name" value="" size="20" maxlength="30" /><br />
    <label for="password">password:</label> <input type="password" name="password" value="" size="20" maxlength="30" /><br /><br />
    <label for="infile">file:</label> <input type="file" name="userfile" size="20" /><br /><br />
    <input type="checkbox" name="agree" value="1" checked="checked" /> <label for="agree">I agree </label><br /><br />
    <input type="radio" name="radioquestion" id="rqa" value="1" /> <label for="rqa">one</label><br />
    <input type="radio" name="radioquestion" id="rqb" value="2" checked="checked"/> <label for="rqb">two</label><br />
    <input type="radio" name="radioquestion" id="rqc" value="3" /> <label for="rqc">three</label><br /><br />
    <label for="selection">select:</label>
    <select name="selection" size="0">
    	<option value="0">zero</option>
    	<option value="1">one</option>
    	<option value="2">two</option>
    	<option value="3">three</option>
    </select><br /><br />
    <label for="selection">select:</label>
    <select name="multiselection" size="2" multiple="multiple">
    	<option value="0">zero</option>
    	<option value="1">one</option>
    	<option value="2">two</option>
    	<option value="3">three</option>
    </select><br /><br /><br />
    <label for="text">text area:</label><br />
    <textarea cols="40" rows="3" name="text">line one
    line two</textarea><br />
    <br /><br /><br />
    <input type="reset" name="reset" value="Reset" />
    <input type="submit" name="submit" value="Submit" />
    <input type="button" name="print" value="Print" onclick="print()" />
    <input type="hidden" name="hiddenfield" value="OK" />
    <br />
    </form>
    EOD;

$pdf->addHTMLCell(html: $html_07, posx: 30, posy: 20);

// =============================================================

// ----------
// 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);