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 setasign\Fpdi\Fpdi; |
||
6 | use Symfony\Component\Finder\Finder; |
||
7 | use setasign\Fpdi\PdfParser\StreamReader; |
||
8 | |||
9 | /** |
||
10 | * Merge existing pdfs into one |
||
11 | */ |
||
12 | class Merger |
||
13 | { |
||
14 | /** |
||
15 | * List of pdf sources to merge |
||
16 | * |
||
17 | * @var SourceInterface[] |
||
18 | */ |
||
19 | private $sources = []; |
||
20 | |||
21 | /** |
||
22 | * @var Fpdi Fpdi object |
||
23 | */ |
||
24 | private $fpdi; |
||
25 | |||
26 | /** |
||
27 | * @var string Directory path used for temporary files |
||
28 | */ |
||
29 | private $tempDir; |
||
30 | |||
31 | /** |
||
32 | * Constructor |
||
33 | * |
||
34 | * @param Fpdi $fpdi |
||
35 | */ |
||
36 | public function __construct(Fpdi $fpdi = null) |
||
37 | { |
||
38 | $this->fpdi = $fpdi ?: new Fpdi; |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * Add raw PDF from string |
||
43 | * |
||
44 | * Note that your PDFs are merged in the order that you add them |
||
45 | * |
||
46 | * @param string $content Raw pdf content |
||
47 | * @param Pages $pages Specification of the pages to add |
||
48 | * @return void |
||
49 | */ |
||
50 | public function addRaw($content, Pages $pages = null) |
||
51 | { |
||
52 | $this->sources[] = new RawSource( |
||
53 | (string)$content, |
||
54 | $pages ?: new Pages |
||
55 | ); |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * Add PDF from filesystem path |
||
60 | * |
||
61 | * Note that your PDFs are merged in the order that you add them |
||
62 | * |
||
63 | * @param string $fname Name of file to add |
||
64 | * @param Pages $pages Pages to add from file |
||
65 | * @return void |
||
66 | * @throws Exception If $fname is not a valid file |
||
67 | */ |
||
68 | public function addFromFile($fname, Pages $pages = null, $cleanup = null) |
||
69 | { |
||
70 | if (!is_null($cleanup)) { |
||
71 | trigger_error('Use of $cleanup argument is deprecated', E_USER_DEPRECATED); |
||
72 | } |
||
73 | |||
74 | if (!is_file($fname) || !is_readable($fname)) { |
||
75 | throw new Exception("'$fname' is not a valid file"); |
||
76 | } |
||
77 | |||
78 | $this->sources[] = new FileSource( |
||
79 | $fname, |
||
80 | $pages ?: new Pages |
||
81 | ); |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Add files using iterator |
||
86 | * |
||
87 | * @param iterable $iterator Iterator or array with names of files to merge |
||
88 | * @param Pages $pages Optional pages constraint used for every added pdf |
||
89 | * @return void |
||
90 | * @throws Exception If $iterator is not valid |
||
91 | */ |
||
92 | public function addIterator($iterator, Pages $pages = null) |
||
93 | { |
||
94 | if (!is_array($iterator) && !$iterator instanceof \Traversable) { |
||
95 | throw new Exception("\$iterator must be traversable"); |
||
96 | } |
||
97 | |||
98 | foreach ($iterator as $fname) { |
||
99 | $this->addFromFile($fname, $pages); |
||
100 | } |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * Add files using a symfony finder |
||
105 | * |
||
106 | * @param Finder $finder |
||
107 | * @param Pages $pages Optional pages constraint used for every added pdf |
||
108 | * @return void |
||
109 | */ |
||
110 | public function addFinder(Finder $finder, Pages $pages = null) |
||
111 | { |
||
112 | foreach ($finder as $fileInfo) { |
||
113 | $this->addFromFile($fileInfo->getRealpath(), $pages); |
||
114 | } |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * Merges your provided PDFs and get raw string |
||
119 | * |
||
120 | * @return string |
||
121 | * @throws Exception If no PDFs were added |
||
122 | * @throws Exception If a specified page does not exist |
||
123 | * |
||
124 | * @TODO Should $sources be emptied after a merge? Why not implement a clear() method instead? |
||
125 | */ |
||
126 | public function merge() |
||
127 | { |
||
128 | if (empty($this->sources)) { |
||
129 | throw new Exception("Unable to merge, no PDFs added"); |
||
130 | } |
||
131 | |||
132 | /** @var string Name of source being processed */ |
||
133 | $name = ''; |
||
134 | |||
135 | try { |
||
136 | $fpdi = clone $this->fpdi; |
||
137 | |||
138 | foreach ($this->sources as $source) { |
||
139 | $name = $source->getName(); |
||
140 | |||
141 | /** @var int Total number of pages in pdf */ |
||
142 | $nrOfPagesInPdf = $fpdi->setSourceFile($source->getStreamReader()); |
||
143 | |||
144 | /** @var Pages The set of pages to merge, defaults to all pages */ |
||
145 | $pagesToMerge = $source->getPages()->hasPages() ? $source->getPages() : new Pages("1-$nrOfPagesInPdf"); |
||
146 | |||
147 | // Add specified pages |
||
148 | foreach ($pagesToMerge as $pageNr) { |
||
149 | $template = $fpdi->importPage($pageNr); |
||
150 | $size = $fpdi->getTemplateSize($template); |
||
151 | $fpdi->AddPage( |
||
152 | $size['width'] > $size['height'] ? 'L' : 'P', |
||
153 | [$size['width'], $size['height']] |
||
0 ignored issues
–
show
|
|||
154 | ); |
||
155 | $fpdi->useTemplate($template); |
||
156 | } |
||
157 | } |
||
158 | |||
159 | $this->sources = []; |
||
160 | |||
161 | return $fpdi->Output('', 'S'); |
||
162 | |||
163 | } catch (\Exception $e) { |
||
164 | throw new Exception("'{$e->getMessage()}' in '{$name}'", 0, $e); |
||
165 | } |
||
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 temporare 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 temporare 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 temporare files are no longer created', |
||
207 | E_USER_DEPRECATED |
||
208 | ); |
||
209 | |||
210 | $this->tempDir = $dirname; |
||
211 | } |
||
212 | } |
||
213 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: