9ce830f22aeeb35c42cbf513ec6cffd2545f0aab.svn-base 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 ##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 - 2014 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. $orientation = ($orientation == 'L') ? 'landscape' : 'portrait';
  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. // Override Paper Size
  87. if (!is_null($this->getPaperSize())) {
  88. $printPaperSize = $this->getPaperSize();
  89. }
  90. if (isset(self::$_paperSizes[$printPaperSize])) {
  91. $paperSize = self::$_paperSizes[$printPaperSize];
  92. }
  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. }