DomPDF.php 4.0 KB

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