DomPDF.php 4.2 KB

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