Data Structures
The php-pdftk library utilizes several specialized helper classes to parse output from pdftk and to generate the complex input files (like XFDF or Info dumps) that pdftk requires.
Output Parsers (ArrayObjects)
When you extract data from a PDF using getData() or getDataFields(), the library does not return a raw string. Instead, it parses the custom text format produced by pdftk into PHP ArrayObject instances.
Because they extend ArrayObject, you can iterate over them with foreach, access specific keys using array syntax, or cast them to raw strings to see the original pdftk output.
DataFields
Returned by Pdf::getDataFields(). It parses the dump_data_fields output block, separating properties for every form field in the document.
$fields = $pdf->getDataFields();
foreach ((array)$fields as $field) {
echo "Field Name: " . $field['FieldName'] . "\n";
echo "Field Type: " . $field['FieldType'] . "\n"; // e.g., Text, Button
}
InfoFields
Returned by Pdf::getData(). Parses the dump_data output into a grouped, nested array structure. Keys like Info, Bookmark, and PageMedia are grouped logically.
$data = $pdf->getData();
$array = (array) $data;
// Accessing document metadata
echo $array['Info']['Title'];
echo $array['Info']['CreationDate'];
// Accessing bookmarks
if (isset($array['Bookmark'])) {
$firstBookmark = $array['Bookmark'][0];
echo $firstBookmark['Title'] . " -> Page " . $firstBookmark['PageNumber'];
}
Temporary File Generators
These classes extend mikehaertl\tmp\File. They are instantiated automatically by the Pdf class when you pass arrays into methods like fillForm() or updateInfo(), but they can also be used manually if you need to generate data files without executing pdftk.
InfoFile
Used internally by updateInfo(). It takes a nested PHP array (matching the structure of InfoFields) and converts it into the highly specific, block-based text format (InfoBegin... InfoKey... InfoValue) expected by pdftk.
FdfFile & XfdfFile
Used internally by fillForm(). They generate Adobe Forms Data Format files.
XfdfFile is highly recommended and is the default. It generates an XML-based XFDF structure. The key advantage of the XfdfFile class in this library is its parsing engine:
- UTF-8 Safe: It natively encodes data to ensure special characters survive the injection process.
- Dot-Notation Nesting: If you pass keys with dots (e.g.,
'Address.Street' => '123 Main'), it automatically expands them into deeply nested XML nodes (<field name="Address"><field name="Street">...), which is critical for complex PDF forms.
$xfdf = new \mikehaertl\pdftk\XfdfFile([
'Client.Name' => 'John Doe',
'Client.Age' => '30'
]);
$xfdf->saveAs('/path/to/export.xfdf');