mPDF.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. $pdfRendererClassFile = PHPExcel_Settings::getPdfRendererPath() . '/mpdf.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_mPDF
  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_mPDF 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. $this->setOrientation($orientation);
  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. $orientation = strtoupper($orientation);
  80. // Override Paper Size
  81. if (!is_null($this->getPaperSize())) {
  82. $printPaperSize = $this->getPaperSize();
  83. }
  84. if (isset(self::$paperSizes[$printPaperSize])) {
  85. $paperSize = self::$paperSizes[$printPaperSize];
  86. }
  87. // Create PDF
  88. $pdf = new mpdf();
  89. $ortmp = $orientation;
  90. $pdf->_setPageSize(strtoupper($paperSize), $ortmp);
  91. $pdf->DefOrientation = $orientation;
  92. $pdf->AddPage($orientation);
  93. // Document info
  94. $pdf->SetTitle($this->phpExcel->getProperties()->getTitle());
  95. $pdf->SetAuthor($this->phpExcel->getProperties()->getCreator());
  96. $pdf->SetSubject($this->phpExcel->getProperties()->getSubject());
  97. $pdf->SetKeywords($this->phpExcel->getProperties()->getKeywords());
  98. $pdf->SetCreator($this->phpExcel->getProperties()->getCreator());
  99. $pdf->WriteHTML(
  100. $this->generateHTMLHeader(false) .
  101. $this->generateSheetData() .
  102. $this->generateHTMLFooter()
  103. );
  104. // Write to file
  105. fwrite($fileHandle, $pdf->Output('', 'S'));
  106. parent::restoreStateAfterSave($fileHandle);
  107. }
  108. }