Skip to content

Form Fields

The Form module (FormFieldManager, FormField) creates interactive AcroForm fields inside the PDF. Field types are defined by the FormFieldType enum: TEXT, PASSWORD, TEXTAREA, CHECKBOX, RADIO, LISTBOX, COMBOBOX, BUTTON. All methods return static, so every call can be chained.

Quick Reference

MethodField Type
textField()Single-line text input
checkboxField()Checkbox toggle
radioField()Radio button (grouped)
listboxField()Scrollable list
comboboxField()Dropdown selector
buttonField()Push button with optional JavaScript action
flattenFields()Convert all interactive fields to static content

Basic Example

php
use Yeeefang\TcpdfNext\Core\Document;

$pdf = Document::create()
    ->addPage()
    ->setFont('Helvetica', '', 12)
    ->cell(0, 10, 'Registration Form', newLine: true)
    ->ln(5)

    // Text field
    ->cell(30, 8, 'Name:')
    ->textField('name', 45, null, 80, 8, [
        'border'    => ['width' => 1],
        'maxLength' => 100,
    ])
    ->ln(12)

    // Email field
    ->cell(30, 8, 'Email:')
    ->textField('email', 45, null, 80, 8)
    ->ln(12)

    // Checkbox
    ->checkboxField('agree', 15, null, 5, 5)
    ->cell(0, 5, '  I agree to the terms', newLine: true)
    ->ln(10)

    // Dropdown
    ->cell(30, 8, 'Country:')
    ->comboboxField('country', 45, null, 60, 8, ['US', 'UK', 'TW', 'JP', 'DE'])
    ->ln(12)

    // Submit button
    ->buttonField('submit', 45, null, 40, 10, 'Submit', 'submitForm("https://example.com/submit")');

Text and Checkbox Fields

php
$pdf->textField(string $name, float $x, float $y, float $w, float $h, array $prop = []);
$pdf->checkboxField(string $name, float $x, float $y, float $w, float $h, bool $checked = false);

Pass null for $y to use the current vertical cursor position.

Radio Button Fields

php
$pdf->radioField(string $name, float $x, float $y, float $w, float $h, array $prop = []);

Radio buttons with the same $name form a mutually exclusive group:

php
$pdf->cell(30, 8, 'Gender:')
    ->radioField('gender', 45, null, 5, 5, ['value' => 'male'])
    ->cell(10, 5, ' M')
    ->radioField('gender', 65, null, 5, 5, ['value' => 'female'])
    ->cell(10, 5, ' F');

List, Combo Box, and Button Fields

php
$pdf->listboxField(string $name, float $x, float $y, float $w, float $h, array $values, array $prop = []);
$pdf->comboboxField(string $name, float $x, float $y, float $w, float $h, array $values, array $prop = []);
$pdf->buttonField(string $name, float $x, float $y, float $w, float $h, string $caption, string $action = '');

listboxField() renders a scrollable multi-row list. comboboxField() renders a single-row dropdown. buttonField() creates a push button with an optional JavaScript action string.

php
$pdf->buttonField('reset', 15, null, 40, 10, 'Reset', 'this.resetForm()')
    ->buttonField('print', 60, null, 40, 10, 'Print', 'this.print()');

Field Properties

The $prop array controls field appearance and behavior:

KeyTypeDescription
borderarrayBorder style with width, color, style keys
bgcolorarrayBackground color as [r, g, b]
fontstringFont family name
fontSizefloatFont size in points
alignmentstringText alignment: left, center, right
maxLengthintMaximum character count (text fields)
readonlyboolPrevent user editing
requiredboolMark as required for form validation
valuestringDefault / initial value

Form Flattening

Flattening converts all interactive fields into static, non-editable content. This is useful for archiving completed forms or producing a final read-only PDF.

php
$pdf->flattenFields();  // Convert all form fields to static content

After flattening, field values become permanent text. The fields can no longer be edited in a PDF viewer.

Released under the LGPL-3.0-or-later License.