PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Classes/PHPExcel/Writer/PDF/DomPDF.php

https://github.com/iGroup/PHPExcel
PHP | 120 lines | 56 code | 14 blank | 50 comment | 11 complexity | 8e7018beafedf051a919df1310c4e288 MD5 | raw file
Possible License(s): LGPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2012 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 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version ##VERSION##, ##DATE##
  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 - 2012 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
  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. }