1 | <?php |
||
2 | |||
3 | namespace TarfinLabs\EasyPdf; |
||
4 | |||
5 | use TCPDF; |
||
6 | use TCPDF_FONTS; |
||
7 | |||
8 | class EasyPdf |
||
9 | { |
||
10 | /** |
||
11 | * Tcpdf instance. |
||
12 | * |
||
13 | * @var TCPDF |
||
14 | */ |
||
15 | public $pdf; |
||
16 | |||
17 | /** |
||
18 | * Pdf header data. |
||
19 | * |
||
20 | * @var bool |
||
21 | */ |
||
22 | protected $header = false; |
||
23 | |||
24 | /** |
||
25 | * Pdf footer data. |
||
26 | * |
||
27 | * @var bool |
||
28 | */ |
||
29 | protected $footer = false; |
||
30 | |||
31 | /** |
||
32 | * Barcode style. |
||
33 | * |
||
34 | * @var array |
||
35 | */ |
||
36 | private $style = [ |
||
37 | 'border' => false, |
||
38 | 'padding' => 0, |
||
39 | 'fgcolor' => [0, 0, 0], |
||
40 | 'bgcolor' => false, |
||
41 | ]; |
||
42 | |||
43 | /** |
||
44 | * EasyPdf constructor. |
||
45 | */ |
||
46 | public function __construct(?string $imagePath = null) |
||
47 | { |
||
48 | if ($imagePath !== null && ! defined('K_PATH_IMAGES')) { |
||
49 | define('K_PATH_IMAGES', $imagePath); |
||
50 | } |
||
51 | |||
52 | $this->pdf = new TCPDF(); |
||
53 | $this->pdf->setPrintHeader($this->header); |
||
54 | $this->pdf->setPrintFooter($this->footer); |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * Add an array of information to the pdf instance. |
||
59 | * |
||
60 | * @param array $information |
||
61 | * @return EasyPdf |
||
62 | */ |
||
63 | public function withInformation(array $information) |
||
64 | { |
||
65 | foreach ($information as $key => $value) { |
||
66 | $this->setInformation($key, $value); |
||
67 | } |
||
68 | |||
69 | return $this; |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * Set an information on the pdf instance. |
||
74 | * |
||
75 | * @param $information |
||
76 | * @param $value |
||
77 | * @return EasyPdf |
||
78 | */ |
||
79 | protected function setInformation($information, $value) |
||
80 | { |
||
81 | $fn = 'Set'.$information; |
||
82 | |||
83 | if (is_array($value)) { |
||
84 | call_user_func([$this->pdf, $fn], ...$value); |
||
85 | } else { |
||
86 | call_user_func([$this->pdf, $fn], $value); |
||
87 | } |
||
88 | |||
89 | return $this; |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * Add an array of configurations on the pdf instance. |
||
94 | * |
||
95 | * @param array $config |
||
96 | * @return $this |
||
97 | */ |
||
98 | public function withConfig(array $config) |
||
99 | { |
||
100 | foreach ($config as $key => $value) { |
||
101 | $this->setConfig($key, $value); |
||
102 | } |
||
103 | |||
104 | return $this; |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * Set a configuration on the pdf instance. |
||
109 | * |
||
110 | * @param $config |
||
111 | * @param $value |
||
112 | * @return $this |
||
113 | */ |
||
114 | protected function setConfig($config, $value) |
||
115 | { |
||
116 | $fn = 'set'.$config; |
||
117 | |||
118 | if (is_array($value)) { |
||
119 | call_user_func([$this->pdf, $fn], ...$value); |
||
120 | } else { |
||
121 | call_user_func([$this->pdf, $fn], $value); |
||
122 | } |
||
123 | |||
124 | return $this; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * Add custom font on the pdf instance. |
||
129 | * |
||
130 | * @param string $font |
||
131 | * @param int|null $size |
||
132 | * @return EasyPdf |
||
133 | */ |
||
134 | public function addFont(string $font, int $size = null) |
||
135 | { |
||
136 | $tcpdfFont = TCPDF_FONTS::addTTFfont($font); |
||
137 | |||
138 | $this->pdf->SetFont($tcpdfFont, '', $size); |
||
139 | |||
140 | return $this; |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * Set given font as default font. |
||
145 | * |
||
146 | * @param string $font |
||
147 | * @param int|null $size |
||
148 | * @return $this |
||
149 | */ |
||
150 | public function setFont(string $font, int $size = null) |
||
151 | { |
||
152 | $this->pdf->SetFont($font, '', $size); |
||
153 | |||
154 | return $this; |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * Set page margins. |
||
159 | * |
||
160 | * @param float|int $left |
||
161 | * @param float|int $top |
||
162 | * @param float|int $right |
||
163 | * @param bool $keepMargins |
||
164 | * @return $this |
||
165 | */ |
||
166 | public function setMargins($left, $top, $right = null, bool $keepMargins = false) |
||
167 | { |
||
168 | $this->pdf->setMargins($left, $top, $right, $keepMargins); |
||
169 | |||
170 | return $this; |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * Add image to header. |
||
175 | * |
||
176 | * @param string $image |
||
177 | * @param int $width |
||
178 | * @param array $textColor |
||
179 | * @param array $lineColor |
||
180 | * @return $this |
||
181 | */ |
||
182 | public function setHeaderData(string $image, int $width, array $textColor = [], array $lineColor = []) |
||
183 | { |
||
184 | $this->pdf->setHeaderData($image, $width, '', '', $textColor, $lineColor); |
||
185 | |||
186 | return $this; |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * Set header margin. |
||
191 | * |
||
192 | * @param int $margin |
||
193 | * @return $this |
||
194 | */ |
||
195 | public function setHeaderMargin(int $margin) |
||
196 | { |
||
197 | $this->pdf->setHeaderMargin($margin); |
||
198 | |||
199 | return $this; |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * Set footer text and line colors. |
||
204 | * |
||
205 | * @param array $textColor |
||
206 | * @param array $lineColor |
||
207 | * @return $this |
||
208 | */ |
||
209 | public function setFooterData(array $textColor = [], array $lineColor = []) |
||
210 | { |
||
211 | $this->pdf->setFooterData($textColor, $lineColor); |
||
212 | |||
213 | return $this; |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * Set footer margin. |
||
218 | * |
||
219 | * @param int $margin |
||
220 | * @return $this |
||
221 | */ |
||
222 | public function setFooterMargin(int $margin) |
||
223 | { |
||
224 | $this->pdf->setFooterMargin($margin); |
||
225 | |||
226 | return $this; |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * Set footer font size. |
||
231 | * |
||
232 | * @param int|null $size |
||
233 | * @return $this |
||
234 | */ |
||
235 | public function setFooterFontSize(?int $size = null) |
||
236 | { |
||
237 | $this->pdf->setFooterFont([PDF_FONT_NAME_DATA, '', $size ?? PDF_FONT_SIZE_DATA]); |
||
238 | |||
239 | return $this; |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * Write html content. |
||
244 | * |
||
245 | * @param $html |
||
246 | * @return EasyPdf |
||
247 | */ |
||
248 | public function loadHtml($html) |
||
249 | { |
||
250 | $this->pdf->AddPage(); |
||
251 | $this->pdf->writeHTML($html); |
||
252 | |||
253 | return $this; |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * Add image to the pdf with given position and size. |
||
258 | * |
||
259 | * @param $image |
||
260 | * @param $x |
||
261 | * @param $y |
||
262 | * @param $width |
||
263 | * @param $height |
||
264 | * @return EasyPdf |
||
265 | */ |
||
266 | public function addImage($image, $x, $y, $width, $height) |
||
267 | { |
||
268 | if (stripos(substr($image, -5), '.svg') !== false) { |
||
269 | $this->pdf->ImageSvg($image, $x, $y, $width, $height); |
||
270 | } else { |
||
271 | $this->pdf->Image($image, $x, $y, $width, $height); |
||
272 | } |
||
273 | |||
274 | return $this; |
||
275 | } |
||
276 | |||
277 | /** |
||
278 | * Override default style for barcode. |
||
279 | * |
||
280 | * @param array $style |
||
281 | * @return $this |
||
282 | */ |
||
283 | public function setBarcodeStyle(array $style) |
||
284 | { |
||
285 | $this->style = $style; |
||
286 | |||
287 | return $this; |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * Add qrcode with best error correction to the pdf with given position and size. |
||
292 | * |
||
293 | * @param $text |
||
294 | * @param $x |
||
295 | * @param $y |
||
296 | * @param $width |
||
297 | * @param $height |
||
298 | * @param string $position |
||
299 | * @return EasyPdf |
||
300 | */ |
||
301 | public function addQrcode($text, $x, $y, $width, $height, $position = 'N') |
||
302 | { |
||
303 | $this->pdf->write2DBarcode($text, 'QRCODE,H', $x, $y, $width, $height, $this->style, $position); |
||
304 | |||
305 | return $this; |
||
306 | } |
||
307 | |||
308 | /** |
||
309 | * Add RAW2 barcode to the pdf with given position and size. |
||
310 | * |
||
311 | * @param $code |
||
312 | * @param $x |
||
313 | * @param $y |
||
314 | * @param $width |
||
315 | * @param $height |
||
316 | * @param string $position |
||
317 | * @return EasyPdf |
||
318 | */ |
||
319 | public function addBarcode($code, $x, $y, $width, $height, $position = 'N') |
||
320 | { |
||
321 | $this->pdf->write2DBarcode($code, 'RAW2', $x, $y, $width, $height, $this->style, $position); |
||
322 | |||
323 | return $this; |
||
324 | } |
||
325 | |||
326 | /** |
||
327 | * Set Tcpdf print header. |
||
328 | * |
||
329 | * @param null $header |
||
0 ignored issues
–
show
Documentation
Bug
introduced
by
![]() |
|||
330 | * @return EasyPdf |
||
331 | */ |
||
332 | public function setHeader($header = null) |
||
333 | { |
||
334 | if (! is_null($header)) { |
||
335 | $this->header = $header; |
||
336 | } |
||
337 | |||
338 | $this->pdf->setPrintHeader($this->header); |
||
339 | |||
340 | return $this; |
||
341 | } |
||
342 | |||
343 | /** |
||
344 | * Set Tcpdf print footer. |
||
345 | * |
||
346 | * @param null $footer |
||
0 ignored issues
–
show
|
|||
347 | * @return EasyPdf |
||
348 | */ |
||
349 | public function setFooter($footer = null) |
||
350 | { |
||
351 | if (! is_null($footer)) { |
||
352 | $this->footer = $footer; |
||
353 | } |
||
354 | |||
355 | $this->pdf->setPrintFooter($this->footer); |
||
356 | |||
357 | return $this; |
||
358 | } |
||
359 | |||
360 | /** |
||
361 | * Sets the active page of PDF. |
||
362 | * |
||
363 | * @param int $page |
||
364 | * @return $this |
||
365 | */ |
||
366 | public function setPage(int $page): self |
||
367 | { |
||
368 | $this->pdf->setPage($page); |
||
369 | |||
370 | return $this; |
||
371 | } |
||
372 | |||
373 | /** |
||
374 | * Save pdf to given path.. |
||
375 | * |
||
376 | * @param string $filename |
||
377 | * @return mixed |
||
378 | */ |
||
379 | public function save(string $filename) |
||
380 | { |
||
381 | $this->pdf->Output($filename, 'F'); |
||
382 | |||
383 | return $this; |
||
384 | } |
||
385 | |||
386 | /** |
||
387 | * Output the generated PDF to browser. |
||
388 | * |
||
389 | * @return $this |
||
390 | */ |
||
391 | public function stream() |
||
392 | { |
||
393 | $this->pdf->Output('doc.pdf', 'I'); |
||
394 | |||
395 | return $this; |
||
396 | } |
||
397 | |||
398 | /** |
||
399 | * Return pdf as a string. |
||
400 | * |
||
401 | * @return string |
||
402 | */ |
||
403 | public function content() |
||
404 | { |
||
405 | return $this->pdf->Output('doc.pdf', 'S'); |
||
406 | } |
||
407 | |||
408 | /** |
||
409 | * Create a new TCPDF instance. |
||
410 | * |
||
411 | * @return $this |
||
412 | */ |
||
413 | public function reset() |
||
414 | { |
||
415 | $this->pdf = new TCPDF(); |
||
416 | |||
417 | return $this; |
||
418 | } |
||
419 | |||
420 | /** |
||
421 | * Create a new Merge instance. |
||
422 | * |
||
423 | * @param array $files |
||
424 | * @return Merge |
||
425 | */ |
||
426 | public static function merge(array $files) |
||
427 | { |
||
428 | return new Merge($files); |
||
429 | } |
||
430 | |||
431 | /** |
||
432 | * Create a new Parser instance. |
||
433 | * |
||
434 | * @param string $path |
||
435 | * @return Parser |
||
436 | * |
||
437 | * @throws Exceptions\UnableToOpen |
||
438 | */ |
||
439 | public static function parser(string $path): Parser |
||
440 | { |
||
441 | return new Parser($path); |
||
442 | } |
||
443 | |||
444 | /** |
||
445 | * Set string for duplex attribute. |
||
446 | * |
||
447 | * @return EasyPdf |
||
448 | */ |
||
449 | public function setDuplex($name) |
||
450 | { |
||
451 | $this->pdf->setViewerPreferences(['Duplex' => $name]); |
||
452 | |||
453 | return $this; |
||
454 | } |
||
455 | } |
||
456 |