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
|
|
|
class Pdftk |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var Bookmarks |
22
|
|
|
*/ |
23
|
|
|
private $bookmarks; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var Metadata |
27
|
|
|
*/ |
28
|
|
|
private $metadata; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var Pages |
32
|
|
|
*/ |
33
|
|
|
private $pages; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var Joiner |
37
|
|
|
*/ |
38
|
|
|
private $joiner; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var Splitter |
42
|
|
|
*/ |
43
|
|
|
private $splitter; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var PdftkWrapper |
47
|
|
|
*/ |
48
|
|
|
private $wrapper; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Constructor. |
52
|
|
|
* |
53
|
|
|
* @param array $options |
54
|
|
|
* @param PdftkWrapper $wrapper |
55
|
|
|
* |
56
|
|
|
* @throws PdfException |
57
|
|
|
*/ |
58
|
6 |
|
public function __construct($options = [], PdftkWrapper $wrapper = null) |
59
|
|
|
{ |
60
|
6 |
|
$this->wrapper = $wrapper ?: new PdftkWrapper(); |
61
|
|
|
|
62
|
6 |
|
if (isset($options['binary'])) { |
63
|
2 |
|
$this->wrapper->setBinary($options['binary']); |
64
|
|
|
} |
65
|
|
|
|
66
|
5 |
|
$this->bookmarks = new Bookmarks($wrapper); |
67
|
5 |
|
$this->metadata = new Metadata($wrapper); |
68
|
5 |
|
$this->pages = new Pages($wrapper); |
69
|
5 |
|
$this->joiner = new Joiner($wrapper); |
70
|
5 |
|
$this->splitter = new Splitter($wrapper); |
71
|
5 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Returns the bookmarks object. |
75
|
|
|
* |
76
|
|
|
* @return Bookmarks |
77
|
|
|
*/ |
78
|
2 |
|
public function getBookmarks() |
79
|
|
|
{ |
80
|
2 |
|
return $this->bookmarks; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Alias for getBookmarks(). |
85
|
|
|
* |
86
|
|
|
* @return Bookmarks |
87
|
|
|
*/ |
88
|
2 |
|
public function bookmarks() |
89
|
|
|
{ |
90
|
2 |
|
return $this->getBookmarks(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Returns the metadata object. |
95
|
|
|
* |
96
|
|
|
* @return Metadata |
97
|
|
|
*/ |
98
|
2 |
|
public function getMetadata() |
99
|
|
|
{ |
100
|
2 |
|
return $this->metadata; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Alias for getMetadata(). |
105
|
|
|
* |
106
|
|
|
* @return Metadata |
107
|
|
|
*/ |
108
|
2 |
|
public function metadata() |
109
|
|
|
{ |
110
|
2 |
|
return $this->getMetadata(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Returns the pages object. |
115
|
|
|
* |
116
|
|
|
* @return Pages |
117
|
|
|
*/ |
118
|
1 |
|
public function getPages() |
119
|
|
|
{ |
120
|
1 |
|
return $this->pages; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Alias for getPages(). |
125
|
|
|
* |
126
|
|
|
* @return Pages |
127
|
|
|
*/ |
128
|
1 |
|
public function pages() |
129
|
|
|
{ |
130
|
1 |
|
return $this->getPages(); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Returns the PDF joiner. |
135
|
|
|
* |
136
|
|
|
* @return Joiner |
137
|
|
|
*/ |
138
|
1 |
|
public function getJoiner() |
139
|
|
|
{ |
140
|
1 |
|
return $this->joiner; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Alias for getJoiner(). |
145
|
|
|
* |
146
|
|
|
* @return Joiner |
147
|
|
|
*/ |
148
|
1 |
|
public function joiner() |
149
|
|
|
{ |
150
|
1 |
|
return $this->getJoiner(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Returns the PDF splitter. |
155
|
|
|
* |
156
|
|
|
* @return Splitter |
157
|
|
|
*/ |
158
|
1 |
|
public function getSplitter() |
159
|
|
|
{ |
160
|
1 |
|
return $this->splitter; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Alias for getSplitter(). |
165
|
|
|
* |
166
|
|
|
* @return Splitter |
167
|
|
|
*/ |
168
|
1 |
|
public function splitter() |
169
|
|
|
{ |
170
|
1 |
|
return $this->getSplitter(); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Apply bookmarks and metadata to PDF file. |
175
|
|
|
* |
176
|
|
|
* @param string $infile |
177
|
|
|
* @param string $outfile |
178
|
|
|
* |
179
|
|
|
* @return self |
180
|
|
|
*/ |
181
|
1 |
|
public function apply($infile, $outfile = null) |
182
|
|
|
{ |
183
|
1 |
|
$this->bookmarks->apply($infile, $outfile); |
184
|
1 |
|
$this->metadata->apply($outfile); |
185
|
|
|
|
186
|
1 |
|
return $this; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Imports bookmarks, metadata and page information from a PDF file. |
191
|
|
|
* |
192
|
|
|
* @param string $infile |
193
|
|
|
* |
194
|
|
|
* @return self |
195
|
|
|
*/ |
196
|
2 |
|
public function import($infile) |
197
|
|
|
{ |
198
|
2 |
|
$dump = $this->wrapper->getPdfDataDump($infile); |
199
|
|
|
|
200
|
2 |
|
$this->pages->clear(); |
201
|
2 |
|
$this->bookmarks->clear(); |
202
|
2 |
|
$this->metadata->clear(); |
203
|
|
|
|
204
|
2 |
|
$this->pages->importFromDump($dump); |
205
|
2 |
|
$this->bookmarks->setMaxpage(count($this->pages->all())); |
206
|
2 |
|
$this->bookmarks->importFromDump($dump); |
207
|
2 |
|
$this->metadata->importFromDump($dump); |
208
|
|
|
|
209
|
2 |
|
return $this; |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|