1 | <?php |
||
2 | /** |
||
3 | * PDFtk wrapper |
||
4 | * |
||
5 | * @copyright 2014-2019 Institute of Legal Medicine, Medical University of Innsbruck |
||
6 | * @author Martin Pircher <[email protected]> |
||
7 | * @author Andreas Erhard <[email protected]> |
||
8 | * @license LGPL-3.0-only |
||
9 | * @link http://www.gerichtsmedizin.at/ |
||
10 | * |
||
11 | * @package pdftk |
||
12 | */ |
||
13 | |||
14 | namespace Gmi\Toolkit\Pdftk; |
||
15 | |||
16 | use Gmi\Toolkit\Pdftk\Exception\PdfException; |
||
17 | |||
18 | /** |
||
19 | * Apply metadata to PDF. |
||
20 | */ |
||
21 | class Metadata |
||
22 | { |
||
23 | /** |
||
24 | * InfoBlock |
||
25 | * |
||
26 | * Creator: if the PDF was converted from another format, the application |
||
27 | * used to create the original document. |
||
28 | * |
||
29 | * Producer: if the PDF was converted from another format, the application |
||
30 | * which did the conversion. |
||
31 | * |
||
32 | * @var array |
||
33 | */ |
||
34 | private $metadata = []; |
||
35 | |||
36 | /** |
||
37 | * @var PdftkWrapper |
||
38 | */ |
||
39 | private $wrapper; |
||
40 | |||
41 | /** |
||
42 | * Constructor. |
||
43 | * |
||
44 | * @param PdftkWrapper $wrapper |
||
45 | */ |
||
46 | 18 | public function __construct(PdftkWrapper $wrapper = null) |
|
47 | { |
||
48 | 18 | $this->wrapper = $wrapper ?: new PdftkWrapper(); |
|
49 | 18 | } |
|
50 | |||
51 | /** |
||
52 | * Set metadata key/value. |
||
53 | * |
||
54 | * @param string $key |
||
55 | * @param string $value |
||
56 | * |
||
57 | * @return self |
||
58 | * |
||
59 | * @throws PdftkException |
||
60 | */ |
||
61 | 12 | public function set($key, $value) |
|
62 | { |
||
63 | 12 | $this->checkKey($key); |
|
64 | |||
65 | 11 | $this->metadata[$key] = (string) $value; |
|
66 | |||
67 | 11 | return $this; |
|
68 | } |
||
69 | |||
70 | /** |
||
71 | * Get metadata value from key. |
||
72 | * |
||
73 | * @param string $key |
||
74 | * |
||
75 | * @return string|bool |
||
76 | * |
||
77 | * @throws PdftkException |
||
78 | */ |
||
79 | 8 | public function get($key) |
|
80 | { |
||
81 | 8 | $this->checkKey($key); |
|
82 | |||
83 | 7 | if (!isset($this->metadata[$key])) { |
|
84 | 2 | return false; |
|
85 | } |
||
86 | |||
87 | 6 | return $this->metadata[$key]; |
|
88 | } |
||
89 | |||
90 | /** |
||
91 | * Unset metadata for key. |
||
92 | * |
||
93 | * @param string $key |
||
94 | * |
||
95 | * @return self |
||
96 | * |
||
97 | * @throws PdftkException |
||
98 | */ |
||
99 | 2 | public function remove($key) |
|
100 | { |
||
101 | 2 | $this->checkKey($key); |
|
102 | |||
103 | 1 | unset($this->metadata[$key]); |
|
104 | |||
105 | 1 | return $this; |
|
106 | } |
||
107 | |||
108 | /** |
||
109 | * Checks whether a key is set. |
||
110 | * |
||
111 | * @param string $key |
||
112 | * |
||
113 | * @return bool |
||
114 | * |
||
115 | * @throws PdftkException |
||
116 | */ |
||
117 | 2 | public function has($key) |
|
118 | { |
||
119 | 2 | $this->checkKey($key); |
|
120 | |||
121 | 1 | return isset($this->metadata[$key]); |
|
122 | } |
||
123 | |||
124 | /** |
||
125 | * Returns all current keys. |
||
126 | * |
||
127 | * @return array |
||
128 | */ |
||
129 | 2 | public function keys() |
|
130 | { |
||
131 | 2 | return array_keys($this->metadata); |
|
132 | } |
||
133 | |||
134 | /** |
||
135 | * Returns all metadata entries [key => value]. |
||
136 | * |
||
137 | * @return array |
||
138 | */ |
||
139 | 1 | public function all() |
|
140 | { |
||
141 | 1 | return $this->metadata; |
|
142 | } |
||
143 | |||
144 | /** |
||
145 | * Remove all metadata. |
||
146 | * |
||
147 | * @return self |
||
148 | */ |
||
149 | 3 | public function clear() |
|
150 | { |
||
151 | 3 | $this->metadata = []; |
|
152 | |||
153 | 3 | return $this; |
|
154 | } |
||
155 | |||
156 | /** |
||
157 | * Apply metadata to file. |
||
158 | * |
||
159 | * @param string $infile |
||
160 | * @param string $outfile |
||
161 | * |
||
162 | * @return self |
||
163 | * |
||
164 | * @throws PdftkException |
||
165 | */ |
||
166 | 5 | public function apply($infile, $outfile = null) |
|
167 | { |
||
168 | 5 | $metaBlock = ''; |
|
169 | 5 | foreach ($this->metadata as $k => $v) { |
|
170 | 5 | $metaBlock .= $this->buildInfoBlock($k, $v); |
|
171 | } |
||
172 | 5 | $this->wrapper->updatePdfDataFromDump($infile, $metaBlock, $outfile); |
|
173 | |||
174 | 4 | return $this; |
|
175 | } |
||
176 | |||
177 | /** |
||
178 | * Imports metadata from a PDF file. |
||
179 | * |
||
180 | * @param string $infile |
||
181 | * |
||
182 | * @return self |
||
183 | */ |
||
184 | 4 | public function import($infile) |
|
185 | { |
||
186 | 4 | $dump = $this->wrapper->getPdfDataDump($infile); |
|
187 | 4 | $this->importFromDump($dump); |
|
188 | |||
189 | 4 | return $this; |
|
190 | } |
||
191 | |||
192 | /** |
||
193 | * Imports PDF metadata from a pdftk dump. |
||
194 | * |
||
195 | * @param string $dump |
||
196 | * |
||
197 | * @return self |
||
198 | */ |
||
199 | 6 | public function importFromDump($dump) |
|
200 | { |
||
201 | 6 | $matches = []; |
|
202 | 6 | $regex = '/InfoBegin??\r?\nInfoKey: (?<key>.*)??\r?\nInfoValue: (?<value>.*)??\r?\n/'; |
|
203 | 6 | preg_match_all($regex, $dump, $matches, PREG_SET_ORDER); |
|
204 | |||
205 | 6 | foreach ($matches as $meta) { |
|
206 | 6 | $this->set($meta['key'], $meta['value']); |
|
207 | } |
||
208 | |||
209 | 6 | return $this; |
|
210 | } |
||
211 | |||
212 | /** |
||
213 | * Builds an InfoBlock string. |
||
214 | * |
||
215 | * @param string $key |
||
216 | * @param string $value |
||
217 | * |
||
218 | * @return string |
||
219 | * |
||
220 | * @throws PdftkException |
||
221 | */ |
||
222 | 5 | private function buildInfoBlock($key, $value) |
|
223 | { |
||
224 | 5 | $this->checkKey($key); |
|
225 | |||
226 | return |
||
227 | 5 | 'InfoBegin' . PHP_EOL . |
|
228 | 5 | 'InfoKey: ' . $key . PHP_EOL . |
|
229 | 5 | 'InfoValue: ' . (string) $value . PHP_EOL; |
|
230 | } |
||
231 | |||
232 | /** |
||
233 | * Checks a metadata key. |
||
234 | * |
||
235 | * @param string $key |
||
236 | * |
||
237 | * @throws PdfException |
||
238 | */ |
||
239 | 15 | private function checkKey($key) |
|
240 | { |
||
241 | 15 | if (!is_string($key) || empty($key)) { |
|
0 ignored issues
–
show
introduced
by
![]() |
|||
242 | 4 | throw new PdfException(sprintf('Invalid key name "%s"!', $key)); |
|
243 | } |
||
244 | 11 | } |
|
245 | } |
||
246 |