Search moodle.org's
Developer Documentation

See Release Notes

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

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [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.6';
  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       */
 144      protected function _getxobjectdict()
 145      {
 146          $out = parent::_getxobjectdict();
 147  
 148          foreach ($this->importedPages as $key => $pageData) {
 149              $out .= '/' . $pageData['id'] . ' ' . $pageData['objectNumber'] . ' 0 R ';
 150          }
 151  
 152          return $out;
 153      }
 154  
 155      /**
 156       * @inheritdoc
 157       * @throws CrossReferenceException
 158       * @throws PdfParserException
 159       */
 160      protected function _putxobjects()
 161      {
 162          foreach ($this->importedPages as $key => $pageData) {
 163              $this->currentObjectNumber = $this->_newobj();
 164              $this->importedPages[$key]['objectNumber'] = $this->currentObjectNumber;
 165              $this->currentReaderId = $pageData['readerId'];
 166              $this->writePdfType($pageData['stream']);
 167              $this->_put('endobj');
 168          }
 169  
 170          foreach (\array_keys($this->readers) as $readerId) {
 171              $parser = $this->getPdfReader($readerId)->getParser();
 172              $this->currentReaderId = $readerId;
 173  
 174              while (($objectNumber = \array_pop($this->objectsToCopy[$readerId])) !== null) {
 175                  try {
 176                      $object = $parser->getIndirectObject($objectNumber);
 177                  } catch (CrossReferenceException $e) {
 178                      if ($e->getCode() === CrossReferenceException::OBJECT_NOT_FOUND) {
 179                          $object = PdfIndirectObject::create($objectNumber, 0, new PdfNull());
 180                      } else {
 181                          throw $e;
 182                      }
 183                  }
 184  
 185                  $this->writePdfType($object);
 186              }
 187          }
 188  
 189          // let's prepare resources for imported pages in templates
 190          foreach ($this->xobjects as $xObjectId => $data) {
 191              if (!isset($data['importedPages'])) {
 192                  continue;
 193              }
 194  
 195              foreach ($data['importedPages'] as $id => $pageKey) {
 196                  $page = $this->importedPages[$pageKey];
 197                  $this->xobjects[$xObjectId]['xobjects'][$id] = ['n' => $page['objectNumber']];
 198              }
 199          }
 200  
 201  
 202          parent::_putxobjects();
 203          $this->currentObjectNumber = null;
 204      }
 205  
 206      /**
 207       * Append content to the buffer of TCPDF.
 208       *
 209       * @param string $s
 210       * @param bool $newLine
 211       */
 212      protected function _put($s, $newLine = true)
 213      {
 214          if ($newLine) {
 215              $this->setBuffer($s . "\n");
 216          } else {
 217              $this->setBuffer($s);
 218          }
 219      }
 220  
 221      /**
 222       * Begin a new object and return the object number.
 223       *
 224       * @param int|string $objid Object ID (leave empty to get a new ID).
 225       * @return int object number
 226       */
 227      protected function _newobj($objid = '')
 228      {
 229          $this->_out($this->_getobj($objid));
 230          return $this->n;
 231      }
 232  
 233      /**
 234       * Writes a PdfType object to the resulting buffer.
 235       *
 236       * @param PdfType $value
 237       * @throws PdfTypeException
 238       */
 239      protected function writePdfType(PdfType $value)
 240      {
 241          if (!$this->encrypted) {
 242              $this->fpdiWritePdfType($value);
 243              return;
 244          }
 245  
 246          if ($value instanceof PdfString) {
 247              $string = PdfString::unescape($value->value);
 248              $string = $this->_encrypt_data($this->currentObjectNumber, $string);
 249              $value->value = \TCPDF_STATIC::_escape($string);
 250          } elseif ($value instanceof PdfHexString) {
 251              $filter = new AsciiHex();
 252              $string = $filter->decode($value->value);
 253              $string = $this->_encrypt_data($this->currentObjectNumber, $string);
 254              $value->value = $filter->encode($string, true);
 255          } elseif ($value instanceof PdfStream) {
 256              $stream = $value->getStream();
 257              $stream = $this->_encrypt_data($this->currentObjectNumber, $stream);
 258              $dictionary = $value->value;
 259              $dictionary->value['Length'] = PdfNumeric::create(\strlen($stream));
 260              $value = PdfStream::create($dictionary, $stream);
 261          } elseif ($value instanceof PdfIndirectObject) {
 262              /**
 263               * @var PdfIndirectObject $value
 264               */
 265              $this->currentObjectNumber = $this->objectMap[$this->currentReaderId][$value->objectNumber];
 266          }
 267  
 268          $this->fpdiWritePdfType($value);
 269      }
 270  }