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