123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 |
- <?php
- /**
- * This file is part of FPDI
- *
- * @package Fpdi
- * @copyright Copyright (c) 2020 Setasign GmbH & Co. KG (https://www.setasign.com)
- * @license http://opensource.org/licenses/mit-license The MIT License
- */
- namespace Fpdi\PdfReader;
- use Fpdi\PdfParser\Filter\FilterException;
- use Fpdi\PdfParser\PdfParser;
- use Fpdi\PdfParser\PdfParserException;
- use Fpdi\PdfParser\Type\PdfArray;
- use Fpdi\PdfParser\Type\PdfDictionary;
- use Fpdi\PdfParser\Type\PdfIndirectObject;
- use Fpdi\PdfParser\Type\PdfNull;
- use Fpdi\PdfParser\Type\PdfNumeric;
- use Fpdi\PdfParser\Type\PdfStream;
- use Fpdi\PdfParser\Type\PdfType;
- use Fpdi\PdfParser\Type\PdfTypeException;
- use Fpdi\PdfReader\DataStructure\Rectangle;
- use Fpdi\PdfParser\CrossReference\CrossReferenceException;
- /**
- * Class representing a page of a PDF document
- */
- class Page
- {
- /**
- * @var PdfIndirectObject
- */
- protected $pageObject;
- /**
- * @var PdfDictionary
- */
- protected $pageDictionary;
- /**
- * @var PdfParser
- */
- protected $parser;
- /**
- * Inherited attributes
- *
- * @var null|array
- */
- protected $inheritedAttributes;
- /**
- * Page constructor.
- *
- * @param PdfIndirectObject $page
- * @param PdfParser $parser
- */
- public function __construct(PdfIndirectObject $page, PdfParser $parser)
- {
- $this->pageObject = $page;
- $this->parser = $parser;
- }
- /**
- * Get the indirect object of this page.
- *
- * @return PdfIndirectObject
- */
- public function getPageObject()
- {
- return $this->pageObject;
- }
- /**
- * Get the dictionary of this page.
- *
- * @return PdfDictionary
- * @throws PdfParserException
- * @throws PdfTypeException
- * @throws CrossReferenceException
- */
- public function getPageDictionary()
- {
- if (null === $this->pageDictionary) {
- $this->pageDictionary = PdfDictionary::ensure(PdfType::resolve($this->getPageObject(), $this->parser));
- }
- return $this->pageDictionary;
- }
- /**
- * Get a page attribute.
- *
- * @param string $name
- * @param bool $inherited
- * @return PdfType|null
- * @throws PdfParserException
- * @throws PdfTypeException
- * @throws CrossReferenceException
- */
- public function getAttribute($name, $inherited = true)
- {
- $dict = $this->getPageDictionary();
- if (isset($dict->value[$name])) {
- return $dict->value[$name];
- }
- $inheritedKeys = ['Resources', 'MediaBox', 'CropBox', 'Rotate'];
- if ($inherited && \in_array($name, $inheritedKeys, true)) {
- if ($this->inheritedAttributes === null) {
- $this->inheritedAttributes = [];
- $inheritedKeys = \array_filter($inheritedKeys, function ($key) use ($dict) {
- return !isset($dict->value[$key]);
- });
- if (\count($inheritedKeys) > 0) {
- $parentDict = PdfType::resolve(PdfDictionary::get($dict, 'Parent'), $this->parser);
- while ($parentDict instanceof PdfDictionary) {
- foreach ($inheritedKeys as $index => $key) {
- if (isset($parentDict->value[$key])) {
- $this->inheritedAttributes[$key] = $parentDict->value[$key];
- unset($inheritedKeys[$index]);
- }
- }
- /** @noinspection NotOptimalIfConditionsInspection */
- if (isset($parentDict->value['Parent']) && \count($inheritedKeys) > 0) {
- $parentDict = PdfType::resolve(PdfDictionary::get($parentDict, 'Parent'), $this->parser);
- } else {
- break;
- }
- }
- }
- }
- if (isset($this->inheritedAttributes[$name])) {
- return $this->inheritedAttributes[$name];
- }
- }
- return null;
- }
- /**
- * Get the rotation value.
- *
- * @return int
- * @throws PdfParserException
- * @throws PdfTypeException
- * @throws CrossReferenceException
- */
- public function getRotation()
- {
- $rotation = $this->getAttribute('Rotate');
- if (null === $rotation) {
- return 0;
- }
- $rotation = PdfNumeric::ensure(PdfType::resolve($rotation, $this->parser))->value % 360;
- if ($rotation < 0) {
- $rotation += 360;
- }
- return $rotation;
- }
- /**
- * Get a boundary of this page.
- *
- * @param string $box
- * @param bool $fallback
- * @return bool|Rectangle
- * @throws PdfParserException
- * @throws PdfTypeException
- * @throws CrossReferenceException
- * @see PageBoundaries
- */
- public function getBoundary($box = PageBoundaries::CROP_BOX, $fallback = true)
- {
- $value = $this->getAttribute($box);
- if ($value !== null) {
- return Rectangle::byPdfArray($value, $this->parser);
- }
- if ($fallback === false) {
- return false;
- }
- switch ($box) {
- case PageBoundaries::BLEED_BOX:
- case PageBoundaries::TRIM_BOX:
- case PageBoundaries::ART_BOX:
- return $this->getBoundary(PageBoundaries::CROP_BOX, true);
- case PageBoundaries::CROP_BOX:
- return $this->getBoundary(PageBoundaries::MEDIA_BOX, true);
- }
- return false;
- }
- /**
- * Get the width and height of this page.
- *
- * @param string $box
- * @param bool $fallback
- * @return array|bool
- * @throws PdfParserException
- * @throws PdfTypeException
- * @throws CrossReferenceException
- */
- public function getWidthAndHeight($box = PageBoundaries::CROP_BOX, $fallback = true)
- {
- $boundary = $this->getBoundary($box, $fallback);
- if ($boundary === false) {
- return false;
- }
- $rotation = $this->getRotation();
- $interchange = ($rotation / 90) % 2;
- return [
- $interchange ? $boundary->getHeight() : $boundary->getWidth(),
- $interchange ? $boundary->getWidth() : $boundary->getHeight()
- ];
- }
- /**
- * Get the raw content stream.
- *
- * @return string
- * @throws PdfReaderException
- * @throws PdfTypeException
- * @throws FilterException
- * @throws PdfParserException
- */
- public function getContentStream()
- {
- $dict = $this->getPageDictionary();
- $contents = PdfType::resolve(PdfDictionary::get($dict, 'Contents'), $this->parser);
- if ($contents instanceof PdfNull) {
- return '';
- }
- if ($contents instanceof PdfArray) {
- $result = [];
- foreach ($contents->value as $content) {
- $content = PdfType::resolve($content, $this->parser);
- if (!($content instanceof PdfStream)) {
- continue;
- }
- $result[] = $content->getUnfilteredStream();
- }
- return \implode("\n", $result);
- }
- if ($contents instanceof PdfStream) {
- return $contents->getUnfilteredStream();
- }
- throw new PdfReaderException(
- 'Array or stream expected.',
- PdfReaderException::UNEXPECTED_DATA_TYPE
- );
- }
- }
|