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 Tcpdf extends Pdf
   9  {
  10      /**
  11       * Gets the implementation of external PDF library that should be used.
  12       *
  13       * @param string $orientation Page orientation
  14       * @param string $unit Unit measure
  15       * @param string $paperSize Paper size
  16       *
  17       * @return \TCPDF implementation
  18       */
  19      protected function createExternalWriterInstance($orientation, $unit, $paperSize)
  20      {
  21          return new \TCPDF($orientation, $unit, $paperSize);
  22      }
  23  
  24      /**
  25       * Save Spreadsheet to file.
  26       *
  27       * @param string $pFilename Name of the file to save as
  28       *
  29       * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
  30       */
  31      public function save($pFilename)
  32      {
  33          $fileHandle = parent::prepareForSave($pFilename);
  34  
  35          //  Default PDF paper size
  36          $paperSize = 'LETTER'; //    Letter    (8.5 in. by 11 in.)
  37  
  38          //  Check for paper size and page orientation
  39          if ($this->getSheetIndex() === null) {
  40              $orientation = ($this->spreadsheet->getSheet(0)->getPageSetup()->getOrientation()
  41                  == PageSetup::ORIENTATION_LANDSCAPE) ? 'L' : 'P';
  42              $printPaperSize = $this->spreadsheet->getSheet(0)->getPageSetup()->getPaperSize();
  43              $printMargins = $this->spreadsheet->getSheet(0)->getPageMargins();
  44          } else {
  45              $orientation = ($this->spreadsheet->getSheet($this->getSheetIndex())->getPageSetup()->getOrientation()
  46                  == PageSetup::ORIENTATION_LANDSCAPE) ? 'L' : 'P';
  47              $printPaperSize = $this->spreadsheet->getSheet($this->getSheetIndex())->getPageSetup()->getPaperSize();
  48              $printMargins = $this->spreadsheet->getSheet($this->getSheetIndex())->getPageMargins();
  49          }
  50  
  51          //  Override Page Orientation
  52          if ($this->getOrientation() !== null) {
  53              $orientation = ($this->getOrientation() == PageSetup::ORIENTATION_LANDSCAPE)
  54                  ? 'L'
  55                  : 'P';
  56          }
  57          //  Override Paper Size
  58          if ($this->getPaperSize() !== null) {
  59              $printPaperSize = $this->getPaperSize();
  60          }
  61  
  62          if (isset(self::$paperSizes[$printPaperSize])) {
  63              $paperSize = self::$paperSizes[$printPaperSize];
  64          }
  65  
  66          //  Create PDF
  67          $pdf = $this->createExternalWriterInstance($orientation, 'pt', $paperSize);
  68          $pdf->setFontSubsetting(false);
  69          //    Set margins, converting inches to points (using 72 dpi)
  70          $pdf->SetMargins($printMargins->getLeft() * 72, $printMargins->getTop() * 72, $printMargins->getRight() * 72);
  71          $pdf->SetAutoPageBreak(true, $printMargins->getBottom() * 72);
  72  
  73          $pdf->setPrintHeader(false);
  74          $pdf->setPrintFooter(false);
  75  
  76          $pdf->AddPage();
  77  
  78          //  Set the appropriate font
  79          $pdf->SetFont($this->getFont());
  80          $pdf->writeHTML(
  81              $this->generateHTMLHeader(false) .
  82              $this->generateSheetData() .
  83              $this->generateHTMLFooter()
  84          );
  85  
  86          //  Document info
  87          $pdf->SetTitle($this->spreadsheet->getProperties()->getTitle());
  88          $pdf->SetAuthor($this->spreadsheet->getProperties()->getCreator());
  89          $pdf->SetSubject($this->spreadsheet->getProperties()->getSubject());
  90          $pdf->SetKeywords($this->spreadsheet->getProperties()->getKeywords());
  91          $pdf->SetCreator($this->spreadsheet->getProperties()->getCreator());
  92  
  93          //  Write to file
  94          fwrite($fileHandle, $pdf->output($pFilename, 'S'));
  95  
  96          parent::restoreStateAfterSave($fileHandle);
  97      }
  98  }