MPDF.php 2.0 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. * @link https://github.com/PHPOffice/PhpWord
  14. * @copyright 2010-2014 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\Writer\WriterInterface;
  19. /**
  20. * MPDF writer
  21. *
  22. * @link http://www.mpdf1.com/
  23. * @since 0.11.0
  24. */
  25. class MPDF extends AbstractRenderer implements WriterInterface
  26. {
  27. /**
  28. * Name of renderer include file
  29. *
  30. * @var string
  31. */
  32. protected $includeFile = 'mpdf.php';
  33. /**
  34. * Save PhpWord to file.
  35. *
  36. * @param string $filename Name of the file to save as
  37. * @return void
  38. */
  39. public function save($filename = null)
  40. {
  41. $fileHandle = parent::prepareForSave($filename);
  42. // PDF settings
  43. $paperSize = strtoupper('A4');
  44. $orientation = strtoupper('portrait');
  45. // Create PDF
  46. $pdf = new \mpdf();
  47. $pdf->_setPageSize($paperSize, $orientation);
  48. $pdf->addPage($orientation);
  49. // Write document properties
  50. $phpWord = $this->getPhpWord();
  51. $docProps = $phpWord->getDocInfo();
  52. $pdf->setTitle($docProps->getTitle());
  53. $pdf->setAuthor($docProps->getCreator());
  54. $pdf->setSubject($docProps->getSubject());
  55. $pdf->setKeywords($docProps->getKeywords());
  56. $pdf->setCreator($docProps->getCreator());
  57. $pdf->writeHTML($this->getContent());
  58. // Write to file
  59. fwrite($fileHandle, $pdf->output($filename, 'S'));
  60. parent::restoreStateAfterSave($fileHandle);
  61. }
  62. }