Campi Form
Il modulo Form (FormFieldManager, FormField) crea campi AcroForm interattivi dentro il PDF. I tipi campo sono definiti dall'enum FormFieldType: TEXT, PASSWORD, TEXTAREA, CHECKBOX, RADIO, LISTBOX, COMBOBOX, BUTTON. Tutti i metodi restituiscono static, quindi ogni chiamata può essere concatenata.
Riferimento Rapido
| Metodo | Tipo Campo |
|---|---|
textField() | Input testo riga singola |
checkboxField() | Toggle checkbox |
radioField() | Radio button (raggruppato) |
listboxField() | Lista scrollabile |
comboboxField() | Selettore dropdown |
buttonField() | Pulsante push con azione JavaScript opzionale |
flattenFields() | Converti tutti i campi interattivi in contenuto statico |
Esempio Base
use Yeeefang\TcpdfNext\Core\Document;
$pdf = Document::create()
->addPage()
->setFont('Helvetica', '', 12)
->cell(0, 10, 'Registration Form', newLine: true)
->ln(5)
// Campo testo
->cell(30, 8, 'Name:')
->textField('name', 45, null, 80, 8, [
'border' => ['width' => 1],
'maxLength' => 100,
])
->ln(12)
// Campo email
->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)
// Pulsante submit
->buttonField('submit', 45, null, 40, 10, 'Submit', 'submitForm("https://example.com/submit")');Campi Testo e Checkbox
$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);Passa null per $y per usare la posizione cursore verticale corrente.
Campi Radio Button
$pdf->radioField(string $name, float $x, float $y, float $w, float $h, array $prop = []);Radio button con lo stesso $name formano un gruppo mutualmente esclusivo:
$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');Campi List, Combo Box e Button
$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() renderizza una lista multi-riga scrollabile. comboboxField() renderizza un dropdown riga singola. buttonField() crea un pulsante push con stringa azione JavaScript opzionale.
$pdf->buttonField('reset', 15, null, 40, 10, 'Reset', 'this.resetForm()')
->buttonField('print', 60, null, 40, 10, 'Print', 'this.print()');Proprietà Campo
L'array $prop controlla aspetto e comportamento campo:
| Chiave | Tipo | Descrizione |
|---|---|---|
border | array | Stile bordo con chiavi width, color, style |
bgcolor | array | Colore sfondo come [r, g, b] |
font | string | Nome famiglia font |
fontSize | float | Dimensione font in punti |
alignment | string | Allineamento testo: left, center, right |
maxLength | int | Conteggio caratteri massimo (campi testo) |
readonly | bool | Previeni modifica utente |
required | bool | Marca come richiesto per validazione form |
value | string | Valore predefinito / iniziale |
Appiattimento Form
L'appiattimento converte tutti i campi interattivi in contenuto statico, non editabile. Questo è utile per archiviare form completati o produrre un PDF finale read-only.
$pdf->flattenFields(); // Converti tutti i campi form in contenuto staticoDopo l'appiattimento, i valori campo diventano testo permanente. I campi non possono più essere modificati in un visualizzatore PDF.