DomPDF.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2013 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 - 2013 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.9, 2013-06-02
  26. */
  27. /** Require DomPDF library */
  28. $pdfRendererClassFile = PHPExcel_Settings::getPdfRendererPath() . '/dompdf_config.inc.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_DomPDF
  36. *
  37. * @category PHPExcel
  38. * @package PHPExcel_Writer_PDF
  39. * @copyright Copyright (c) 2006 - 2013 PHPExcel (http://www.codeplex.com/PHPExcel)
  40. */
  41. class PHPExcel_Writer_PDF_DomPDF 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. // Override Page Orientation
  80. if (!is_null($this->getOrientation())) {
  81. $orientation = ($this->getOrientation() == PHPExcel_Worksheet_PageSetup::ORIENTATION_DEFAULT)
  82. ? PHPExcel_Worksheet_PageSetup::ORIENTATION_PORTRAIT
  83. : $this->getOrientation();
  84. }
  85. // Override Paper Size
  86. if (!is_null($this->getPaperSize())) {
  87. $printPaperSize = $this->getPaperSize();
  88. }
  89. if (isset(self::$_paperSizes[$printPaperSize])) {
  90. $paperSize = self::$_paperSizes[$printPaperSize];
  91. }
  92. $orientation = ($orientation == 'L') ? 'landscape' : 'portrait';
  93. // Create PDF
  94. $pdf = new DOMPDF();
  95. $pdf->set_paper(strtolower($paperSize), $orientation);
  96. $pdf->load_html(
  97. $this->generateHTMLHeader(FALSE) .
  98. $this->generateSheetData() .
  99. $this->generateHTMLFooter()
  100. );
  101. $pdf->render();
  102. // Write to file
  103. fwrite($fileHandle, $pdf->output());
  104. parent::restoreStateAfterSave($fileHandle);
  105. }
  106. }