Skip to content

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

MetodoTipo 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

php
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

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

Passa null per $y per usare la posizione cursore verticale corrente.

Campi Radio Button

php
$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:

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

Campi List, Combo Box e Button

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() renderizza una lista multi-riga scrollabile. comboboxField() renderizza un dropdown riga singola. buttonField() crea un pulsante push con stringa azione JavaScript opzionale.

php
$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:

ChiaveTipoDescrizione
borderarrayStile bordo con chiavi width, color, style
bgcolorarrayColore sfondo come [r, g, b]
fontstringNome famiglia font
fontSizefloatDimensione font in punti
alignmentstringAllineamento testo: left, center, right
maxLengthintConteggio caratteri massimo (campi testo)
readonlyboolPrevieni modifica utente
requiredboolMarca come richiesto per validazione form
valuestringValore 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.

php
$pdf->flattenFields();  // Converti tutti i campi form in contenuto statico

Dopo l'appiattimento, i valori campo diventano testo permanente. I campi non possono più essere modificati in un visualizzatore PDF.

Rilasciato sotto licenza LGPL-3.0-or-later.