1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @file |
5
|
|
|
* This file is part of the PdfParser library. |
6
|
|
|
* |
7
|
|
|
* @author Sébastien MALOT <[email protected]> |
8
|
|
|
* @date 2017-01-03 |
9
|
|
|
* |
10
|
|
|
* @license LGPLv3 |
11
|
|
|
* @url <https://github.com/smalot/pdfparser> |
12
|
|
|
* |
13
|
|
|
* PdfParser is a pdf library written in PHP, extraction oriented. |
14
|
|
|
* Copyright (C) 2017 - Sébastien MALOT <[email protected]> |
15
|
|
|
* |
16
|
|
|
* This program is free software: you can redistribute it and/or modify |
17
|
|
|
* it under the terms of the GNU Lesser General Public License as published by |
18
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
19
|
|
|
* (at your option) any later version. |
20
|
|
|
* |
21
|
|
|
* This program is distributed in the hope that it will be useful, |
22
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
23
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
24
|
|
|
* GNU Lesser General Public License for more details. |
25
|
|
|
* |
26
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
27
|
|
|
* along with this program. |
28
|
|
|
* If not, see <http://www.pdfparser.org/sites/default/LICENSE.txt>. |
29
|
|
|
*/ |
30
|
|
|
|
31
|
|
|
namespace Smalot\PdfParser; |
32
|
|
|
|
33
|
|
|
use Smalot\PdfParser\Element\ElementArray; |
34
|
|
|
use Smalot\PdfParser\Element\ElementMissing; |
35
|
|
|
use Smalot\PdfParser\Element\ElementStruct; |
36
|
|
|
use Smalot\PdfParser\Element\ElementXRef; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Class Header |
40
|
|
|
*/ |
41
|
|
|
class Header |
42
|
|
|
{ |
43
|
|
|
/** |
44
|
|
|
* @var Document |
45
|
|
|
*/ |
46
|
|
|
protected $document = null; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var Element[] |
50
|
|
|
*/ |
51
|
|
|
protected $elements = null; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param Element[] $elements list of elements |
55
|
|
|
* @param Document $document document |
56
|
|
|
*/ |
57
|
44 |
|
public function __construct($elements = [], Document $document = null) |
58
|
|
|
{ |
59
|
44 |
|
$this->elements = $elements; |
60
|
44 |
|
$this->document = $document; |
61
|
44 |
|
} |
62
|
|
|
|
63
|
30 |
|
public function init() |
64
|
|
|
{ |
65
|
30 |
|
foreach ($this->elements as $name => $element) { |
66
|
30 |
|
$element->init(); |
67
|
|
|
} |
68
|
30 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Returns all elements. |
72
|
|
|
*/ |
73
|
25 |
|
public function getElements() |
74
|
|
|
{ |
75
|
25 |
|
foreach ($this->elements as $name => $element) { |
76
|
25 |
|
$this->resolveXRef($name); |
77
|
|
|
} |
78
|
|
|
|
79
|
25 |
|
return $this->elements; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Used only for debug. |
84
|
|
|
* |
85
|
|
|
* @return array |
86
|
|
|
*/ |
87
|
1 |
|
public function getElementTypes() |
88
|
|
|
{ |
89
|
1 |
|
$types = []; |
90
|
|
|
|
91
|
1 |
|
foreach ($this->elements as $key => $element) { |
92
|
1 |
|
$types[$key] = \get_class($element); |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
return $types; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param bool $deep |
100
|
|
|
* |
101
|
|
|
* @return array |
102
|
|
|
*/ |
103
|
23 |
|
public function getDetails($deep = true) |
104
|
|
|
{ |
105
|
23 |
|
$values = []; |
106
|
23 |
|
$elements = $this->getElements(); |
107
|
|
|
|
108
|
23 |
|
foreach ($elements as $key => $element) { |
109
|
23 |
|
if ($element instanceof self && $deep) { |
110
|
|
|
$values[$key] = $element->getDetails($deep); |
111
|
23 |
|
} elseif ($element instanceof PDFObject && $deep) { |
112
|
1 |
|
$values[$key] = $element->getDetails(false); |
113
|
23 |
|
} elseif ($element instanceof ElementArray) { |
114
|
2 |
|
if ($deep) { |
115
|
2 |
|
$values[$key] = $element->getDetails(); |
116
|
|
|
} |
117
|
23 |
|
} elseif ($element instanceof Element) { |
118
|
23 |
|
$values[$key] = (string) $element; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
23 |
|
return $values; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Indicate if an element name is available in header. |
127
|
|
|
* |
128
|
|
|
* @param string $name The name of the element |
129
|
|
|
* |
130
|
|
|
* @return bool |
131
|
|
|
*/ |
132
|
31 |
|
public function has($name) |
133
|
|
|
{ |
134
|
31 |
|
return \array_key_exists($name, $this->elements); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param string $name |
139
|
|
|
* |
140
|
|
|
* @return Element|PDFObject |
141
|
|
|
*/ |
142
|
32 |
|
public function get($name) |
143
|
|
|
{ |
144
|
32 |
|
if (\array_key_exists($name, $this->elements)) { |
145
|
32 |
|
return $this->resolveXRef($name); |
146
|
|
|
} |
147
|
|
|
|
148
|
29 |
|
return new ElementMissing(); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Resolve XRef to object. |
153
|
|
|
* |
154
|
|
|
* @param string $name |
155
|
|
|
* |
156
|
|
|
* @return Element|PDFObject |
157
|
|
|
* |
158
|
|
|
* @throws \Exception |
159
|
|
|
*/ |
160
|
34 |
|
protected function resolveXRef($name) |
161
|
|
|
{ |
162
|
34 |
|
if (($obj = $this->elements[$name]) instanceof ElementXRef && null !== $this->document) { |
163
|
|
|
/** @var ElementXRef $obj */ |
164
|
26 |
|
$object = $this->document->getObjectById($obj->getId()); |
165
|
|
|
|
166
|
26 |
|
if (null === $object) { |
167
|
2 |
|
return new ElementMissing(); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
// Update elements list for future calls. |
171
|
26 |
|
$this->elements[$name] = $object; |
172
|
|
|
} |
173
|
|
|
|
174
|
34 |
|
return $this->elements[$name]; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @param string $content The content to parse |
179
|
|
|
* @param Document $document The document |
180
|
|
|
* @param int $position The new position of the cursor after parsing |
181
|
|
|
* |
182
|
|
|
* @return Header |
183
|
|
|
*/ |
184
|
18 |
|
public static function parse($content, Document $document, &$position = 0) |
185
|
|
|
{ |
186
|
|
|
/* @var Header $header */ |
187
|
18 |
|
if ('<<' == substr(trim($content), 0, 2)) { |
188
|
17 |
|
$header = ElementStruct::parse($content, $document, $position); |
189
|
|
|
} else { |
190
|
5 |
|
$elements = ElementArray::parse($content, $document, $position); |
191
|
5 |
|
$header = new self([], $document); |
192
|
|
|
|
193
|
5 |
|
if ($elements) { |
194
|
4 |
|
$header = new self($elements->getRawContent(), null); |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
198
|
18 |
|
if ($header) { |
199
|
18 |
|
return $header; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
// Build an empty header. |
203
|
|
|
return new self([], $document); |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|