AbstractReader.php 2.4 KB

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