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