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
| Method | Field 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
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
$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
$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:
$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
$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.
$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:
| Key | Type | Description |
|---|---|---|
border | array | Border style with width, color, style keys |
bgcolor | array | Background color as [r, g, b] |
font | string | Font family name |
fontSize | float | Font size in points |
alignment | string | Text alignment: left, center, right |
maxLength | int | Maximum character count (text fields) |
readonly | bool | Prevent user editing |
required | bool | Mark as required for form validation |
value | string | Default / 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.
$pdf->flattenFields(); // Convert all form fields to static contentAfter flattening, field values become permanent text. The fields can no longer be edited in a PDF viewer.