1 | <?php |
||||
2 | declare(strict_types = 1); |
||||
3 | |||||
4 | namespace Phauthentic\Presentation\View; |
||||
5 | |||||
6 | /** |
||||
7 | * Abstract Pdf |
||||
8 | */ |
||||
9 | class PdfView extends View implements PdfViewInterface |
||||
10 | { |
||||
11 | /** |
||||
12 | * Html to be rendered |
||||
13 | * |
||||
14 | * @var string |
||||
15 | */ |
||||
16 | protected $html = ''; |
||||
17 | |||||
18 | /** |
||||
19 | * Page size of the pdf |
||||
20 | * |
||||
21 | * @var string |
||||
22 | */ |
||||
23 | protected $pageSize = self::PAGE_SIZE_A4; |
||||
24 | |||||
25 | /** |
||||
26 | * Orientation of the pdf |
||||
27 | * |
||||
28 | * @var string |
||||
29 | */ |
||||
30 | protected $orientation = self::ORIENTATION_PORTRAIT; |
||||
31 | |||||
32 | /** |
||||
33 | * Encoding |
||||
34 | * |
||||
35 | * @var string |
||||
36 | */ |
||||
37 | protected $encoding = 'UTF-8'; |
||||
38 | |||||
39 | /** |
||||
40 | * Footer HTML |
||||
41 | * |
||||
42 | * @var array |
||||
43 | */ |
||||
44 | protected $footer = ['left' => null, 'center' => null, 'right' => null]; |
||||
45 | |||||
46 | /** |
||||
47 | * Header HTML |
||||
48 | * |
||||
49 | * @var array |
||||
50 | */ |
||||
51 | protected $header = ['left' => null, 'center' => null, 'right' => null]; |
||||
52 | |||||
53 | /** |
||||
54 | * Bottom margin in mm |
||||
55 | * |
||||
56 | * @var number |
||||
57 | */ |
||||
58 | protected $marginBottom = null; |
||||
59 | |||||
60 | /** |
||||
61 | * Left margin in mm |
||||
62 | * |
||||
63 | * @var number |
||||
64 | */ |
||||
65 | protected $marginLeft = null; |
||||
66 | |||||
67 | /** |
||||
68 | * Right margin in mm |
||||
69 | * |
||||
70 | * @var number |
||||
71 | */ |
||||
72 | protected $marginRight = null; |
||||
73 | |||||
74 | /** |
||||
75 | * Top margin in mm |
||||
76 | * |
||||
77 | * @var number |
||||
78 | */ |
||||
79 | protected $marginTop = null; |
||||
80 | |||||
81 | /** |
||||
82 | * Title of the document |
||||
83 | * |
||||
84 | * @var string |
||||
85 | */ |
||||
86 | protected $title = null; |
||||
87 | |||||
88 | /** |
||||
89 | * Javascript delay before rendering document in milliseconds |
||||
90 | * |
||||
91 | * @var int |
||||
92 | */ |
||||
93 | protected $delay = null; |
||||
94 | |||||
95 | /** |
||||
96 | * Window status required before rendering document |
||||
97 | * |
||||
98 | * @var string |
||||
99 | */ |
||||
100 | protected $windowStatus = null; |
||||
101 | |||||
102 | /** |
||||
103 | * Flag that tells if we need to pass it through crypto |
||||
104 | * |
||||
105 | * @var bool |
||||
106 | */ |
||||
107 | protected $protect = false; |
||||
108 | |||||
109 | /** |
||||
110 | * User password, used with crypto |
||||
111 | * |
||||
112 | * @var string |
||||
113 | */ |
||||
114 | protected $userPassword = null; |
||||
115 | |||||
116 | /** |
||||
117 | * Owner password, used with crypto |
||||
118 | * |
||||
119 | * @var string |
||||
120 | */ |
||||
121 | protected $ownerPassword = null; |
||||
122 | |||||
123 | /** |
||||
124 | * Permissions that are allowed, used with crypto |
||||
125 | * |
||||
126 | * false: none |
||||
127 | * true: all |
||||
128 | * array: List of permissions that are allowed |
||||
129 | * |
||||
130 | * @var mixed |
||||
131 | */ |
||||
132 | protected $allow = false; |
||||
133 | |||||
134 | /** |
||||
135 | * Available permissions |
||||
136 | * |
||||
137 | * @var array |
||||
138 | */ |
||||
139 | protected $availablePermissions = [ |
||||
140 | 'print', |
||||
141 | 'degraded_print', |
||||
142 | 'modify', |
||||
143 | 'assembly', |
||||
144 | 'copy_contents', |
||||
145 | 'screen_readers', |
||||
146 | 'annotate', |
||||
147 | 'fill_in', |
||||
148 | ]; |
||||
149 | |||||
150 | /** |
||||
151 | * Constructor |
||||
152 | * |
||||
153 | * @param array $config Pdf configs to use |
||||
154 | */ |
||||
155 | 1 | public function __construct($config = []) |
|||
156 | { |
||||
157 | $options = [ |
||||
158 | 1 | 'pageSize', |
|||
159 | 'orientation', |
||||
160 | 'margin', |
||||
161 | 'title', |
||||
162 | 'encoding', |
||||
163 | 'protect', |
||||
164 | 'userPassword', |
||||
165 | 'ownerPassword', |
||||
166 | 'permissions', |
||||
167 | 'cache', |
||||
168 | 'delay', |
||||
169 | 'windowStatus', |
||||
170 | ]; |
||||
171 | 1 | foreach ($options as $option) { |
|||
172 | 1 | if (isset($config[$option])) { |
|||
173 | 1 | $this->{$option}($config[$option]); |
|||
174 | } |
||||
175 | } |
||||
176 | 1 | } |
|||
177 | |||||
178 | /** |
||||
179 | * Get/Set Html. |
||||
180 | * |
||||
181 | * @param null|string $html Html to set |
||||
182 | * @return mixed |
||||
183 | */ |
||||
184 | 1 | public function getHtml(): string |
|||
185 | { |
||||
186 | 1 | return $this->html; |
|||
187 | } |
||||
188 | |||||
189 | /** |
||||
190 | * Sets the HTML to render |
||||
191 | * |
||||
192 | * @param string $html HTML |
||||
193 | * @return |
||||
194 | */ |
||||
195 | public function setHtml($html = null): PdfViewInterface |
||||
196 | { |
||||
197 | $this->html = $html; |
||||
198 | |||||
199 | return $this; |
||||
200 | } |
||||
201 | |||||
202 | /** |
||||
203 | * Set Page size. |
||||
204 | * |
||||
205 | * @param null|string $pageSize Page size to set |
||||
206 | * @return \Phauthentic\Presentation\View\PdfViewInterface |
||||
207 | */ |
||||
208 | public function setPageSize(?string $pageSize): PdfViewInterface |
||||
209 | { |
||||
210 | $this->pageSize = $pageSize; |
||||
211 | |||||
212 | return $this; |
||||
213 | } |
||||
214 | |||||
215 | /** |
||||
216 | * Gets the page size |
||||
217 | * |
||||
218 | * @return string|null |
||||
219 | */ |
||||
220 | 1 | public function getPageSize(): ?string |
|||
221 | { |
||||
222 | 1 | return $this->pageSize; |
|||
223 | } |
||||
224 | |||||
225 | /** |
||||
226 | * Gets the orientation |
||||
227 | * |
||||
228 | * @return string |
||||
229 | */ |
||||
230 | 1 | public function getOrientation(): ?string |
|||
231 | { |
||||
232 | 1 | return $this->orientation; |
|||
233 | } |
||||
234 | |||||
235 | /** |
||||
236 | * Get/Set Orientation. |
||||
237 | * |
||||
238 | * @param null|string $orientation orientation to set |
||||
239 | * @return mixed |
||||
240 | */ |
||||
241 | public function setOrientation(?string $orientation): PdfViewInterface |
||||
242 | { |
||||
243 | if ($orientation === null) { |
||||
244 | return $this->orientation; |
||||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||||
245 | } |
||||
246 | $this->orientation = $orientation; |
||||
247 | |||||
248 | return $this; |
||||
249 | } |
||||
250 | |||||
251 | /** |
||||
252 | * Get/Set Encoding. |
||||
253 | * |
||||
254 | * @param null|string $encoding encoding to set |
||||
255 | * @return $this |
||||
256 | */ |
||||
257 | public function setEncoding(?string $encoding): PdfViewInterface |
||||
258 | { |
||||
259 | $this->encoding = $encoding; |
||||
260 | |||||
261 | return $this; |
||||
262 | } |
||||
263 | |||||
264 | /** |
||||
265 | * Gets the encoding |
||||
266 | * |
||||
267 | * @return string|null |
||||
268 | */ |
||||
269 | 1 | public function getEncoding(): ?string |
|||
270 | { |
||||
271 | 1 | return $this->encoding; |
|||
272 | } |
||||
273 | |||||
274 | /** |
||||
275 | * Get/Set footer HTML. |
||||
276 | * |
||||
277 | * @param null|string $left left side footer |
||||
278 | * @param null|string $center center footer |
||||
279 | * @param null|string $right right side footer |
||||
280 | * @return mixed |
||||
281 | */ |
||||
282 | 1 | public function footer($left = null, $center = null, $right = null) |
|||
283 | { |
||||
284 | 1 | if ($left === null && $center === null && $right === null) { |
|||
285 | 1 | return $this->footer; |
|||
286 | } |
||||
287 | |||||
288 | if (is_array($left)) { |
||||
0 ignored issues
–
show
|
|||||
289 | extract($left, EXTR_IF_EXISTS); |
||||
290 | } |
||||
291 | |||||
292 | $this->footer = compact('left', 'center', 'right'); |
||||
293 | |||||
294 | return $this; |
||||
295 | } |
||||
296 | |||||
297 | /** |
||||
298 | * Get/Set header HTML. |
||||
299 | * |
||||
300 | * @param null|string $left left side header |
||||
301 | * @param null|string $center center header |
||||
302 | * @param null|string $right right side header |
||||
303 | * @return mixed |
||||
304 | */ |
||||
305 | 1 | public function header($left = null, $center = null, $right = null) |
|||
306 | { |
||||
307 | 1 | if ($left === null && $center === null && $right === null) { |
|||
308 | 1 | return $this->header; |
|||
309 | } |
||||
310 | |||||
311 | if (is_array($left)) { |
||||
0 ignored issues
–
show
|
|||||
312 | extract($left, EXTR_IF_EXISTS); |
||||
313 | } |
||||
314 | |||||
315 | $this->header = compact('left', 'center', 'right'); |
||||
316 | |||||
317 | return $this; |
||||
318 | } |
||||
319 | |||||
320 | /** |
||||
321 | * Gets the margins |
||||
322 | * |
||||
323 | * @return array |
||||
324 | */ |
||||
325 | 1 | public function getMargin(): array |
|||
326 | { |
||||
327 | return [ |
||||
328 | 1 | 'bottom' => $this->marginBottom, |
|||
329 | 1 | 'left' => $this->marginLeft, |
|||
330 | 1 | 'right' => $this->marginRight, |
|||
331 | 1 | 'top' => $this->marginTop, |
|||
332 | ]; |
||||
333 | } |
||||
334 | |||||
335 | /** |
||||
336 | * Get/Set page margins. |
||||
337 | * |
||||
338 | * Several options are available |
||||
339 | * |
||||
340 | * Array format |
||||
341 | * ------------ |
||||
342 | * First param can be an array with the following options: |
||||
343 | * - bottom |
||||
344 | * - left |
||||
345 | * - right |
||||
346 | * - top |
||||
347 | * |
||||
348 | * Set margin for all borders |
||||
349 | * -------------------------- |
||||
350 | * $bottom is set to a string |
||||
351 | * Leave all other parameters empty |
||||
352 | * |
||||
353 | * Set margin for horizontal and vertical |
||||
354 | * -------------------------------------- |
||||
355 | * $bottom value will be set to bottom and top |
||||
356 | * $left value will be set to left and right |
||||
357 | * |
||||
358 | * @param null|string|array $bottom bottom margin, or array of margins |
||||
359 | * @param null|string $left left margin |
||||
360 | * @param null|string $right right margin |
||||
361 | * @param null|string $top top margin |
||||
362 | * @return mixed |
||||
363 | */ |
||||
364 | public function setMargin($bottom = null, $left = null, $right = null, $top = null) |
||||
365 | { |
||||
366 | if (is_array($bottom)) { |
||||
367 | extract($bottom, EXTR_IF_EXISTS); |
||||
368 | } |
||||
369 | |||||
370 | if ($bottom && $left === null && $right === null && $top === null) { |
||||
371 | $left = $right = $top = $bottom; |
||||
372 | } |
||||
373 | |||||
374 | if ($bottom && $top === null) { |
||||
375 | $top = $bottom; |
||||
376 | } |
||||
377 | |||||
378 | if ($left && $right === null) { |
||||
379 | $right = $left; |
||||
380 | } |
||||
381 | |||||
382 | $this->marginBottom($bottom); |
||||
0 ignored issues
–
show
It seems like
$bottom can also be of type array ; however, parameter $margin of Phauthentic\Presentation...PdfView::marginBottom() does only seem to accept null|string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
383 | $this->marginLeft($left); |
||||
0 ignored issues
–
show
It seems like
$left can also be of type array ; however, parameter $margin of Phauthentic\Presentation...w\PdfView::marginLeft() does only seem to accept null|string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
384 | $this->marginRight($right); |
||||
0 ignored issues
–
show
It seems like
$right can also be of type array ; however, parameter $margin of Phauthentic\Presentation...\PdfView::marginRight() does only seem to accept null|string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
385 | $this->marginTop($top); |
||||
0 ignored issues
–
show
It seems like
$top can also be of type array ; however, parameter $margin of Phauthentic\Presentation\View\PdfView::marginTop() does only seem to accept null|string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
386 | |||||
387 | return $this; |
||||
388 | } |
||||
389 | |||||
390 | /** |
||||
391 | * Get/Set bottom margin. |
||||
392 | * |
||||
393 | * @param null|string $margin margin to set |
||||
394 | * @return mixed |
||||
395 | */ |
||||
396 | public function marginBottom($margin = null) |
||||
397 | { |
||||
398 | if ($margin === null) { |
||||
399 | return $this->marginBottom; |
||||
400 | } |
||||
401 | $this->marginBottom = $margin; |
||||
0 ignored issues
–
show
It seems like
$margin of type string is incompatible with the declared type double|integer of property $marginBottom .
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property. Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.. ![]() |
|||||
402 | |||||
403 | return $this; |
||||
404 | } |
||||
405 | |||||
406 | /** |
||||
407 | * Get/Set left margin. |
||||
408 | * |
||||
409 | * @param null|string $margin margin to set |
||||
410 | * @return mixed |
||||
411 | */ |
||||
412 | public function marginLeft($margin = null) |
||||
413 | { |
||||
414 | if ($margin === null) { |
||||
415 | return $this->marginLeft; |
||||
416 | } |
||||
417 | $this->marginLeft = $margin; |
||||
0 ignored issues
–
show
It seems like
$margin of type string is incompatible with the declared type double|integer of property $marginLeft .
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property. Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.. ![]() |
|||||
418 | |||||
419 | return $this; |
||||
420 | } |
||||
421 | |||||
422 | /** |
||||
423 | * Get/Set right margin. |
||||
424 | * |
||||
425 | * @param null|string $margin margin to set |
||||
426 | * @return mixed |
||||
427 | */ |
||||
428 | public function marginRight($margin = null) |
||||
429 | { |
||||
430 | if ($margin === null) { |
||||
431 | return $this->marginRight; |
||||
432 | } |
||||
433 | $this->marginRight = $margin; |
||||
0 ignored issues
–
show
It seems like
$margin of type string is incompatible with the declared type double|integer of property $marginRight .
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property. Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.. ![]() |
|||||
434 | |||||
435 | return $this; |
||||
436 | } |
||||
437 | |||||
438 | /** |
||||
439 | * Get/Set top margin. |
||||
440 | * |
||||
441 | * @param null|string $margin margin to set |
||||
442 | * @return mixed |
||||
443 | */ |
||||
444 | public function marginTop($margin = null) |
||||
445 | { |
||||
446 | if ($margin === null) { |
||||
447 | return $this->marginTop; |
||||
448 | } |
||||
449 | $this->marginTop = $margin; |
||||
0 ignored issues
–
show
It seems like
$margin of type string is incompatible with the declared type double|integer of property $marginTop .
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property. Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.. ![]() |
|||||
450 | |||||
451 | return $this; |
||||
452 | } |
||||
453 | |||||
454 | /** |
||||
455 | * Set document title |
||||
456 | * |
||||
457 | * @param null|string $title title to set |
||||
458 | * @return mixed |
||||
459 | */ |
||||
460 | public function setTitle(?string $title) |
||||
461 | { |
||||
462 | $this->title = $title; |
||||
463 | |||||
464 | return $this; |
||||
465 | } |
||||
466 | |||||
467 | /** |
||||
468 | * Gets the title |
||||
469 | * |
||||
470 | * @return string|null |
||||
471 | */ |
||||
472 | 1 | public function getTitle(): ?string |
|||
473 | { |
||||
474 | 1 | return $this->title; |
|||
475 | } |
||||
476 | |||||
477 | /** |
||||
478 | * |
||||
479 | */ |
||||
480 | 1 | public function getJsDelay() |
|||
481 | { |
||||
482 | 1 | return $this->delay(); |
|||
483 | } |
||||
484 | |||||
485 | /** |
||||
486 | * Get/Set javascript delay. |
||||
487 | * |
||||
488 | * @param null|int $delay delay to set in milliseconds |
||||
489 | * @return mixed |
||||
490 | */ |
||||
491 | 1 | public function delay($delay = null) |
|||
492 | { |
||||
493 | 1 | if ($delay === null) { |
|||
494 | 1 | return $this->delay; |
|||
495 | } |
||||
496 | $this->delay = $delay; |
||||
497 | |||||
498 | return $this; |
||||
499 | } |
||||
500 | |||||
501 | /** |
||||
502 | * Set the required window status for rendering |
||||
503 | * |
||||
504 | * Waits until the status is equal to the string before rendering the pdf |
||||
505 | * |
||||
506 | * @param null|string $status status to set as string |
||||
507 | * @return \Phauthentic\Presentation\View\PdfViewInterface |
||||
508 | */ |
||||
509 | public function setWindowStatus(?string $status): PdfViewInterface |
||||
510 | { |
||||
511 | $this->windowStatus = $status; |
||||
512 | |||||
513 | return $this; |
||||
514 | } |
||||
515 | |||||
516 | /** |
||||
517 | * Gets the window status |
||||
518 | * |
||||
519 | * @return null|string |
||||
520 | */ |
||||
521 | 1 | public function getWindowStatus(): ?string |
|||
522 | { |
||||
523 | 1 | return $this->windowStatus; |
|||
524 | } |
||||
525 | |||||
526 | /** |
||||
527 | * Get/Set protection. |
||||
528 | * |
||||
529 | * @param null|bool $protect True or false |
||||
530 | * @return mixed |
||||
531 | */ |
||||
532 | public function protect($protect = null) |
||||
533 | { |
||||
534 | if ($protect === null) { |
||||
535 | return $this->protect; |
||||
536 | } |
||||
537 | $this->protect = $protect; |
||||
538 | |||||
539 | return $this; |
||||
540 | } |
||||
541 | |||||
542 | /** |
||||
543 | * Get/Set userPassword |
||||
544 | * |
||||
545 | * The user password is used to control who can open the PDF document. |
||||
546 | * |
||||
547 | * @param null|string $password password to set |
||||
548 | * @return mixed |
||||
549 | */ |
||||
550 | public function userPassword($password = null) |
||||
551 | { |
||||
552 | if ($password === null) { |
||||
553 | return $this->userPassword; |
||||
554 | } |
||||
555 | $this->userPassword = $password; |
||||
556 | |||||
557 | return $this; |
||||
558 | } |
||||
559 | |||||
560 | /** |
||||
561 | * Get/Set ownerPassword. |
||||
562 | * |
||||
563 | * The owner password is used to control who can modify, print, manage the PDF document. |
||||
564 | * |
||||
565 | * @param null|string $password password to set |
||||
566 | * @return mixed |
||||
567 | */ |
||||
568 | public function ownerPassword($password = null) |
||||
569 | { |
||||
570 | if ($password === null) { |
||||
571 | return $this->ownerPassword; |
||||
572 | } |
||||
573 | $this->ownerPassword = $password; |
||||
574 | |||||
575 | return $this; |
||||
576 | } |
||||
577 | |||||
578 | /** |
||||
579 | * Get/Set permissions. |
||||
580 | * |
||||
581 | * all: allow all permissions |
||||
582 | * none: allow no permissions |
||||
583 | * array: list of permissions that are allowed |
||||
584 | * |
||||
585 | * @param null|bool|array $permissions Permissions to set |
||||
586 | * @throws \Cake\Core\Exception\Exception |
||||
587 | * @return mixed |
||||
588 | */ |
||||
589 | public function permissions($permissions = null) |
||||
590 | { |
||||
591 | if (!$this->protect()) { |
||||
592 | return $this; |
||||
593 | } |
||||
594 | |||||
595 | if ($permissions === null) { |
||||
596 | return $this->allow; |
||||
597 | } |
||||
598 | |||||
599 | if (is_string($permissions) && $permissions == 'all') { |
||||
0 ignored issues
–
show
|
|||||
600 | $permissions = true; |
||||
601 | } |
||||
602 | |||||
603 | if (is_string($permissions) && $permissions == 'none') { |
||||
0 ignored issues
–
show
|
|||||
604 | $permissions = false; |
||||
605 | } |
||||
606 | |||||
607 | if (is_array($permissions)) { |
||||
608 | foreach ($permissions as $permission) { |
||||
609 | if (!in_array($permission, $this->availablePermissions)) { |
||||
610 | throw new Exception(sprintf('Invalid permission: %s', $permission)); |
||||
611 | } |
||||
612 | |||||
613 | if (!$this->crypto()->permissionImplemented($permission)) { |
||||
0 ignored issues
–
show
The method
crypto() does not exist on Phauthentic\Presentation\View\PdfView .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||
614 | throw new Exception(sprintf('Permission not implemented in crypto engine: %s', $permission)); |
||||
615 | } |
||||
616 | } |
||||
617 | } |
||||
618 | |||||
619 | $this->allow = $permissions; |
||||
620 | |||||
621 | return $this; |
||||
622 | } |
||||
623 | } |
||||
624 |