PDF.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. * @copyright 2010-2018 PHPWord contributors
  15. * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
  16. */
  17. namespace PhpOffice\PhpWord\Writer;
  18. use PhpOffice\PhpWord\Exception\Exception;
  19. use PhpOffice\PhpWord\PhpWord;
  20. use PhpOffice\PhpWord\Settings;
  21. /**
  22. * PDF Writer
  23. *
  24. * @since 0.10.0
  25. */
  26. class PDF
  27. {
  28. /**
  29. * The wrapper for the requested PDF rendering engine
  30. *
  31. * @var \PhpOffice\PhpWord\Writer\PDF\AbstractRenderer
  32. */
  33. private $renderer = null;
  34. /**
  35. * Instantiate a new renderer of the configured type within this container class
  36. *
  37. * @param \PhpOffice\PhpWord\PhpWord $phpWord
  38. *
  39. * @throws \PhpOffice\PhpWord\Exception\Exception
  40. */
  41. public function __construct(PhpWord $phpWord)
  42. {
  43. $pdfLibraryName = Settings::getPdfRendererName();
  44. $pdfLibraryPath = Settings::getPdfRendererPath();
  45. if (is_null($pdfLibraryName) || is_null($pdfLibraryPath)) {
  46. throw new Exception('PDF rendering library or library path has not been defined.');
  47. }
  48. $includePath = str_replace('\\', '/', get_include_path());
  49. $rendererPath = str_replace('\\', '/', $pdfLibraryPath);
  50. if (strpos($rendererPath, $includePath) === false) {
  51. set_include_path(get_include_path() . PATH_SEPARATOR . $pdfLibraryPath);
  52. }
  53. $rendererName = get_class($this) . '\\' . $pdfLibraryName;
  54. $this->renderer = new $rendererName($phpWord);
  55. }
  56. /**
  57. * Magic method to handle direct calls to the configured PDF renderer wrapper class.
  58. *
  59. * @param string $name Renderer library method name
  60. * @param mixed[] $arguments Array of arguments to pass to the renderer method
  61. * @return mixed Returned data from the PDF renderer wrapper method
  62. */
  63. public function __call($name, $arguments)
  64. {
  65. // Note: Commented because all exceptions should already be catched by `__construct`
  66. // if ($this->renderer === null) {
  67. // throw new Exception("PDF Rendering library has not been defined.");
  68. // }
  69. return call_user_func_array(array($this->renderer, $name), $arguments);
  70. }
  71. }