Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Writer\Pdf;
   4  
   5  use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
   6  use PhpOffice\PhpSpreadsheet\Writer\Pdf;
   7  
   8  class Dompdf extends Pdf
   9  {
  10      /**
  11       * Gets the implementation of external PDF library that should be used.
  12       *
  13       * @return \Dompdf\Dompdf implementation
  14       */
  15      protected function createExternalWriterInstance()
  16      {
  17          return new \Dompdf\Dompdf();
  18      }
  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  
  31          //  Default PDF paper size
  32          $paperSize = 'LETTER'; //    Letter    (8.5 in. by 11 in.)
  33  
  34          //  Check for paper size and page orientation
  35          if ($this->getSheetIndex() === null) {
  36              $orientation = ($this->spreadsheet->getSheet(0)->getPageSetup()->getOrientation()
  37                  == PageSetup::ORIENTATION_LANDSCAPE) ? 'L' : 'P';
  38              $printPaperSize = $this->spreadsheet->getSheet(0)->getPageSetup()->getPaperSize();
  39          } else {
  40              $orientation = ($this->spreadsheet->getSheet($this->getSheetIndex())->getPageSetup()->getOrientation()
  41                  == PageSetup::ORIENTATION_LANDSCAPE) ? 'L' : 'P';
  42              $printPaperSize = $this->spreadsheet->getSheet($this->getSheetIndex())->getPageSetup()->getPaperSize();
  43          }
  44  
  45          $orientation = ($orientation == 'L') ? 'landscape' : 'portrait';
  46  
  47          //  Override Page Orientation
  48          if ($this->getOrientation() !== null) {
  49              $orientation = ($this->getOrientation() == PageSetup::ORIENTATION_DEFAULT)
  50                  ? PageSetup::ORIENTATION_PORTRAIT
  51                  : $this->getOrientation();
  52          }
  53          //  Override Paper Size
  54          if ($this->getPaperSize() !== null) {
  55              $printPaperSize = $this->getPaperSize();
  56          }
  57  
  58          if (isset(self::$paperSizes[$printPaperSize])) {
  59              $paperSize = self::$paperSizes[$printPaperSize];
  60          }
  61  
  62          //  Create PDF
  63          $pdf = $this->createExternalWriterInstance();
  64          $pdf->setPaper(strtolower($paperSize), $orientation);
  65  
  66          $pdf->loadHtml(
  67              $this->generateHTMLHeader(false) .
  68              $this->generateSheetData() .
  69              $this->generateHTMLFooter()
  70          );
  71          $pdf->render();
  72  
  73          //  Write to file
  74          fwrite($fileHandle, $pdf->output());
  75  
  76          parent::restoreStateAfterSave($fileHandle);
  77      }
  78  }