Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
13 / 13 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
|
100.00% |
13 / 13 |
|
100.00% |
4 / 4 |
7 | |
100.00% |
1 / 1 |
|
__construct | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
4 | |||
__call | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
save | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getRenderer | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | /** |
3 | * This file is part of PHPWord - A pure PHP library for reading and writing |
4 | * word processing documents. |
5 | * |
6 | * PHPWord is free software distributed under the terms of the GNU Lesser |
7 | * General Public License version 3 as published by the Free Software Foundation. |
8 | * |
9 | * For the full copyright and license information, please read the LICENSE |
10 | * file that was distributed with this source code. For the full list of |
11 | * contributors, visit https://github.com/PHPOffice/PHPWord/contributors. |
12 | * |
13 | * @see https://github.com/PHPOffice/PhpWord |
14 | * |
15 | * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 |
16 | */ |
17 | |
18 | namespace PhpOffice\PhpWord\Writer; |
19 | |
20 | use PhpOffice\PhpWord\Exception\Exception; |
21 | use PhpOffice\PhpWord\PhpWord; |
22 | use PhpOffice\PhpWord\Settings; |
23 | use PhpOffice\PhpWord\Writer\PDF\AbstractRenderer; |
24 | |
25 | /** |
26 | * PDF Writer. |
27 | * |
28 | * @since 0.10.0 |
29 | */ |
30 | class PDF |
31 | { |
32 | /** |
33 | * The wrapper for the requested PDF rendering engine. |
34 | * |
35 | * @var \PhpOffice\PhpWord\Writer\PDF\AbstractRenderer |
36 | */ |
37 | private $renderer; |
38 | |
39 | /** |
40 | * Instantiate a new renderer of the configured type within this container class. |
41 | */ |
42 | public function __construct(PhpWord $phpWord) |
43 | { |
44 | $pdfLibraryName = Settings::getPdfRendererName(); |
45 | $pdfLibraryPath = Settings::getPdfRendererPath(); |
46 | if (null === $pdfLibraryName || null === $pdfLibraryPath) { |
47 | throw new Exception('PDF rendering library or library path has not been defined.'); |
48 | } |
49 | |
50 | $includePath = str_replace('\\', '/', get_include_path()); |
51 | $rendererPath = str_replace('\\', '/', $pdfLibraryPath); |
52 | if (strpos($rendererPath, $includePath) === false) { |
53 | set_include_path(get_include_path() . PATH_SEPARATOR . $pdfLibraryPath); |
54 | } |
55 | |
56 | $rendererName = static::class . '\\' . $pdfLibraryName; |
57 | $this->renderer = new $rendererName($phpWord); |
58 | } |
59 | |
60 | /** |
61 | * Magic method to handle direct calls to the configured PDF renderer wrapper class. |
62 | * |
63 | * @param string $name Renderer library method name |
64 | * @param mixed[] $arguments Array of arguments to pass to the renderer method |
65 | * |
66 | * @return mixed Returned data from the PDF renderer wrapper method |
67 | */ |
68 | public function __call($name, $arguments) |
69 | { |
70 | // Note: Commented because all exceptions should already be catched by `__construct` |
71 | // if ($this->renderer === null) { |
72 | // throw new Exception("PDF Rendering library has not been defined."); |
73 | // } |
74 | |
75 | return call_user_func_array([$this->getRenderer(), $name], $arguments); |
76 | } |
77 | |
78 | public function save(string $filename): void |
79 | { |
80 | $this->getRenderer()->save($filename); |
81 | } |
82 | |
83 | public function getRenderer(): AbstractRenderer |
84 | { |
85 | return $this->renderer; |
86 | } |
87 | } |