Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

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

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