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\Tcpdf;
  12  
  13  use setasign\Fpdi\FpdiTrait;
  14  use setasign\Fpdi\PdfParser\CrossReference\CrossReferenceException;
  15  use setasign\Fpdi\PdfParser\Filter\AsciiHex;
  16  use setasign\Fpdi\PdfParser\PdfParserException;
  17  use setasign\Fpdi\PdfParser\Type\PdfHexString;
  18  use setasign\Fpdi\PdfParser\Type\PdfIndirectObject;
  19  use setasign\Fpdi\PdfParser\Type\PdfNull;
  20  use setasign\Fpdi\PdfParser\Type\PdfNumeric;
  21  use setasign\Fpdi\PdfParser\Type\PdfStream;
  22  use setasign\Fpdi\PdfParser\Type\PdfString;
  23  use setasign\Fpdi\PdfParser\Type\PdfType;
  24  use setasign\Fpdi\PdfParser\Type\PdfTypeException;
  25  
  26  /**
  27   * Class Fpdi
  28   *
  29   * This class let you import pages of existing PDF documents into a reusable structure for TCPDF.
  30   *
  31   * @method _encrypt_data(int $n, string $s) string
  32   */
  33  class Fpdi extends \pdf
  34  {
  35      use FpdiTrait {
  36          writePdfType as fpdiWritePdfType;
  37          useImportedPage as fpdiUseImportedPage;
  38      }
  39  
  40      /**
  41       * FPDI version
  42       *
  43       * @string
  44       */
  45      const VERSION = '2.3.7';
  46  
  47      /**
  48       * A counter for template ids.
  49       *
  50       * @var int
  51       */
  52      protected $templateId = 0;
  53  
  54      /**
  55       * The currently used object number.
  56       *
  57       * @var int|null
  58       */
  59      protected $currentObjectNumber;
  60  
  61      protected function _enddoc()
  62      {
  63          parent::_enddoc();
  64          $this->cleanUp();
  65      }
  66  
  67      /**
  68       * Get the next template id.
  69       *
  70       * @return int
  71       */
  72      protected function getNextTemplateId()
  73      {
  74          return $this->templateId++;
  75      }
  76  
  77      /**
  78       * Draws an imported page onto the page or another template.
  79       *
  80       * Give only one of the size parameters (width, height) to calculate the other one automatically in view to the
  81       * aspect ratio.
  82       *
  83       * @param mixed $tpl The template id
  84       * @param float|int|array $x The abscissa of upper-left corner. Alternatively you could use an assoc array
  85       *                           with the keys "x", "y", "width", "height", "adjustPageSize".
  86       * @param float|int $y The ordinate of upper-left corner.
  87       * @param float|int|null $width The width.
  88       * @param float|int|null $height The height.
  89       * @param bool $adjustPageSize
  90       * @return array The size
  91       * @see FpdiTrait::getTemplateSize()
  92       */
  93      public function useTemplate($tpl, $x = 0, $y = 0, $width = null, $height = null, $adjustPageSize = false)
  94      {
  95          return $this->useImportedPage($tpl, $x, $y, $width, $height, $adjustPageSize);
  96      }
  97  
  98      /**
  99       * Draws an imported page onto the page.
 100       *
 101       * Give only one of the size parameters (width, height) to calculate the other one automatically in view to the
 102       * aspect ratio.
 103       *
 104       * @param mixed $pageId The page id
 105       * @param float|int|array $x The abscissa of upper-left corner. Alternatively you could use an assoc array
 106       *                           with the keys "x", "y", "width", "height", "adjustPageSize".
 107       * @param float|int $y The ordinate of upper-left corner.
 108       * @param float|int|null $width The width.
 109       * @param float|int|null $height The height.
 110       * @param bool $adjustPageSize
 111       * @return array The size.
 112       * @see Fpdi::getTemplateSize()
 113       */
 114      public function useImportedPage($pageId, $x = 0, $y = 0, $width = null, $height = null, $adjustPageSize = false)
 115      {
 116          $size = $this->fpdiUseImportedPage($pageId, $x, $y, $width, $height, $adjustPageSize);
 117          if ($this->inxobj) {
 118              $importedPage = $this->importedPages[$pageId];
 119              $this->xobjects[$this->xobjid]['importedPages'][$importedPage['id']] = $pageId;
 120          }
 121  
 122          return $size;
 123      }
 124  
 125      /**
 126       * Get the size of an imported page.
 127       *
 128       * Give only one of the size parameters (width, height) to calculate the other one automatically in view to the
 129       * aspect ratio.
 130       *
 131       * @param mixed $tpl The template id
 132       * @param float|int|null $width The width.
 133       * @param float|int|null $height The height.
 134       * @return array|bool An array with following keys: width, height, 0 (=width), 1 (=height), orientation (L or P)
 135       */
 136      public function getTemplateSize($tpl, $width = null, $height = null)
 137      {
 138          return $this->getImportedPageSize($tpl, $width, $height);
 139      }
 140  
 141      /**
 142       * @inheritdoc
 143       * @return string
 144       */
 145      protected function _getxobjectdict()
 146      {
 147          $out = parent::_getxobjectdict();
 148  
 149          foreach ($this->importedPages as $key => $pageData) {
 150              $out .= '/' . $pageData['id'] . ' ' . $pageData['objectNumber'] . ' 0 R ';
 151          }
 152  
 153          return $out;
 154      }
 155  
 156      /**
 157       * @inheritdoc
 158       * @throws CrossReferenceException
 159       * @throws PdfParserException
 160       */
 161      protected function _putxobjects()
 162      {
 163          foreach ($this->importedPages as $key => $pageData) {
 164              $this->currentObjectNumber = $this->_newobj();
 165              $this->importedPages[$key]['objectNumber'] = $this->currentObjectNumber;
 166              $this->currentReaderId = $pageData['readerId'];
 167              $this->writePdfType($pageData['stream']);
 168              $this->_put('endobj');
 169          }
 170  
 171          foreach (\array_keys($this->readers) as $readerId) {
 172              $parser = $this->getPdfReader($readerId)->getParser();
 173              $this->currentReaderId = $readerId;
 174  
 175              while (($objectNumber = \array_pop($this->objectsToCopy[$readerId])) !== null) {
 176                  try {
 177                      $object = $parser->getIndirectObject($objectNumber);
 178                  } catch (CrossReferenceException $e) {
 179                      if ($e->getCode() === CrossReferenceException::OBJECT_NOT_FOUND) {
 180                          $object = PdfIndirectObject::create($objectNumber, 0, new PdfNull());
 181                      } else {
 182                          throw $e;
 183                      }
 184                  }
 185  
 186                  $this->writePdfType($object);
 187              }
 188          }
 189  
 190          // let's prepare resources for imported pages in templates
 191          foreach ($this->xobjects as $xObjectId => $data) {
 192              if (!isset($data['importedPages'])) {
 193                  continue;
 194              }
 195  
 196              foreach ($data['importedPages'] as $id => $pageKey) {
 197                  $page = $this->importedPages[$pageKey];
 198                  $this->xobjects[$xObjectId]['xobjects'][$id] = ['n' => $page['objectNumber']];
 199              }
 200          }
 201  
 202  
 203          parent::_putxobjects();
 204          $this->currentObjectNumber = null;
 205      }
 206  
 207      /**
 208       * Append content to the buffer of TCPDF.
 209       *
 210       * @param string $s
 211       * @param bool $newLine
 212       */
 213      protected function _put($s, $newLine = true)
 214      {
 215          if ($newLine) {
 216              $this->setBuffer($s . "\n");
 217          } else {
 218              $this->setBuffer($s);
 219          }
 220      }
 221  
 222      /**
 223       * Begin a new object and return the object number.
 224       *
 225       * @param int|string $objid Object ID (leave empty to get a new ID).
 226       * @return int object number
 227       */
 228      protected function _newobj($objid = '')
 229      {
 230          $this->_out($this->_getobj($objid));
 231          return $this->n;
 232      }
 233  
 234      /**
 235       * Writes a PdfType object to the resulting buffer.
 236       *
 237       * @param PdfType $value
 238       * @throws PdfTypeException
 239       */
 240      protected function writePdfType(PdfType $value)
 241      {
 242          if (!$this->encrypted) {
 243              $this->fpdiWritePdfType($value);
 244              return;
 245          }
 246  
 247          if ($value instanceof PdfString) {
 248              $string = PdfString::unescape($value->value);
 249              $string = $this->_encrypt_data($this->currentObjectNumber, $string);
 250              $value->value = \TCPDF_STATIC::_escape($string);
 251          } elseif ($value instanceof PdfHexString) {
 252              $filter = new AsciiHex();
 253              $string = $filter->decode($value->value);
 254              $string = $this->_encrypt_data($this->currentObjectNumber, $string);
 255              $value->value = $filter->encode($string, true);
 256          } elseif ($value instanceof PdfStream) {
 257              $stream = $value->getStream();
 258              $stream = $this->_encrypt_data($this->currentObjectNumber, $stream);
 259              $dictionary = $value->value;
 260              $dictionary->value['Length'] = PdfNumeric::create(\strlen($stream));
 261              $value = PdfStream::create($dictionary, $stream);
 262          } elseif ($value instanceof PdfIndirectObject) {
 263              /**
 264               * @var PdfIndirectObject $value
 265               */
 266              $this->currentObjectNumber = $this->objectMap[$this->currentReaderId][$value->objectNumber];
 267          }
 268  
 269          $this->fpdiWritePdfType($value);
 270      }
 271  }