Skip to content

Commit

Permalink
Issue #148 Support multi value fields
Browse files Browse the repository at this point in the history
  • Loading branch information
mikehaertl committed Jul 26, 2021
1 parent 01bb1d8 commit 7aec98c
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 11 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ $pdf = new Pdf('/full/path/to/form.pdf');
$result = $pdf->fillForm([
'name'=>'ÄÜÖ äüö мирано čárka',
'nested.name' => 'valX',

// Only for Xfdf files: multi-value fields
'pets' => ['Cat', 'Mouse'],
])
->needAppearances()
->saveAs('filled.pdf');
Expand Down
56 changes: 51 additions & 5 deletions src/XfdfFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,42 @@
* This class represents a temporary XFDF file that can be used to fill a PDF
* form with valid unicode characters.
*
* Form data must be passed to the constructor as an array in this form:
*
* ```
* [
* // Field name => field value
* 'Firstname' => 'John',
*
* // Hierarchical/nested fields in dot notation
* 'Address.Street' => 'Some Street',
* 'Address.City' => 'Any City',
*
* // Multi value fields
* 'Pets' => ['Cat', 'Mouse'],
* ]
* ```
*
* This will result in the following XML structure (header/footer omitted):
*
* ```
* <field name="Firstname">
* <Value>John</Value>
* </field>
* <field name="Address">
* <field name="Street">
* <Value>Some Street</Value>
* </field>
* <field name="City">
* <Value>Any City</Value>
* </field>
* </field>
* <field name="Pets">
* <Value>Cat</Value>
* <Value>Mouse</Value>
* </field>
* ```
*
* @author Tomas Holy <[email protected]>
* @author Michael Härtl <[email protected]>
* @license http://www.opensource.org/licenses/MIT
Expand All @@ -20,20 +56,23 @@ class XfdfFile extends File
<?xml version="1.0" encoding="UTF-8"?>
<xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">
<fields>
FDF;

// XFDF file footer
const XFDF_FOOTER = <<<FDF
</fields>
</xfdf>
FDF;

/**
* Constructor
*
*
* @param array $data the form data as name => value
* @param string|null $suffix the optional suffix for the tmp file
* @param string|null $suffix the optional prefix for the tmp file. If null
* @param string|null $prefix the optional prefix for the tmp file. If null
* 'php_tmpfile_' is used.
* @param string|null $directory directory where the file should be
* created. Autodetected if not provided.
Expand Down Expand Up @@ -142,11 +181,18 @@ protected function writeFields($fp, $fields)
foreach ($fields as $key => $value) {
$key = $this->xmlEncode($key);
fwrite($fp, "<field name=\"$key\">\n");
if (is_array($value)) {
$this->writeFields($fp, $value);
if (!is_array($value)) {
$value = array($value);
}
if (isset($value[0])) {
// Numeric keys: single or multi-value field
foreach($value as $val) {
$val = $this->xmlEncode($val);
fwrite($fp, "<value>$val</value>\n");
}
} else {
$value = $this->xmlEncode($value);
fwrite($fp, "<value>$value</value>\n");
// String keys: nested/hierarchical fields
$this->writeFields($fp, $value);
}
fwrite($fp, "</field>\n");
}
Expand Down
11 changes: 7 additions & 4 deletions tests/XfdfFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ public function testXfdfFileCreation()
'checkbox 2' => 0,
'radio 1' => 2,
'address.street' => 'some street',
"umlauts-in-value" => "öäüÖÄÜ",
"umlauts-in-value" => 'öäüÖÄÜ',
'some.other.value' => 'val1',
'some.other.value2' => 'val2',
"öäüÖÄÜ" => "umlauts in key",
"special-in-value" => "ۧ&()",
"€ key" => "special in key",
'öäüÖÄÜ' => "umlauts in key",
'special-in-value' => 'ۧ&()',
'€ key' => 'special in key',
'multivalue1' => ['val1', 'val2'],
'nested.dummy' => 'valX',
'nested.multivalue2' => ['val3', 'val4'],
);

$oXfdfFile = new XfdfFile($data, null, null, __DIR__);
Expand Down
18 changes: 16 additions & 2 deletions tests/files/XfdfFileTest.xfdf
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">
<fields><field name="name">
<fields>
<field name="name">
<value>Jürgen čárka čČćĆđĐ мирано</value>
</field>
<field name="email">
Expand Down Expand Up @@ -45,5 +46,18 @@
<field name="€ key">
<value>special in key</value>
</field>
<field name="multivalue1">
<value>val1</value>
<value>val2</value>
</field>
<field name="nested">
<field name="dummy">
<value>valX</value>
</field>
<field name="multivalue2">
<value>val3</value>
<value>val4</value>
</field>
</field>
</fields>
</xfdf>
</xfdf>

0 comments on commit 7aec98c

Please sign in to comment.