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