1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PDFtk wrapper |
4
|
|
|
* |
5
|
|
|
* @copyright 2014-2024 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 bookmarks to PDF. |
20
|
|
|
*/ |
21
|
|
|
class Bookmarks |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Bookmarks. |
25
|
|
|
* |
26
|
|
|
* @var Bookmark[] |
27
|
|
|
*/ |
28
|
|
|
private $bookmarks = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Maximum page number. |
32
|
|
|
* |
33
|
|
|
* @var int |
34
|
|
|
*/ |
35
|
|
|
private $maxpage = -1; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var WrapperInterface |
39
|
|
|
*/ |
40
|
|
|
private $wrapper; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Constructor. |
44
|
|
|
*/ |
45
|
|
|
public function __construct(WrapperInterface $wrapper = null) |
46
|
|
|
{ |
47
|
18 |
|
$this->wrapper = $wrapper ?: new PdftkWrapper(); |
48
|
|
|
} |
49
|
18 |
|
|
50
|
18 |
|
/** |
51
|
|
|
* Add bookmark to page. |
52
|
|
|
*/ |
53
|
|
|
public function add(Bookmark $bookmark): self |
54
|
|
|
{ |
55
|
|
|
if (0 === $bookmark->getPageNumber()) { |
56
|
|
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
12 |
|
if ($bookmark->getLevel() < 1 || $bookmark->getLevel() > 99) { |
60
|
|
|
throw new PdfException(sprintf('Invalid bookmark level: %s', $bookmark->getLevel())); |
61
|
12 |
|
} |
62
|
1 |
|
|
63
|
|
|
if ($bookmark->getPageNumber() < 1) { |
64
|
|
|
throw new PdfException(sprintf('Invalid page number: %s', $bookmark->getPageNumber())); |
65
|
11 |
|
} elseif ($this->maxpage > 0 && $bookmark->getPageNumber() > $this->maxpage) { |
66
|
1 |
|
throw new PdfException('Page number out of range!'); |
67
|
|
|
} |
68
|
|
|
|
69
|
10 |
|
$this->bookmarks[] = $bookmark; |
70
|
1 |
|
|
71
|
9 |
|
return $this; |
72
|
1 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
8 |
|
* Returns all bookmarks. |
76
|
|
|
* |
77
|
8 |
|
* @return Bookmark[] |
78
|
|
|
*/ |
79
|
|
|
public function all(): array |
80
|
|
|
{ |
81
|
|
|
return $this->bookmarks; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
9 |
|
* Deletes a bookmark. |
86
|
|
|
*/ |
87
|
9 |
|
public function remove(Bookmark $bookmark): self |
88
|
|
|
{ |
89
|
|
|
foreach ($this->bookmarks as $key => $currentBookmark) { |
90
|
|
|
// weak comparison - value objects with equal values are considered equal |
91
|
|
|
if ($currentBookmark == $bookmark) { |
92
|
|
|
unset($this->bookmarks[$key]); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$this->resetArrayIndizes(); |
97
|
1 |
|
|
98
|
|
|
return $this; |
99
|
1 |
|
} |
100
|
|
|
|
101
|
1 |
|
/** |
102
|
1 |
|
* Delete bookmark from page. |
103
|
|
|
*/ |
104
|
|
|
public function removeByPageNumber(int $pageNumber): self |
105
|
|
|
{ |
106
|
1 |
|
foreach ($this->bookmarks as $key => $currentBookmark) { |
107
|
|
|
if ($currentBookmark->getPageNumber() === $pageNumber) { |
108
|
1 |
|
unset($this->bookmarks[$key]); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$this->resetArrayIndizes(); |
113
|
|
|
|
114
|
|
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
1 |
|
* Remove all bookmarks. |
119
|
|
|
*/ |
120
|
1 |
|
public function clear(): self |
121
|
1 |
|
{ |
122
|
1 |
|
$this->bookmarks = []; |
123
|
|
|
|
124
|
|
|
return $this; |
125
|
|
|
} |
126
|
1 |
|
|
127
|
|
|
/** |
128
|
1 |
|
* Apply bookmarks to PDF file. |
129
|
|
|
* |
130
|
|
|
* @throws PdfException |
131
|
|
|
*/ |
132
|
|
|
public function apply(string $infile, string $outfile = null): self |
133
|
|
|
{ |
134
|
|
|
$this->wrapper->applyBookmarks($this, $infile, $outfile); |
135
|
|
|
|
136
|
3 |
|
return $this; |
137
|
|
|
} |
138
|
3 |
|
|
139
|
|
|
/** |
140
|
3 |
|
* Imports bookmarks from a PDF file. |
141
|
|
|
* |
142
|
|
|
* @throws PdfException |
143
|
|
|
*/ |
144
|
|
|
public function import(string $infile): self |
145
|
|
|
{ |
146
|
|
|
$this->wrapper->importBookmarks($this, $infile); |
147
|
|
|
|
148
|
|
|
return $this; |
149
|
|
|
} |
150
|
|
|
|
151
|
3 |
|
/** |
152
|
|
|
* Sets the maximum page number of the PDF to validate page numbers. |
153
|
3 |
|
* |
154
|
|
|
* @internal |
155
|
2 |
|
*/ |
156
|
|
|
public function setMaxpage(int $maxpage): self |
157
|
|
|
{ |
158
|
|
|
$this->maxpage = $maxpage; |
159
|
|
|
|
160
|
|
|
return $this; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Resets the bookmark array indizes (e.g. after removal of a bookmark). |
165
|
3 |
|
*/ |
166
|
|
|
private function resetArrayIndizes(): void |
167
|
3 |
|
{ |
168
|
2 |
|
$this->bookmarks = array_values($this->bookmarks); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|