tcPDF.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 1.8.0, 2014-03-02
  26. */
  27. /** Require tcPDF library */
  28. $pdfRendererClassFile = PHPExcel_Settings::getPdfRendererPath() . '/tcpdf.php';
  29. if (file_exists($pdfRendererClassFile)) {
  30. $k_path_url = PHPExcel_Settings::getPdfRendererPath();
  31. require_once $pdfRendererClassFile;
  32. } else {
  33. throw new PHPExcel_Writer_Exception('Unable to load PDF Rendering library');
  34. }
  35. /**
  36. * PHPExcel_Writer_PDF_tcPDF
  37. *
  38. * @category PHPExcel
  39. * @package PHPExcel_Writer_PDF
  40. * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  41. */
  42. class PHPExcel_Writer_PDF_tcPDF extends PHPExcel_Writer_PDF_Core implements PHPExcel_Writer_IWriter
  43. {
  44. /**
  45. * Create a new PHPExcel_Writer_PDF
  46. *
  47. * @param PHPExcel $phpExcel PHPExcel object
  48. */
  49. public function __construct(PHPExcel $phpExcel)
  50. {
  51. parent::__construct($phpExcel);
  52. }
  53. /**
  54. * Save PHPExcel to file
  55. *
  56. * @param string $pFilename Name of the file to save as
  57. * @throws PHPExcel_Writer_Exception
  58. */
  59. public function save($pFilename = NULL)
  60. {
  61. $fileHandle = parent::prepareForSave($pFilename);
  62. // Default PDF paper size
  63. $paperSize = 'LETTER'; // Letter (8.5 in. by 11 in.)
  64. // Check for paper size and page orientation
  65. if (is_null($this->getSheetIndex())) {
  66. $orientation = ($this->_phpExcel->getSheet(0)->getPageSetup()->getOrientation()
  67. == PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE)
  68. ? 'L'
  69. : 'P';
  70. $printPaperSize = $this->_phpExcel->getSheet(0)->getPageSetup()->getPaperSize();
  71. $printMargins = $this->_phpExcel->getSheet(0)->getPageMargins();
  72. } else {
  73. $orientation = ($this->_phpExcel->getSheet($this->getSheetIndex())->getPageSetup()->getOrientation()
  74. == PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE)
  75. ? 'L'
  76. : 'P';
  77. $printPaperSize = $this->_phpExcel->getSheet($this->getSheetIndex())->getPageSetup()->getPaperSize();
  78. $printMargins = $this->_phpExcel->getSheet($this->getSheetIndex())->getPageMargins();
  79. }
  80. // Override Page Orientation
  81. if (!is_null($this->getOrientation())) {
  82. $orientation = ($this->getOrientation() == PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE)
  83. ? 'L'
  84. : 'P';
  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 TCPDF($orientation, 'pt', $paperSize);
  95. $pdf->setFontSubsetting(FALSE);
  96. // Set margins, converting inches to points (using 72 dpi)
  97. $pdf->SetMargins($printMargins->getLeft() * 72, $printMargins->getTop() * 72, $printMargins->getRight() * 72);
  98. $pdf->SetAutoPageBreak(TRUE, $printMargins->getBottom() * 72);
  99. $pdf->setPrintHeader(FALSE);
  100. $pdf->setPrintFooter(FALSE);
  101. $pdf->AddPage();
  102. // Set the appropriate font
  103. $pdf->SetFont($this->getFont());
  104. $pdf->writeHTML(
  105. $this->generateHTMLHeader(FALSE) .
  106. $this->generateSheetData() .
  107. $this->generateHTMLFooter()
  108. );
  109. // Document info
  110. $pdf->SetTitle($this->_phpExcel->getProperties()->getTitle());
  111. $pdf->SetAuthor($this->_phpExcel->getProperties()->getCreator());
  112. $pdf->SetSubject($this->_phpExcel->getProperties()->getSubject());
  113. $pdf->SetKeywords($this->_phpExcel->getProperties()->getKeywords());
  114. $pdf->SetCreator($this->_phpExcel->getProperties()->getCreator());
  115. // Write to file
  116. fwrite($fileHandle, $pdf->output($pFilename, 'S'));
  117. parent::restoreStateAfterSave($fileHandle);
  118. }
  119. }