DomPDF.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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\PDF;
  18. use Dompdf\Dompdf as DompdfLib;
  19. use PhpOffice\PhpWord\Writer\WriterInterface;
  20. /**
  21. * DomPDF writer
  22. *
  23. * @see https://github.com/dompdf/dompdf
  24. * @since 0.10.0
  25. */
  26. class DomPDF extends AbstractRenderer implements WriterInterface
  27. {
  28. /**
  29. * Name of renderer include file
  30. *
  31. * @var string
  32. */
  33. protected $includeFile = null;
  34. /**
  35. * Gets the implementation of external PDF library that should be used.
  36. *
  37. * @return Dompdf implementation
  38. */
  39. protected function createExternalWriterInstance()
  40. {
  41. return new DompdfLib();
  42. }
  43. /**
  44. * Save PhpWord to file.
  45. *
  46. * @param string $filename Name of the file to save as
  47. */
  48. public function save($filename = null)
  49. {
  50. $fileHandle = parent::prepareForSave($filename);
  51. // PDF settings
  52. $paperSize = 'A4';
  53. $orientation = 'portrait';
  54. // Create PDF
  55. $pdf = $this->createExternalWriterInstance();
  56. $pdf->setPaper(strtolower($paperSize), $orientation);
  57. $pdf->loadHtml(str_replace(PHP_EOL, '', $this->getContent()));
  58. $pdf->render();
  59. // Write to file
  60. fwrite($fileHandle, $pdf->output());
  61. parent::restoreStateAfterSave($fileHandle);
  62. }
  63. }