mPDF.php 4.7 KB

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