tcPDF.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. $pdfRendererClassFile = PHPExcel_Settings::getPdfRendererPath() . '/tcpdf.php';
  8. if (file_exists($pdfRendererClassFile)) {
  9. $k_path_url = PHPExcel_Settings::getPdfRendererPath();
  10. require_once $pdfRendererClassFile;
  11. } else {
  12. throw new PHPExcel_Writer_Exception('Unable to load PDF Rendering library');
  13. }
  14. /**
  15. * PHPExcel_Writer_PDF_tcPDF
  16. *
  17. * Copyright (c) 2006 - 2015 PHPExcel
  18. *
  19. * This library is free software; you can redistribute it and/or
  20. * modify it under the terms of the GNU Lesser General Public
  21. * License as published by the Free Software Foundation; either
  22. * version 2.1 of the License, or (at your option) any later version.
  23. *
  24. * This library is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  27. * Lesser General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU Lesser General Public
  30. * License along with this library; if not, write to the Free Software
  31. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  32. *
  33. * @category PHPExcel
  34. * @package PHPExcel_Writer_PDF
  35. * @copyright Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
  36. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  37. * @version ##VERSION##, ##DATE##
  38. */
  39. class PHPExcel_Writer_PDF_tcPDF extends PHPExcel_Writer_PDF_Core implements PHPExcel_Writer_IWriter
  40. {
  41. /**
  42. * Create a new PHPExcel_Writer_PDF
  43. *
  44. * @param PHPExcel $phpExcel PHPExcel object
  45. */
  46. public function __construct(PHPExcel $phpExcel)
  47. {
  48. parent::__construct($phpExcel);
  49. }
  50. /**
  51. * Save PHPExcel to file
  52. *
  53. * @param string $pFilename Name of the file to save as
  54. * @throws PHPExcel_Writer_Exception
  55. */
  56. public function save($pFilename = null)
  57. {
  58. $fileHandle = parent::prepareForSave($pFilename);
  59. // Default PDF paper size
  60. $paperSize = 'LETTER'; // Letter (8.5 in. by 11 in.)
  61. // Check for paper size and page orientation
  62. if (is_null($this->getSheetIndex())) {
  63. $orientation = ($this->phpExcel->getSheet(0)->getPageSetup()->getOrientation()
  64. == PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE) ? 'L' : 'P';
  65. $printPaperSize = $this->phpExcel->getSheet(0)->getPageSetup()->getPaperSize();
  66. $printMargins = $this->phpExcel->getSheet(0)->getPageMargins();
  67. } else {
  68. $orientation = ($this->phpExcel->getSheet($this->getSheetIndex())->getPageSetup()->getOrientation()
  69. == PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE) ? 'L' : 'P';
  70. $printPaperSize = $this->phpExcel->getSheet($this->getSheetIndex())->getPageSetup()->getPaperSize();
  71. $printMargins = $this->phpExcel->getSheet($this->getSheetIndex())->getPageMargins();
  72. }
  73. // Override Page Orientation
  74. if (!is_null($this->getOrientation())) {
  75. $orientation = ($this->getOrientation() == PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE)
  76. ? 'L'
  77. : 'P';
  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 TCPDF($orientation, 'pt', $paperSize);
  88. $pdf->setFontSubsetting(false);
  89. // Set margins, converting inches to points (using 72 dpi)
  90. $pdf->SetMargins($printMargins->getLeft() * 72, $printMargins->getTop() * 72, $printMargins->getRight() * 72);
  91. $pdf->SetAutoPageBreak(true, $printMargins->getBottom() * 72);
  92. $pdf->setPrintHeader(false);
  93. $pdf->setPrintFooter(false);
  94. $pdf->AddPage();
  95. // Set the appropriate font
  96. $pdf->SetFont($this->getFont());
  97. $pdf->writeHTML(
  98. $this->generateHTMLHeader(false) .
  99. $this->generateSheetData() .
  100. $this->generateHTMLFooter()
  101. );
  102. // Document info
  103. $pdf->SetTitle($this->phpExcel->getProperties()->getTitle());
  104. $pdf->SetAuthor($this->phpExcel->getProperties()->getCreator());
  105. $pdf->SetSubject($this->phpExcel->getProperties()->getSubject());
  106. $pdf->SetKeywords($this->phpExcel->getProperties()->getKeywords());
  107. $pdf->SetCreator($this->phpExcel->getProperties()->getCreator());
  108. // Write to file
  109. fwrite($fileHandle, $pdf->output($pFilename, 'S'));
  110. parent::restoreStateAfterSave($fileHandle);
  111. }
  112. }