Skip to content

Form Filler

FormFiller reads and fills AcroForm fields using incremental updates, preserving the original PDF structure and any existing signatures.

use Phpdftk\Pdf\Toolkit\FormFiller;
// From file
$filler = FormFiller::open('form.pdf');
// From string
$filler = FormFiller::openString($pdfBytes);
// Encrypted PDF
$filler = FormFiller::open('secured.pdf', password: 'secret');
$names = $filler->getFieldNames();
// => ['name', 'email', 'subscribe', 'country']
$info = $filler->getFieldInfo('email');
$info->name; // 'email'
$info->type; // FieldType::Text
$info->value; // current value or null
$info->flags; // int (PDF /Ff flags)
$info->maxLen; // ?int (max length for text fields)
$info->options; // ?string[] (options for choice fields)
$info->rect; // ?float[] ([x1, y1, x2, y2] widget rectangle)
$values = $filler->getFieldValues();
// => ['name' => 'Jane', 'email' => null, 'subscribe' => 'Off', ...]
if ($filler->hasField('signature')) {
// field exists
}
$filler->fill('name', 'Jane Doe')
->fill('email', 'jane@example.com');
$filler->fillMany([
'name' => 'Jane Doe',
'email' => 'jane@example.com',
'city' => 'Toronto',
]);
$filler->check('subscribe'); // check
$filler->check('subscribe', false); // uncheck
$filler->select('country', 'Canada');
// To file
$filler->save('filled.pdf');
// To string
$bytes = $filler->toBytes();

The FieldType enum covers the four PDF interactive field types:

Enum casePDF valueCovers
FieldType::TextTxText input fields
FieldType::ButtonBtnCheckboxes, radio buttons, push buttons
FieldType::ChoiceChDropdowns, list boxes
FieldType::SignatureSigSignature fields
$filler->getPageCount(); // int
$reader = $filler->getReader(); // PdfReader