Skip to content

Commit 0ceaaef

Browse files
authored
Merge pull request #70 from bravadomizzou/master
Add ability to get form data fields as array
2 parents 7eb1a38 + 7eaaaaf commit 0ceaaef

File tree

5 files changed

+158
-6
lines changed

5 files changed

+158
-6
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,9 @@ $data = $pdf->getData();
216216
// Get form data fields
217217
$pdf = new Pdf('/path/my.pdf');
218218
$data = $pdf->getDataFields();
219+
echo $data; // raw string; also can directly call $data->__toString();
220+
print_r($data); // metadata as array; also can directly call $data->__toArray();
221+
$field1Value = $data[0]['FieldValue']; // array access to a field's value
219222
```
220223

221224
#### How to perform more than one operation on a PDF

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
}
1212
],
1313
"require": {
14-
"php": ">=5.0.0",
14+
"php": ">=5.3.0",
1515
"mikehaertl/php-shellcommand": "^1.0.2",
1616
"mikehaertl/php-tmpfile": "^1.0.0"
1717
},

src/DataFields.php

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
3+
namespace mikehaertl\pdftk;
4+
5+
use ArrayObject;
6+
7+
/**
8+
* Class DataFields
9+
*
10+
* @author Ray Holland <raymondaholland+php-pdftk@gmail.com>
11+
*/
12+
class DataFields extends ArrayObject
13+
{
14+
private $_string;
15+
16+
private $_array;
17+
18+
/**
19+
* DataFields constructor.
20+
*
21+
* @param string $input
22+
* @param int $flags
23+
* @param string $iterator_class
24+
*/
25+
public function __construct($input = null, $flags = 0, $iterator_class = "ArrayIterator")
26+
{
27+
$this->_string = $input ?: '';
28+
$this->_array = $this->parseData($this->_string);
29+
30+
return parent::__construct($this->_array, $flags, $iterator_class);
31+
}
32+
33+
/**
34+
* @return string
35+
*/
36+
public function __toString()
37+
{
38+
return $this->_string;
39+
}
40+
41+
/**
42+
* @return array
43+
*/
44+
public function __toArray()
45+
{
46+
return $this->_array;
47+
}
48+
49+
/**
50+
* Parse the output of dump_data_fields into something usable.
51+
* Derived from: http://stackoverflow.com/a/34864936/744228
52+
* Example input (includes '---' line):
53+
* ---
54+
* FieldType: Text
55+
* FieldName: Text1
56+
* FieldFlags: 0
57+
* FieldValue: University of Missouri : Ray-Holland
58+
* FieldValueDefault: University of Missouri : Ray-Holland
59+
* FieldJustification: Left
60+
* FieldMaxLength: 99
61+
*
62+
* @param $dataString
63+
* @return array
64+
*/
65+
private function parseData($dataString)
66+
{
67+
$output = array();
68+
$field = array();
69+
foreach (explode(PHP_EOL, $dataString) as $line) {
70+
$trimmedLine = trim($line);
71+
if ($trimmedLine === '---' || $trimmedLine === '') {
72+
// Block completed; process it
73+
if (sizeof($field) > 0) {
74+
$output[] = $field;
75+
}
76+
$field = array();
77+
continue;
78+
}
79+
// Process contents of data block
80+
$parts = explode(':', $line);
81+
$key = null;
82+
$value = null;
83+
84+
// Handle colon in the value
85+
if (sizeof($parts) !== 2) {
86+
$key = $parts[0];
87+
unset($parts[0]);
88+
$value = implode(':', $parts);
89+
}
90+
91+
$key = $key ?: trim($parts[0]);
92+
$value = $value ?: trim($parts[1]);
93+
if (isset($field[$key])) {
94+
$field[$key] = (array) $field[$key];
95+
$field[$key][] = $value;
96+
}
97+
else {
98+
$field[$key] = $value;
99+
}
100+
}
101+
102+
// process final block
103+
if (sizeof($field) > 0) {
104+
$output[] = $field;
105+
}
106+
107+
return $output;
108+
}
109+
}

src/Pdf.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class Pdf
6262
protected $_data_utf8;
6363

6464
/**
65-
* @var string the PDF form field data as returned from getDataFields()
65+
* @var DataFields the PDF form field data as returned from getDataFields()
6666
*/
6767
protected $_dataFields;
6868
protected $_dataFields_utf8;
@@ -331,7 +331,7 @@ public function getData($utf8 = true)
331331

332332
/**
333333
* @param bool $utf8 whether to dump the data UTF-8 encoded. Default is true.
334-
* @return string|bool data about the PDF form fields or false on failure
334+
* @return DataFields|bool data about the PDF form fields or false on failure
335335
*/
336336
public function getDataFields($utf8 = true)
337337
{
@@ -342,7 +342,7 @@ public function getDataFields($utf8 = true)
342342
if (!$command->execute()) {
343343
return false;
344344
} else {
345-
$this->$property = trim($command->getOutput());
345+
$this->$property = new DataFields(trim($command->getOutput()));
346346
}
347347
}
348348
return $this->$property;

tests/PdfTest.php

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -545,8 +545,10 @@ public function testCanGetDataFields()
545545

546546
$pdf = new Pdf($form);
547547
$data = $pdf->getDataFields();
548-
$this->assertInternalType('string', $data);
549-
$this->assertEquals($this->formDataFields, $data);
548+
$this->assertInternalType('string', $data->__toString());
549+
$this->assertEquals($this->formDataFields, $data->__toString());
550+
$this->assertInternalType('array', $data->__toArray());
551+
$this->assertEquals($this->formDataFieldsArray, $data->__toArray());
550552
}
551553

552554
protected function getDocument1()
@@ -621,4 +623,42 @@ protected function getOutFile()
621623
FieldJustification: Left
622624
EOD;
623625

626+
protected $formDataFieldsArray = array(
627+
array(
628+
'FieldType' => 'Button',
629+
'FieldName' => 'checkbox 1',
630+
'FieldFlags' => '0',
631+
'FieldValue' => 'On',
632+
'FieldJustification' => 'Left',
633+
'FieldStateOption' => array('Off', 'On'),
634+
),
635+
array(
636+
'FieldType' => 'Button',
637+
'FieldName' => 'checkbox 2',
638+
'FieldFlags' => '0',
639+
'FieldValue' => 'On',
640+
'FieldJustification' => 'Left',
641+
'FieldStateOption' => array('Off', 'On'),
642+
),
643+
array(
644+
'FieldType' => 'Button',
645+
'FieldName' => 'radio 1',
646+
'FieldFlags' => '49152',
647+
'FieldValue' => '2',
648+
'FieldJustification' => 'Left',
649+
'FieldStateOption' => array('1', '2', 'Off'),
650+
),
651+
array(
652+
'FieldType' => 'Text',
653+
'FieldName' => 'email',
654+
'FieldFlags' => '0',
655+
'FieldJustification' => 'Left',
656+
),
657+
array(
658+
'FieldType' => 'Text',
659+
'FieldName' => 'name',
660+
'FieldFlags' => '0',
661+
'FieldJustification' => 'Left',
662+
),
663+
);
624664
}

0 commit comments

Comments
 (0)