Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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

   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       * embed images, or link to images.
  12       *
  13       * @var bool
  14       */
  15      protected $embedImages = true;
  16  
  17      /**
  18       * Gets the implementation of external PDF library that should be used.
  19       *
  20       * @return \Dompdf\Dompdf implementation
  21       */
  22      protected function createExternalWriterInstance()
  23      {
  24          return new \Dompdf\Dompdf();
  25      }
  26  
  27      /**
  28       * Save Spreadsheet to file.
  29       *
  30       * @param string $filename Name of the file to save as
  31       */
  32      public function save($filename, int $flags = 0): void
  33      {
  34          $fileHandle = parent::prepareForSave($filename);
  35  
  36          //  Check for paper size and page orientation
  37          $setup = $this->spreadsheet->getSheet($this->getSheetIndex() ?? 0)->getPageSetup();
  38          $orientation = $this->getOrientation() ?? $setup->getOrientation();
  39          $orientation = ($orientation === PageSetup::ORIENTATION_LANDSCAPE) ? 'L' : 'P';
  40          $printPaperSize = $this->getPaperSize() ?? $setup->getPaperSize();
  41          $paperSize = self::$paperSizes[$printPaperSize] ?? PageSetup::getPaperSizeDefault();
  42          if (is_array($paperSize) && count($paperSize) === 2) {
  43              $paperSize = [0.0, 0.0, $paperSize[0], $paperSize[1]];
  44          }
  45  
  46          $orientation = ($orientation == 'L') ? 'landscape' : 'portrait';
  47  
  48          //  Create PDF
  49          $pdf = $this->createExternalWriterInstance();
  50          $pdf->setPaper($paperSize, $orientation);
  51  
  52          $pdf->loadHtml($this->generateHTMLAll());
  53          $pdf->render();
  54  
  55          //  Write to file
  56          fwrite($fileHandle, $pdf->output() ?? '');
  57  
  58          parent::restoreStateAfterSave();
  59      }
  60  }