mPDF.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /** Require mPDF library */
  3. $pdfRendererClassFile = PHPExcel_Settings::getPdfRendererPath() . '/mpdf.php';
  4. if (file_exists($pdfRendererClassFile)) {
  5. require_once $pdfRendererClassFile;
  6. } else {
  7. throw new PHPExcel_Writer_Exception('Unable to load PDF Rendering library');
  8. }
  9. /**
  10. * PHPExcel_Writer_PDF_mPDF
  11. *
  12. * Copyright (c) 2006 - 2015 PHPExcel
  13. *
  14. * This library is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU Lesser General Public
  16. * License as published by the Free Software Foundation; either
  17. * version 2.1 of the License, or (at your option) any later version.
  18. *
  19. * This library is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  22. * Lesser General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Lesser General Public
  25. * License along with this library; if not, write to the Free Software
  26. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  27. *
  28. * @category PHPExcel
  29. * @package PHPExcel_Writer_PDF
  30. * @copyright Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
  31. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  32. * @version ##VERSION##, ##DATE##
  33. */
  34. class PHPExcel_Writer_PDF_mPDF extends PHPExcel_Writer_PDF_Core implements PHPExcel_Writer_IWriter
  35. {
  36. /**
  37. * Create a new PHPExcel_Writer_PDF
  38. *
  39. * @param PHPExcel $phpExcel PHPExcel object
  40. */
  41. public function __construct(PHPExcel $phpExcel)
  42. {
  43. parent::__construct($phpExcel);
  44. }
  45. /**
  46. * Save PHPExcel to file
  47. *
  48. * @param string $pFilename Name of the file to save as
  49. * @throws PHPExcel_Writer_Exception
  50. */
  51. public function save($pFilename = null)
  52. {
  53. $fileHandle = parent::prepareForSave($pFilename);
  54. // Default PDF paper size
  55. $paperSize = 'LETTER'; // Letter (8.5 in. by 11 in.)
  56. // Check for paper size and page orientation
  57. if (is_null($this->getSheetIndex())) {
  58. $orientation = ($this->phpExcel->getSheet(0)->getPageSetup()->getOrientation()
  59. == PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE) ? 'L' : 'P';
  60. $printPaperSize = $this->phpExcel->getSheet(0)->getPageSetup()->getPaperSize();
  61. $printMargins = $this->phpExcel->getSheet(0)->getPageMargins();
  62. } else {
  63. $orientation = ($this->phpExcel->getSheet($this->getSheetIndex())->getPageSetup()->getOrientation()
  64. == PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE) ? 'L' : 'P';
  65. $printPaperSize = $this->phpExcel->getSheet($this->getSheetIndex())->getPageSetup()->getPaperSize();
  66. $printMargins = $this->phpExcel->getSheet($this->getSheetIndex())->getPageMargins();
  67. }
  68. $this->setOrientation($orientation);
  69. // Override Page Orientation
  70. if (!is_null($this->getOrientation())) {
  71. $orientation = ($this->getOrientation() == PHPExcel_Worksheet_PageSetup::ORIENTATION_DEFAULT)
  72. ? PHPExcel_Worksheet_PageSetup::ORIENTATION_PORTRAIT
  73. : $this->getOrientation();
  74. }
  75. $orientation = strtoupper($orientation);
  76. // Override Paper Size
  77. if (!is_null($this->getPaperSize())) {
  78. $printPaperSize = $this->getPaperSize();
  79. }
  80. if (isset(self::$paperSizes[$printPaperSize])) {
  81. $paperSize = self::$paperSizes[$printPaperSize];
  82. }
  83. // Create PDF
  84. $pdf = new mpdf();
  85. $ortmp = $orientation;
  86. $pdf->_setPageSize(strtoupper($paperSize), $ortmp);
  87. $pdf->DefOrientation = $orientation;
  88. $pdf->AddPage($orientation);
  89. // Document info
  90. $pdf->SetTitle($this->phpExcel->getProperties()->getTitle());
  91. $pdf->SetAuthor($this->phpExcel->getProperties()->getCreator());
  92. $pdf->SetSubject($this->phpExcel->getProperties()->getSubject());
  93. $pdf->SetKeywords($this->phpExcel->getProperties()->getKeywords());
  94. $pdf->SetCreator($this->phpExcel->getProperties()->getCreator());
  95. $pdf->WriteHTML(
  96. $this->generateHTMLHeader(false) .
  97. $this->generateSheetData() .
  98. $this->generateHTMLFooter()
  99. );
  100. // Write to file
  101. fwrite($fileHandle, $pdf->Output('', 'S'));
  102. parent::restoreStateAfterSave($fileHandle);
  103. }
  104. }