|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- <?php
-
-
- if (!class_exists('pdf_parser')) {
- require_once('pdf_parser.php');
- }
-
-
- class fpdi_pdf_parser extends pdf_parser
- {
-
-
- protected $_pages;
-
-
-
- protected $_pageCount;
-
-
-
- public $pageNo;
-
-
-
- public $_pdfVersion;
-
-
-
- public $availableBoxes = array('/MediaBox', '/CropBox', '/BleedBox', '/TrimBox', '/ArtBox');
-
-
-
- public function __construct($filename)
- {
- parent::__construct($filename);
-
-
- $pages = $this->resolveObject($this->_root[1][1]['/Pages']);
-
-
- $this->_readPages($pages, $this->_pages);
-
-
- $this->_pageCount = count($this->_pages);
- }
-
-
-
- public function getPageCount()
- {
- return $this->_pageCount;
- }
-
-
-
- public function setPageNo($pageNo)
- {
- $pageNo = ((int) $pageNo) - 1;
-
- if ($pageNo < 0 || $pageNo >= $this->getPageCount()) {
- throw new InvalidArgumentException('Invalid page number!');
- }
-
- $this->pageNo = $pageNo;
- }
-
-
-
- public function getPageResources()
- {
- return $this->_getPageResources($this->_pages[$this->pageNo]);
- }
-
-
-
- protected function _getPageResources($obj)
- {
- $obj = $this->resolveObject($obj);
-
-
-
-
-
- if (isset($obj[1][1]['/Resources'])) {
- $res = $this->resolveObject($obj[1][1]['/Resources']);
- if ($res[0] == pdf_parser::TYPE_OBJECT)
- return $res[1];
- return $res;
- }
-
- if (!isset($obj[1][1]['/Parent'])) {
- return false;
- }
-
- $res = $this->_getPageResources($obj[1][1]['/Parent']);
- if ($res[0] == pdf_parser::TYPE_OBJECT)
- return $res[1];
- return $res;
- }
-
-
-
- public function getContent()
- {
- $buffer = '';
-
- if (isset($this->_pages[$this->pageNo][1][1]['/Contents'])) {
- $contents = $this->_getPageContent($this->_pages[$this->pageNo][1][1]['/Contents']);
- foreach ($contents AS $tmpContent) {
- $buffer .= $this->_unFilterStream($tmpContent) . ' ';
- }
- }
-
- return $buffer;
- }
-
-
-
- protected function _getPageContent($contentRef)
- {
- $contents = array();
-
- if ($contentRef[0] == pdf_parser::TYPE_OBJREF) {
- $content = $this->resolveObject($contentRef);
- if ($content[1][0] == pdf_parser::TYPE_ARRAY) {
- $contents = $this->_getPageContent($content[1]);
- } else {
- $contents[] = $content;
- }
- } else if ($contentRef[0] == pdf_parser::TYPE_ARRAY) {
- foreach ($contentRef[1] AS $tmp_content_ref) {
- $contents = array_merge($contents, $this->_getPageContent($tmp_content_ref));
- }
- }
-
- return $contents;
- }
-
-
-
- protected function _getPageBox($page, $boxIndex, $k)
- {
- $page = $this->resolveObject($page);
- $box = null;
- if (isset($page[1][1][$boxIndex])) {
- $box = $page[1][1][$boxIndex];
- }
-
- if (!is_null($box) && $box[0] == pdf_parser::TYPE_OBJREF) {
- $tmp_box = $this->resolveObject($box);
- $box = $tmp_box[1];
- }
-
- if (!is_null($box) && $box[0] == pdf_parser::TYPE_ARRAY) {
- $b = $box[1];
- return array(
- 'x' => $b[0][1] / $k,
- 'y' => $b[1][1] / $k,
- 'w' => abs($b[0][1] - $b[2][1]) / $k,
- 'h' => abs($b[1][1] - $b[3][1]) / $k,
- 'llx' => min($b[0][1], $b[2][1]) / $k,
- 'lly' => min($b[1][1], $b[3][1]) / $k,
- 'urx' => max($b[0][1], $b[2][1]) / $k,
- 'ury' => max($b[1][1], $b[3][1]) / $k,
- );
- } else if (!isset($page[1][1]['/Parent'])) {
- return false;
- } else {
- return $this->_getPageBox($this->resolveObject($page[1][1]['/Parent']), $boxIndex, $k);
- }
- }
-
-
-
- public function getPageBoxes($pageNo, $k)
- {
- if (!isset($this->_pages[$pageNo - 1])) {
- throw new InvalidArgumentException('Page ' . $pageNo . ' does not exists.');
- }
-
- return $this->_getPageBoxes($this->_pages[$pageNo - 1], $k);
- }
-
-
-
- protected function _getPageBoxes($page, $k)
- {
- $boxes = array();
-
- foreach($this->availableBoxes AS $box) {
- if ($_box = $this->_getPageBox($page, $box, $k)) {
- $boxes[$box] = $_box;
- }
- }
-
- return $boxes;
- }
-
-
-
- public function getPageRotation($pageNo)
- {
- if (!isset($this->_pages[$pageNo - 1])) {
- throw new InvalidArgumentException('Page ' . $pageNo . ' does not exists.');
- }
-
- return $this->_getPageRotation($this->_pages[$pageNo - 1]);
- }
-
-
-
- protected function _getPageRotation($obj)
- {
- $obj = $this->resolveObject($obj);
- if (isset($obj[1][1]['/Rotate'])) {
- $res = $this->resolveObject($obj[1][1]['/Rotate']);
- if ($res[0] == pdf_parser::TYPE_OBJECT)
- return $res[1];
- return $res;
- }
-
- if (!isset($obj[1][1]['/Parent'])) {
- return false;
- }
-
- $res = $this->_getPageRotation($obj[1][1]['/Parent']);
- if ($res[0] == pdf_parser::TYPE_OBJECT)
- return $res[1];
-
- return $res;
- }
-
-
-
- protected function _readPages(&$pages, &$result)
- {
-
- $_kids = $this->resolveObject($pages[1][1]['/Kids']);
-
- if (!is_array($_kids)) {
- throw new Exception('Cannot find /Kids in current /Page-Dictionary');
- }
-
- if ($_kids[0] === self::TYPE_OBJECT) {
- $_kids = $_kids[1];
- }
-
- $kids = $_kids[1];
-
- foreach ($kids as $v) {
- $pg = $this->resolveObject($v);
- if ($pg[1][1]['/Type'][1] === '/Pages') {
-
-
- $this->_readPages($pg, $result);
- } else {
- $result[] = $pg;
- }
- }
- }
- }
|