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  /**
   4   * This file is part of FPDI
   5   *
   6   * @package   setasign\Fpdi
   7   * @copyright Copyright (c) 2020 Setasign GmbH & Co. KG (https://www.setasign.com)
   8   * @license   http://opensource.org/licenses/mit-license The MIT License
   9   */
  10  
  11  namespace setasign\Fpdi\Tfpdf;
  12  
  13  use setasign\Fpdi\FpdiTrait;
  14  use setasign\Fpdi\PdfParser\CrossReference\CrossReferenceException;
  15  use setasign\Fpdi\PdfParser\PdfParserException;
  16  use setasign\Fpdi\PdfParser\Type\PdfIndirectObject;
  17  use setasign\Fpdi\PdfParser\Type\PdfNull;
  18  
  19  /**
  20   * Class Fpdi
  21   *
  22   * This class let you import pages of existing PDF documents into a reusable structure for tFPDF.
  23   */
  24  class Fpdi extends FpdfTpl
  25  {
  26      use FpdiTrait;
  27  
  28      /**
  29       * FPDI version
  30       *
  31       * @string
  32       */
  33      const VERSION = '2.3.5';
  34  
  35      public function _enddoc()
  36      {
  37          parent::_enddoc();
  38          $this->cleanUp();
  39      }
  40  
  41      /**
  42       * Draws an imported page or a template onto the page or another template.
  43       *
  44       * Give only one of the size parameters (width, height) to calculate the other one automatically in view to the
  45       * aspect ratio.
  46       *
  47       * @param mixed $tpl The template id
  48       * @param float|int|array $x The abscissa of upper-left corner. Alternatively you could use an assoc array
  49       *                           with the keys "x", "y", "width", "height", "adjustPageSize".
  50       * @param float|int $y The ordinate of upper-left corner.
  51       * @param float|int|null $width The width.
  52       * @param float|int|null $height The height.
  53       * @param bool $adjustPageSize
  54       * @return array The size
  55       * @see Fpdi::getTemplateSize()
  56       */
  57      public function useTemplate($tpl, $x = 0, $y = 0, $width = null, $height = null, $adjustPageSize = false)
  58      {
  59          if (isset($this->importedPages[$tpl])) {
  60              $size = $this->useImportedPage($tpl, $x, $y, $width, $height, $adjustPageSize);
  61              if ($this->currentTemplateId !== null) {
  62                  $this->templates[$this->currentTemplateId]['resources']['templates']['importedPages'][$tpl] = $tpl;
  63              }
  64              return $size;
  65          }
  66  
  67          return parent::useTemplate($tpl, $x, $y, $width, $height, $adjustPageSize);
  68      }
  69  
  70      /**
  71       * Get the size of an imported page or template.
  72       *
  73       * Give only one of the size parameters (width, height) to calculate the other one automatically in view to the
  74       * aspect ratio.
  75       *
  76       * @param mixed $tpl The template id
  77       * @param float|int|null $width The width.
  78       * @param float|int|null $height The height.
  79       * @return array|bool An array with following keys: width, height, 0 (=width), 1 (=height), orientation (L or P)
  80       */
  81      public function getTemplateSize($tpl, $width = null, $height = null)
  82      {
  83          $size = parent::getTemplateSize($tpl, $width, $height);
  84          if ($size === false) {
  85              return $this->getImportedPageSize($tpl, $width, $height);
  86          }
  87  
  88          return $size;
  89      }
  90  
  91      /**
  92       * @inheritdoc
  93       * @throws CrossReferenceException
  94       * @throws PdfParserException
  95       */
  96      public function _putimages()
  97      {
  98          $this->currentReaderId = null;
  99          parent::_putimages();
 100  
 101          foreach ($this->importedPages as $key => $pageData) {
 102              $this->_newobj();
 103              $this->importedPages[$key]['objectNumber'] = $this->n;
 104              $this->currentReaderId = $pageData['readerId'];
 105              $this->writePdfType($pageData['stream']);
 106              $this->_put('endobj');
 107          }
 108  
 109          foreach (\array_keys($this->readers) as $readerId) {
 110              $parser = $this->getPdfReader($readerId)->getParser();
 111              $this->currentReaderId = $readerId;
 112  
 113              while (($objectNumber = \array_pop($this->objectsToCopy[$readerId])) !== null) {
 114                  try {
 115                      $object = $parser->getIndirectObject($objectNumber);
 116                  } catch (CrossReferenceException $e) {
 117                      if ($e->getCode() === CrossReferenceException::OBJECT_NOT_FOUND) {
 118                          $object = PdfIndirectObject::create($objectNumber, 0, new PdfNull());
 119                      } else {
 120                          throw $e;
 121                      }
 122                  }
 123  
 124                  $this->writePdfType($object);
 125              }
 126          }
 127  
 128          $this->currentReaderId = null;
 129      }
 130  
 131      /**
 132       * @inheritdoc
 133       */
 134      protected function _putxobjectdict()
 135      {
 136          foreach ($this->importedPages as $key => $pageData) {
 137              $this->_put('/' . $pageData['id'] . ' ' . $pageData['objectNumber'] . ' 0 R');
 138          }
 139  
 140          parent::_putxobjectdict();
 141      }
 142  
 143      /**
 144       * @inheritdoc
 145       */
 146      protected function _put($s, $newLine = true)
 147      {
 148          if ($newLine) {
 149              $this->buffer .= $s . "\n";
 150          } else {
 151              $this->buffer .= $s;
 152          }
 153      }
 154  }