/templates/movementmeetslife_prod/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Writer/Pdf/Mpdf.php

https://bitbucket.org/davideoz/globalcalendar · PHP · 93 lines · 57 code · 14 blank · 22 comment · 8 complexity · b1f68277c0b525d0c5ee7c717f866964 MD5 · raw file

  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\Writer\Pdf;
  3. use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
  4. use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
  5. use PhpOffice\PhpSpreadsheet\Writer\Pdf;
  6. class Mpdf extends Pdf
  7. {
  8. /**
  9. * Gets the implementation of external PDF library that should be used.
  10. *
  11. * @param array $config Configuration array
  12. *
  13. * @return \Mpdf\Mpdf implementation
  14. */
  15. protected function createExternalWriterInstance($config)
  16. {
  17. return new \Mpdf\Mpdf($config);
  18. }
  19. /**
  20. * Save Spreadsheet to file.
  21. *
  22. * @param string $pFilename Name of the file to save as
  23. *
  24. * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
  25. * @throws PhpSpreadsheetException
  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 (null === $this->getSheetIndex()) {
  34. $orientation = ($this->spreadsheet->getSheet(0)->getPageSetup()->getOrientation()
  35. == PageSetup::ORIENTATION_LANDSCAPE) ? 'L' : 'P';
  36. $printPaperSize = $this->spreadsheet->getSheet(0)->getPageSetup()->getPaperSize();
  37. } else {
  38. $orientation = ($this->spreadsheet->getSheet($this->getSheetIndex())->getPageSetup()->getOrientation()
  39. == PageSetup::ORIENTATION_LANDSCAPE) ? 'L' : 'P';
  40. $printPaperSize = $this->spreadsheet->getSheet($this->getSheetIndex())->getPageSetup()->getPaperSize();
  41. }
  42. $this->setOrientation($orientation);
  43. // Override Page Orientation
  44. if (null !== $this->getOrientation()) {
  45. $orientation = ($this->getOrientation() == PageSetup::ORIENTATION_DEFAULT)
  46. ? PageSetup::ORIENTATION_PORTRAIT
  47. : $this->getOrientation();
  48. }
  49. $orientation = strtoupper($orientation);
  50. // Override Paper Size
  51. if (null !== $this->getPaperSize()) {
  52. $printPaperSize = $this->getPaperSize();
  53. }
  54. if (isset(self::$paperSizes[$printPaperSize])) {
  55. $paperSize = self::$paperSizes[$printPaperSize];
  56. }
  57. // Create PDF
  58. $config = ['tempDir' => $this->tempDir];
  59. $pdf = $this->createExternalWriterInstance($config);
  60. $ortmp = $orientation;
  61. $pdf->_setPageSize(strtoupper($paperSize), $ortmp);
  62. $pdf->DefOrientation = $orientation;
  63. $pdf->AddPage($orientation);
  64. // Document info
  65. $pdf->SetTitle($this->spreadsheet->getProperties()->getTitle());
  66. $pdf->SetAuthor($this->spreadsheet->getProperties()->getCreator());
  67. $pdf->SetSubject($this->spreadsheet->getProperties()->getSubject());
  68. $pdf->SetKeywords($this->spreadsheet->getProperties()->getKeywords());
  69. $pdf->SetCreator($this->spreadsheet->getProperties()->getCreator());
  70. $pdf->WriteHTML(
  71. $this->generateHTMLHeader(false) .
  72. $this->generateSheetData() .
  73. $this->generateHTMLFooter()
  74. );
  75. // Write to file
  76. fwrite($fileHandle, $pdf->Output('', 'S'));
  77. parent::restoreStateAfterSave($fileHandle);
  78. }
  79. }