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