1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace iio\libmergepdf; |
4
|
|
|
|
5
|
|
|
use setasign\Fpdi\Fpdi; |
6
|
|
|
use Symfony\Component\Finder\Finder; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Merge existing pdfs into one |
10
|
|
|
*/ |
11
|
|
|
class Merger |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* List of pdf sources to merge |
15
|
|
|
* |
16
|
|
|
* @var SourceInterface[] |
17
|
|
|
*/ |
18
|
|
|
private $sources = []; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var Fpdi Fpdi object |
22
|
|
|
*/ |
23
|
|
|
private $fpdi; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string Directory path used for temporary files |
27
|
|
|
*/ |
28
|
|
|
private $tempDir; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Constructor |
32
|
|
|
* |
33
|
|
|
* @param Fpdi $fpdi |
34
|
|
|
*/ |
35
|
|
|
public function __construct(Fpdi $fpdi = null) |
36
|
|
|
{ |
37
|
|
|
$this->fpdi = $fpdi ?: new Fpdi; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Add raw PDF from string |
42
|
|
|
* |
43
|
|
|
* Note that your PDFs are merged in the order that you add them |
44
|
|
|
* |
45
|
|
|
* @param string $content Raw pdf content |
46
|
|
|
* @param Pages $pages Specification of the pages to add |
47
|
|
|
* @return void |
48
|
|
|
*/ |
49
|
|
|
public function addRaw($content, Pages $pages = null) |
50
|
|
|
{ |
51
|
|
|
$this->sources[] = new RawSource($content, $pages ?: new Pages); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Add PDF from file |
56
|
|
|
* |
57
|
|
|
* Note that your PDFs are merged in the order that you add them |
58
|
|
|
* |
59
|
|
|
* @param string $filename Name of file to add |
60
|
|
|
* @param Pages $pages Pages to add from file |
61
|
|
|
* @return void |
62
|
|
|
* @throws Exception If $filename is not a valid file |
63
|
|
|
*/ |
64
|
|
|
public function addFile($filename, Pages $pages = null) |
65
|
|
|
{ |
66
|
|
|
// TODO vad h'nder egentligen annars... |
67
|
|
|
if (!is_file($filename) || !is_readable($filename)) { |
68
|
|
|
throw new Exception("'$filename' is not a valid file"); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$this->sources[] = new FileSource($filename, $pages ?: new Pages); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Add PDF from file |
76
|
|
|
* |
77
|
|
|
* @deprecated Since version 3.1 |
78
|
|
|
*/ |
79
|
|
|
public function addFromFile($fname, Pages $pages = null, $cleanup = null) |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
trigger_error('addFromFile() is deprecated, use addFile() instead', E_USER_DEPRECATED); |
82
|
|
|
$this->addFile($fname, $pages); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Add files using iterator |
87
|
|
|
* |
88
|
|
|
* @param iterable $iterator Iterator or array with names of files to merge |
89
|
|
|
* @param Pages $pages Optional pages constraint used for every added pdf |
90
|
|
|
* @return void |
91
|
|
|
* @throws Exception If $iterator is not valid |
92
|
|
|
*/ |
93
|
|
|
public function addIterator($iterator, Pages $pages = null) |
94
|
|
|
{ |
95
|
|
|
if (!is_array($iterator) && !$iterator instanceof \Traversable) { |
96
|
|
|
throw new Exception("\$iterator must be traversable"); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
foreach ($iterator as $filename) { |
100
|
|
|
$this->addFile($filename, $pages); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Add files using a symfony finder |
106
|
|
|
* |
107
|
|
|
* @param Finder $finder |
108
|
|
|
* @param Pages $pages Optional pages constraint used for every added pdf |
109
|
|
|
* @return void |
110
|
|
|
*/ |
111
|
|
|
public function addFinder(Finder $finder, Pages $pages = null) |
112
|
|
|
{ |
113
|
|
|
foreach ($finder as $fileInfo) { |
114
|
|
|
$this->addFile($fileInfo->getRealpath(), $pages); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Merges your provided PDFs and get raw string |
120
|
|
|
* |
121
|
|
|
* @return string |
122
|
|
|
* @throws Exception If no PDFs were added |
123
|
|
|
* @throws Exception If a specified page does not exist |
124
|
|
|
* |
125
|
|
|
* @TODO Should $sources be emptied after a merge? Why not implement a clear() method instead? |
126
|
|
|
*/ |
127
|
|
|
public function merge() |
128
|
|
|
{ |
129
|
|
|
if (empty($this->sources)) { |
130
|
|
|
throw new Exception("Unable to merge, no PDFs added"); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** @var string Name of source being processed */ |
134
|
|
|
$name = ''; |
135
|
|
|
|
136
|
|
|
try { |
137
|
|
|
$fpdi = clone $this->fpdi; |
138
|
|
|
|
139
|
|
|
foreach ($this->sources as $source) { |
140
|
|
|
$name = $source->getName(); |
141
|
|
|
|
142
|
|
|
/** @var int Total number of pages in pdf */ |
143
|
|
|
$nrOfPagesInPdf = $fpdi->setSourceFile($source->getStreamReader()); |
144
|
|
|
|
145
|
|
|
/** @var Pages The set of pages to merge, defaults to all pages */ |
146
|
|
|
$pagesToMerge = $source->getPages()->hasPages() ? $source->getPages() : new Pages("1-$nrOfPagesInPdf"); |
147
|
|
|
|
148
|
|
|
// Add specified pages |
149
|
|
|
foreach ($pagesToMerge as $pageNr) { |
150
|
|
|
$template = $fpdi->importPage($pageNr); |
151
|
|
|
$size = $fpdi->getTemplateSize($template); |
152
|
|
|
$fpdi->AddPage( |
153
|
|
|
$size['width'] > $size['height'] ? 'L' : 'P', |
154
|
|
|
[$size['width'], $size['height']] |
|
|
|
|
155
|
|
|
); |
156
|
|
|
$fpdi->useTemplate($template); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
$this->sources = []; |
161
|
|
|
|
162
|
|
|
return $fpdi->Output('', 'S'); |
163
|
|
|
|
164
|
|
|
} catch (\Exception $e) { |
165
|
|
|
throw new Exception("'{$e->getMessage()}' in '{$name}'", 0, $e); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Create temporary file and return name |
171
|
|
|
* |
172
|
|
|
* @deprecated Since version 3.1 |
173
|
|
|
*/ |
174
|
|
|
public function getTempFname() |
175
|
|
|
{ |
176
|
|
|
trigger_error( |
177
|
|
|
'Use of getTempFname() is deprecated as temporare files are no longer created', |
178
|
|
|
E_USER_DEPRECATED |
179
|
|
|
); |
180
|
|
|
|
181
|
|
|
return tempnam($this->getTempDir(), "libmergepdf"); |
|
|
|
|
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Get directory path for temporary files |
186
|
|
|
* |
187
|
|
|
* @deprecated Since version 3.1 |
188
|
|
|
*/ |
189
|
|
|
public function getTempDir() |
190
|
|
|
{ |
191
|
|
|
trigger_error( |
192
|
|
|
'Use of getTempDir() is deprecated as temporare files are no longer created', |
193
|
|
|
E_USER_DEPRECATED |
194
|
|
|
); |
195
|
|
|
|
196
|
|
|
return $this->tempDir ?: sys_get_temp_dir(); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Set directory path for temporary files |
201
|
|
|
* |
202
|
|
|
* @deprecated Since version 3.1 |
203
|
|
|
*/ |
204
|
|
|
public function setTempDir($dirname) |
205
|
|
|
{ |
206
|
|
|
trigger_error( |
207
|
|
|
'Use of setTempDir() is deprecated as temporare files are no longer created', |
208
|
|
|
E_USER_DEPRECATED |
209
|
|
|
); |
210
|
|
|
|
211
|
|
|
$this->tempDir = $dirname; |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.