Tcpdf.php 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\Writer\Pdf;
  3. use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
  4. use PhpOffice\PhpSpreadsheet\Writer\Pdf;
  5. class Tcpdf extends Pdf
  6. {
  7. /**
  8. * Gets the implementation of external PDF library that should be used.
  9. *
  10. * @param string $orientation Page orientation
  11. * @param string $unit Unit measure
  12. * @param string $paperSize Paper size
  13. *
  14. * @return \TCPDF implementation
  15. */
  16. protected function createExternalWriterInstance($orientation, $unit, $paperSize)
  17. {
  18. return new \TCPDF($orientation, $unit, $paperSize);
  19. }
  20. /**
  21. * Save Spreadsheet to file.
  22. *
  23. * @param string $pFilename Name of the file to save as
  24. *
  25. * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
  26. */
  27. public function save($pFilename)
  28. {
  29. $fileHandle = parent::prepareForSave($pFilename);
  30. // Default PDF paper size
  31. $paperSize = 'LETTER'; // Letter (8.5 in. by 11 in.)
  32. // Check for paper size and page orientation
  33. if ($this->getSheetIndex() === null) {
  34. $orientation = ($this->spreadsheet->getSheet(0)->getPageSetup()->getOrientation()
  35. == PageSetup::ORIENTATION_LANDSCAPE) ? 'L' : 'P';
  36. $printPaperSize = $this->spreadsheet->getSheet(0)->getPageSetup()->getPaperSize();
  37. $printMargins = $this->spreadsheet->getSheet(0)->getPageMargins();
  38. } else {
  39. $orientation = ($this->spreadsheet->getSheet($this->getSheetIndex())->getPageSetup()->getOrientation()
  40. == PageSetup::ORIENTATION_LANDSCAPE) ? 'L' : 'P';
  41. $printPaperSize = $this->spreadsheet->getSheet($this->getSheetIndex())->getPageSetup()->getPaperSize();
  42. $printMargins = $this->spreadsheet->getSheet($this->getSheetIndex())->getPageMargins();
  43. }
  44. // Override Page Orientation
  45. if ($this->getOrientation() !== null) {
  46. $orientation = ($this->getOrientation() == PageSetup::ORIENTATION_LANDSCAPE)
  47. ? 'L'
  48. : 'P';
  49. }
  50. // Override Paper Size
  51. if ($this->getPaperSize() !== null) {
  52. $printPaperSize = $this->getPaperSize();
  53. }
  54. if (isset(self::$paperSizes[$printPaperSize])) {
  55. $paperSize = self::$paperSizes[$printPaperSize];
  56. }
  57. // Create PDF
  58. $pdf = $this->createExternalWriterInstance($orientation, 'pt', $paperSize);
  59. $pdf->setFontSubsetting(false);
  60. // Set margins, converting inches to points (using 72 dpi)
  61. $pdf->SetMargins($printMargins->getLeft() * 72, $printMargins->getTop() * 72, $printMargins->getRight() * 72);
  62. $pdf->SetAutoPageBreak(true, $printMargins->getBottom() * 72);
  63. $pdf->setPrintHeader(false);
  64. $pdf->setPrintFooter(false);
  65. $pdf->AddPage();
  66. // Set the appropriate font
  67. $pdf->SetFont($this->getFont());
  68. $pdf->writeHTML(
  69. $this->generateHTMLHeader(false) .
  70. $this->generateSheetData() .
  71. $this->generateHTMLFooter()
  72. );
  73. // Document info
  74. $pdf->SetTitle($this->spreadsheet->getProperties()->getTitle());
  75. $pdf->SetAuthor($this->spreadsheet->getProperties()->getCreator());
  76. $pdf->SetSubject($this->spreadsheet->getProperties()->getSubject());
  77. $pdf->SetKeywords($this->spreadsheet->getProperties()->getKeywords());
  78. $pdf->SetCreator($this->spreadsheet->getProperties()->getCreator());
  79. // Write to file
  80. fwrite($fileHandle, $pdf->output($pFilename, 'S'));
  81. parent::restoreStateAfterSave($fileHandle);
  82. }
  83. }