Mpdf.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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->AddPageByArray([
  64. 'orientation' => $orientation,
  65. 'margin-left' => $this->inchesToMm($this->spreadsheet->getActiveSheet()->getPageMargins()->getLeft()),
  66. 'margin-right' => $this->inchesToMm($this->spreadsheet->getActiveSheet()->getPageMargins()->getRight()),
  67. 'margin-top' => $this->inchesToMm($this->spreadsheet->getActiveSheet()->getPageMargins()->getTop()),
  68. 'margin-bottom' => $this->inchesToMm($this->spreadsheet->getActiveSheet()->getPageMargins()->getBottom()),
  69. ]);
  70. // Document info
  71. $pdf->SetTitle($this->spreadsheet->getProperties()->getTitle());
  72. $pdf->SetAuthor($this->spreadsheet->getProperties()->getCreator());
  73. $pdf->SetSubject($this->spreadsheet->getProperties()->getSubject());
  74. $pdf->SetKeywords($this->spreadsheet->getProperties()->getKeywords());
  75. $pdf->SetCreator($this->spreadsheet->getProperties()->getCreator());
  76. $pdf->WriteHTML($this->generateHTMLHeader(false));
  77. $html = $this->generateSheetData();
  78. foreach (\array_chunk(\explode(PHP_EOL, $html), 1000) as $lines) {
  79. $pdf->WriteHTML(\implode(PHP_EOL, $lines));
  80. }
  81. $pdf->WriteHTML($this->generateHTMLFooter());
  82. // Write to file
  83. fwrite($fileHandle, $pdf->Output('', 'S'));
  84. parent::restoreStateAfterSave($fileHandle);
  85. }
  86. /**
  87. * Convert inches to mm.
  88. *
  89. * @param float $inches
  90. *
  91. * @return float
  92. */
  93. private function inchesToMm($inches)
  94. {
  95. return $inches * 25.4;
  96. }
  97. }