Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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

   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      public function save($pFilename): void
  26      {
  27          $fileHandle = parent::prepareForSave($pFilename);
  28  
  29          //  Default PDF paper size
  30          $paperSize = 'LETTER'; //    Letter    (8.5 in. by 11 in.)
  31  
  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          } 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  
  43          $orientation = ($orientation == 'L') ? 'landscape' : 'portrait';
  44  
  45          //  Override Page Orientation
  46          if ($this->getOrientation() !== null) {
  47              $orientation = ($this->getOrientation() == PageSetup::ORIENTATION_DEFAULT)
  48                  ? PageSetup::ORIENTATION_PORTRAIT
  49                  : $this->getOrientation();
  50          }
  51          //  Override Paper Size
  52          if ($this->getPaperSize() !== null) {
  53              $printPaperSize = $this->getPaperSize();
  54          }
  55  
  56          if (isset(self::$paperSizes[$printPaperSize])) {
  57              $paperSize = self::$paperSizes[$printPaperSize];
  58          }
  59  
  60          //  Create PDF
  61          $pdf = $this->createExternalWriterInstance();
  62          $pdf->setPaper(strtolower($paperSize), $orientation);
  63  
  64          $pdf->loadHtml($this->generateHTMLAll());
  65          $pdf->render();
  66  
  67          //  Write to file
  68          fwrite($fileHandle, $pdf->output());
  69  
  70          parent::restoreStateAfterSave();
  71      }
  72  }