MPDF.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 PhpOffice\PhpWord\PhpWord;
  19. use PhpOffice\PhpWord\Settings;
  20. use PhpOffice\PhpWord\Writer\WriterInterface;
  21. /**
  22. * MPDF writer
  23. *
  24. * @see http://www.mpdf1.com/
  25. * @since 0.11.0
  26. */
  27. class MPDF extends AbstractRenderer implements WriterInterface
  28. {
  29. /**
  30. * Overridden to set the correct includefile, only needed for MPDF 5
  31. *
  32. * @codeCoverageIgnore
  33. * @param PhpWord $phpWord
  34. */
  35. public function __construct(PhpWord $phpWord)
  36. {
  37. if (file_exists(Settings::getPdfRendererPath() . '/mpdf.php')) {
  38. // MPDF version 5.* needs this file to be included, later versions not
  39. $this->includeFile = 'mpdf.php';
  40. }
  41. parent::__construct($phpWord);
  42. }
  43. /**
  44. * Gets the implementation of external PDF library that should be used.
  45. *
  46. * @return Mpdf implementation
  47. */
  48. protected function createExternalWriterInstance()
  49. {
  50. $mPdfClass = $this->getMPdfClassName();
  51. return new $mPdfClass();
  52. }
  53. /**
  54. * Save PhpWord to file.
  55. *
  56. * @param string $filename Name of the file to save as
  57. */
  58. public function save($filename = null)
  59. {
  60. $fileHandle = parent::prepareForSave($filename);
  61. // PDF settings
  62. $paperSize = strtoupper('A4');
  63. $orientation = strtoupper('portrait');
  64. // Create PDF
  65. $pdf = $this->createExternalWriterInstance();
  66. $pdf->_setPageSize($paperSize, $orientation);
  67. $pdf->addPage($orientation);
  68. // Write document properties
  69. $phpWord = $this->getPhpWord();
  70. $docProps = $phpWord->getDocInfo();
  71. $pdf->setTitle($docProps->getTitle());
  72. $pdf->setAuthor($docProps->getCreator());
  73. $pdf->setSubject($docProps->getSubject());
  74. $pdf->setKeywords($docProps->getKeywords());
  75. $pdf->setCreator($docProps->getCreator());
  76. $pdf->writeHTML($this->getContent());
  77. // Write to file
  78. fwrite($fileHandle, $pdf->output($filename, 'S'));
  79. parent::restoreStateAfterSave($fileHandle);
  80. }
  81. /**
  82. * Return classname of MPDF to instantiate
  83. *
  84. * @codeCoverageIgnore
  85. * @return string
  86. */
  87. private function getMPdfClassName()
  88. {
  89. if ($this->includeFile != null) {
  90. // MPDF version 5.*
  91. return '\mpdf';
  92. }
  93. // MPDF version > 6.*
  94. return '\Mpdf\Mpdf';
  95. }
  96. }