/PHPExcel/Writer/PDF/DomPDF.php

https://bitbucket.org/nfredricks/wp-employee-time · PHP · 128 lines · 55 code · 18 blank · 55 comment · 13 complexity · 02241f0c76f1a77c52224c9be60674ba MD5 · raw file

  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
  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 1.7.8, 2012-10-12
  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 Exception('Unable to load PDF Rendering library');
  33. }
  34. /**
  35. * PHPExcel_Writer_PDF_DomPDF
  36. *
  37. * @category PHPExcel
  38. * @package PHPExcel_Writer
  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. * Create a new PHPExcel_Writer_PDF
  44. *
  45. * @param PHPExcel $phpExcel PHPExcel object
  46. */
  47. public function __construct(PHPExcel $phpExcel) {
  48. parent::__construct($phpExcel);
  49. }
  50. /**
  51. * Save PHPExcel to file
  52. *
  53. * @param string $pFileName
  54. * @throws Exception
  55. */
  56. public function save($pFilename = null) {
  57. // garbage collect
  58. $this->_phpExcel->garbageCollect();
  59. $saveArrayReturnType = PHPExcel_Calculation::getArrayReturnType();
  60. PHPExcel_Calculation::setArrayReturnType(PHPExcel_Calculation::RETURN_ARRAY_AS_VALUE);
  61. // Open file
  62. $fileHandle = fopen($pFilename, 'w');
  63. if ($fileHandle === false) {
  64. throw new Exception("Could not open file $pFilename for writing.");
  65. }
  66. // Set PDF
  67. $this->_isPdf = true;
  68. // Build CSS
  69. $this->buildCSS(true);
  70. // Default PDF paper size
  71. $paperSize = 'LETTER'; // Letter (8.5 in. by 11 in.)
  72. // Check for paper size and page orientation
  73. if (is_null($this->getSheetIndex())) {
  74. $orientation = ($this->_phpExcel->getSheet(0)->getPageSetup()->getOrientation() == PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE) ? 'L' : 'P';
  75. $printPaperSize = $this->_phpExcel->getSheet(0)->getPageSetup()->getPaperSize();
  76. $printMargins = $this->_phpExcel->getSheet(0)->getPageMargins();
  77. } else {
  78. $orientation = ($this->_phpExcel->getSheet($this->getSheetIndex())->getPageSetup()->getOrientation() == PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE) ? 'L' : 'P';
  79. $printPaperSize = $this->_phpExcel->getSheet($this->getSheetIndex())->getPageSetup()->getPaperSize();
  80. $printMargins = $this->_phpExcel->getSheet($this->getSheetIndex())->getPageMargins();
  81. }
  82. // Override Page Orientation
  83. if (!is_null($this->getOrientation())) {
  84. $orientation = ($this->getOrientation() == PHPExcel_Worksheet_PageSetup::ORIENTATION_DEFAULT) ?
  85. PHPExcel_Worksheet_PageSetup::ORIENTATION_PORTRAIT : $this->getOrientation();
  86. }
  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. $orientation = ($orientation == 'L') ? 'landscape' : 'portrait';
  95. // Create PDF
  96. $pdf = new DOMPDF();
  97. $pdf->set_paper(strtolower($paperSize), $orientation);
  98. $pdf->load_html(
  99. $this->generateHTMLHeader(false) .
  100. $this->generateSheetData() .
  101. $this->generateHTMLFooter()
  102. );
  103. $pdf->render();
  104. // Write to file
  105. fwrite($fileHandle, $pdf->output());
  106. // Close file
  107. fclose($fileHandle);
  108. PHPExcel_Calculation::setArrayReturnType($saveArrayReturnType);
  109. }
  110. }