Fill Dynamic XFA
This demo shows you how to fill a dynamic XFA form.
A dynamic XFA form only uses the PDF format as a container while the whole content is rendered at runtime.
You need to use a PDF viewer that support XFA for both the template and the result.
PHP
<?php
use setasign\SetaPDF2\Core\Document;
use setasign\SetaPDF2\Core\Writer\HttpWriter;
use setasign\SetaPDF2\FormFiller\FormFiller;
// load and register the autoload function
require_once __DIR__ . '/../../../../../bootstrap.php';
// get the document instance
$document = Document::loadByFilename(
$assetsDirectory . '/pdfs/forms/xfa/Badge.pdf',
new HttpWriter('dynamic-xfa-form.pdf')
);
// now get an instance of the form filler
$formFiller = new FormFiller($document);
// generate some dummy data:
$firstNames = ['Peter', 'Carl', 'Dan', 'Stan', 'Roger', 'Martin', 'Paul', 'Rick', 'Chris', 'Burton'];
$lastNames = ['Walker', 'Bent', 'Stuckle', 'Willow', 'Williams', 'Müller', 'Meyer', 'Schulze', 'Cell'];
$companyNames = ['tektown Ltd.', 'camtown Ltd.', 'lenstown Ltd.', 'etown Ltd.'];
$xml = '<badges>';
for ($i = 100; $i > 0; $i--) {
$name = htmlspecialchars($firstNames[array_rand($firstNames)] . ' ' . $lastNames[array_rand($lastNames)], ENT_XML1);
$companyName = htmlspecialchars($companyNames[array_rand($companyNames)], ENT_XML1);
$id = mt_rand(1000000, 9999999);
$xml .= <<<XML
<badge>
<name>$name</name>
<company>$companyName</company>
<barcode>$id</barcode>
</badge>
XML;
}
$xml .= '</badges>';
// get the XFA helper
$xfa = $formFiller->getXfa();
if ($xfa === false) {
echo "No XFA data found.";
}
// pass the XML data
$xfa->setData($xml);
// save and finish
$document->save()->finish();
