Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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

   1  <?php
   2  /**
   3   * This file is part of FPDI
   4   *
   5   * @package   setasign\Fpdi
   6   * @copyright Copyright (c) 2019 Setasign - Jan Slabon (https://www.setasign.com)
   7   * @license   http://opensource.org/licenses/mit-license The MIT License
   8   */
   9  
  10  namespace setasign\Fpdi\PdfParser\CrossReference;
  11  
  12  use setasign\Fpdi\PdfParser\PdfParser;
  13  use setasign\Fpdi\PdfParser\Type\PdfDictionary;
  14  use setasign\Fpdi\PdfParser\Type\PdfToken;
  15  use setasign\Fpdi\PdfParser\Type\PdfTypeException;
  16  
  17  /**
  18   * Abstract class for cross-reference reader classes.
  19   *
  20   * @package setasign\Fpdi\PdfParser\CrossReference
  21   */
  22  abstract class AbstractReader
  23  {
  24      /**
  25       * @var PdfParser
  26       */
  27      protected $parser;
  28  
  29      /**
  30       * @var PdfDictionary
  31       */
  32      protected $trailer;
  33  
  34      /**
  35       * AbstractReader constructor.
  36       *
  37       * @param PdfParser $parser
  38       * @throws CrossReferenceException
  39       * @throws PdfTypeException
  40       */
  41      public function __construct(PdfParser $parser)
  42      {
  43          $this->parser = $parser;
  44          $this->readTrailer();
  45      }
  46  
  47      /**
  48       * Get the trailer dictionary.
  49       *
  50       * @return PdfDictionary
  51       */
  52      public function getTrailer()
  53      {
  54          return $this->trailer;
  55      }
  56  
  57      /**
  58       * Read the trailer dictionary.
  59       *
  60       * @throws CrossReferenceException
  61       * @throws PdfTypeException
  62       */
  63      protected function readTrailer()
  64      {
  65          try {
  66              $trailerKeyword = $this->parser->readValue(null, PdfToken::class);
  67              if ($trailerKeyword->value !== 'trailer') {
  68                  throw new CrossReferenceException(
  69                      \sprintf(
  70                          'Unexpected end of cross reference. "trailer"-keyword expected, got: %s.',
  71                          $trailerKeyword->value
  72                      ),
  73                      CrossReferenceException::UNEXPECTED_END
  74                  );
  75              }
  76          } catch (PdfTypeException $e) {
  77              throw new CrossReferenceException(
  78                  'Unexpected end of cross reference. "trailer"-keyword expected, got an invalid object type.',
  79                  CrossReferenceException::UNEXPECTED_END,
  80                  $e
  81              );
  82          }
  83  
  84          try {
  85              $trailer = $this->parser->readValue(null, PdfDictionary::class);
  86          } catch (PdfTypeException $e) {
  87              throw new CrossReferenceException(
  88                  'Unexpected end of cross reference. Trailer not found.',
  89                  CrossReferenceException::UNEXPECTED_END,
  90                  $e
  91              );
  92          }
  93  
  94          $this->trailer = $trailer;
  95      }
  96  }