tcPDF.php 4.8 KB

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