Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

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

   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Writer\Pdf;
   4  
   5  use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
   6  use PhpOffice\PhpSpreadsheet\Writer\Html;
   7  use PhpOffice\PhpSpreadsheet\Writer\Pdf;
   8  
   9  class Mpdf extends Pdf
  10  {
  11      /**
  12       * Gets the implementation of external PDF library that should be used.
  13       *
  14       * @param array $config Configuration array
  15       *
  16       * @return \Mpdf\Mpdf implementation
  17       */
  18      protected function createExternalWriterInstance($config)
  19      {
  20          return new \Mpdf\Mpdf($config);
  21      }
  22  
  23      /**
  24       * Save Spreadsheet to file.
  25       *
  26       * @param string $filename Name of the file to save as
  27       */
  28      public function save($filename, int $flags = 0): void
  29      {
  30          $fileHandle = parent::prepareForSave($filename);
  31  
  32          //  Check for paper size and page orientation
  33          $setup = $this->spreadsheet->getSheet($this->getSheetIndex() ?? 0)->getPageSetup();
  34          $orientation = $this->getOrientation() ?? $setup->getOrientation();
  35          $orientation = ($orientation === PageSetup::ORIENTATION_LANDSCAPE) ? 'L' : 'P';
  36          $printPaperSize = $this->getPaperSize() ?? $setup->getPaperSize();
  37          $paperSize = self::$paperSizes[$printPaperSize] ?? PageSetup::getPaperSizeDefault();
  38  
  39          //  Create PDF
  40          $config = ['tempDir' => $this->tempDir . '/mpdf'];
  41          $pdf = $this->createExternalWriterInstance($config);
  42          $ortmp = $orientation;
  43          $pdf->_setPageSize($paperSize, $ortmp);
  44          $pdf->DefOrientation = $orientation;
  45          $pdf->AddPageByArray([
  46              'orientation' => $orientation,
  47              'margin-left' => $this->inchesToMm($this->spreadsheet->getActiveSheet()->getPageMargins()->getLeft()),
  48              'margin-right' => $this->inchesToMm($this->spreadsheet->getActiveSheet()->getPageMargins()->getRight()),
  49              'margin-top' => $this->inchesToMm($this->spreadsheet->getActiveSheet()->getPageMargins()->getTop()),
  50              'margin-bottom' => $this->inchesToMm($this->spreadsheet->getActiveSheet()->getPageMargins()->getBottom()),
  51          ]);
  52  
  53          //  Document info
  54          $pdf->SetTitle($this->spreadsheet->getProperties()->getTitle());
  55          $pdf->SetAuthor($this->spreadsheet->getProperties()->getCreator());
  56          $pdf->SetSubject($this->spreadsheet->getProperties()->getSubject());
  57          $pdf->SetKeywords($this->spreadsheet->getProperties()->getKeywords());
  58          $pdf->SetCreator($this->spreadsheet->getProperties()->getCreator());
  59  
  60          $html = $this->generateHTMLAll();
  61          $bodyLocation = strpos($html, Html::BODY_LINE);
  62          // Make sure first data presented to Mpdf includes body tag
  63          //   so that Mpdf doesn't parse it as content. Issue 2432.
  64          if ($bodyLocation !== false) {
  65              $bodyLocation += strlen(Html::BODY_LINE);
  66              $pdf->WriteHTML(substr($html, 0, $bodyLocation));
  67              $html = substr($html, $bodyLocation);
  68          }
  69          foreach (\array_chunk(\explode(PHP_EOL, $html), 1000) as $lines) {
  70              $pdf->WriteHTML(\implode(PHP_EOL, $lines));
  71          }
  72  
  73          //  Write to file
  74          fwrite($fileHandle, $pdf->Output('', 'S'));
  75  
  76          parent::restoreStateAfterSave();
  77      }
  78  
  79      /**
  80       * Convert inches to mm.
  81       *
  82       * @param float $inches
  83       *
  84       * @return float
  85       */
  86      private function inchesToMm($inches)
  87      {
  88          return $inches * 25.4;
  89      }
  90  }