123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361 |
- <?php
- class fpdi_pdf_parser extends pdf_parser {
-
- var $pages;
-
- var $page_count;
-
- var $pageno;
-
- var $fpdi;
-
- var $availableBoxes = array("/MediaBox","/CropBox","/BleedBox","/TrimBox","/ArtBox");
-
- function fpdi_pdf_parser($filename,&$fpdi) {
- $this->fpdi =& $fpdi;
- $this->filename = $filename;
- parent::pdf_parser($filename);
- if ($this->success == false) { return false; }
-
- $pages = $this->pdf_resolve_object($this->c, $this->root[1][1]['/Pages']);
- if ($this->success == false) { return false; }
-
- $this->read_pages($this->c, $pages, $this->pages);
- if ($this->success == false) { return false; }
-
- $this->page_count = count($this->pages);
- }
-
- function getPageCount() {
- return $this->page_count;
- }
-
- function setPageno($pageno) {
- $pageno = ((int) $pageno) - 1;
- if ($pageno < 0 || $pageno >= $this->getPageCount()) {
- $this->fpdi->error("Pagenumber is wrong!");
- }
- $this->pageno = $pageno;
- }
-
- function getPageResources() {
- return $this->_getPageResources($this->pages[$this->pageno]);
- }
-
- function _getPageResources ($obj) {
- $obj = $this->pdf_resolve_object($this->c, $obj);
-
-
-
-
- if (isset ($obj[1][1]['/Resources'])) {
- $res = $this->pdf_resolve_object($this->c, $obj[1][1]['/Resources']);
- if ($res[0] == PDF_TYPE_OBJECT)
- return $res[1];
- return $res;
- } else {
- if (!isset ($obj[1][1]['/Parent'])) {
- return false;
- } else {
- $res = $this->_getPageResources($obj[1][1]['/Parent']);
- if ($res[0] == PDF_TYPE_OBJECT)
- return $res[1];
- return $res;
- }
- }
- }
-
- 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 $tmp_content) {
- $buffer .= $this->_rebuildContentStream($tmp_content).' ';
- }
- }
- return $buffer;
- }
-
- function _getPageContent($content_ref) {
- $contents = array();
- if ($content_ref[0] == PDF_TYPE_OBJREF) {
- $content = $this->pdf_resolve_object($this->c, $content_ref);
- if ($content[1][0] == PDF_TYPE_ARRAY) {
- $contents = $this->_getPageContent($content[1]);
- } else {
- $contents[] = $content;
- }
- } else if ($content_ref[0] == PDF_TYPE_ARRAY) {
- foreach ($content_ref[1] AS $tmp_content_ref) {
- $contents = array_merge($contents,$this->_getPageContent($tmp_content_ref));
- }
- }
- return $contents;
- }
-
- function _rebuildContentStream($obj) {
- $filters = array();
- if (isset($obj[1][1]['/Filter'])) {
- $_filter = $obj[1][1]['/Filter'];
- if ($_filter[0] == PDF_TYPE_TOKEN) {
- $filters[] = $_filter;
- } else if ($_filter[0] == PDF_TYPE_ARRAY) {
- $filters = $_filter[1];
- }
- }
- $stream = $obj[2][1];
- foreach ($filters AS $_filter) {
- switch ($_filter[1]) {
- case "/FlateDecode":
- if (function_exists('gzuncompress')) {
- $stream = (strlen($stream) > 0) ? @gzuncompress($stream) : '';
- } else {
- $this->fpdi->error(sprintf("To handle %s filter, please compile php with zlib support.",$_filter[1]));
- }
- if ($stream === false) {
- $this->fpdi->error("Error while decompressing stream.");
- }
- break;
-
- case '/LZWDecode':
- include_once(_MPDF_PATH.'mpdfi/filters/FilterLZW.php');
- $decoder =& new FilterLZW();
- $stream = $decoder->decode($stream);
- break;
- case '/ASCII85Decode':
- include_once(_MPDF_PATH.'mpdfi/filters/FilterASCII85.php');
- $decoder =& new FilterASCII85();
- $stream = $decoder->decode($stream);
- break;
- case null:
- $stream = $stream;
- break;
- default:
- $this->fpdi->error(sprintf("Unsupported Filter: %s",$_filter[1]));
- }
- }
- return $stream;
- }
-
- function getPageBox($page, $box_index) {
- $page = $this->pdf_resolve_object($this->c,$page);
- $box = null;
- if (isset($page[1][1][$box_index]))
- $box =& $page[1][1][$box_index];
- if (!is_null($box) && $box[0] == PDF_TYPE_OBJREF) {
- $tmp_box = $this->pdf_resolve_object($this->c,$box);
- $box = $tmp_box[1];
- }
- if (!is_null($box) && $box[0] == PDF_TYPE_ARRAY) {
- $b =& $box[1];
- return array("x" => $b[0][1]/$this->fpdi->k,
- "y" => $b[1][1]/$this->fpdi->k,
- "w" => abs($b[0][1]-$b[2][1])/$this->fpdi->k,
- "h" => abs($b[1][1]-$b[3][1])/$this->fpdi->k);
- } else if (!isset ($page[1][1]['/Parent'])) {
- return false;
- } else {
- return $this->getPageBox($this->pdf_resolve_object($this->c, $page[1][1]['/Parent']), $box_index);
- }
- }
- function getPageBoxes($pageno) {
- return $this->_getPageBoxes($this->pages[$pageno-1]);
- }
-
- function _getPageBoxes($page) {
- $boxes = array();
- foreach($this->availableBoxes AS $box) {
- if ($_box = $this->getPageBox($page,$box)) {
- $boxes[$box] = $_box;
- }
- }
- return $boxes;
- }
- function getPageRotation($pageno) {
- return $this->_getPageRotation($this->pages[$pageno-1]);
- }
- function _getPageRotation ($obj) {
- $obj = $this->pdf_resolve_object($this->c, $obj);
- if (isset ($obj[1][1]['/Rotate'])) {
- $res = $this->pdf_resolve_object($this->c, $obj[1][1]['/Rotate']);
- if ($res[0] == PDF_TYPE_OBJECT)
- return $res[1];
- return $res;
- } else {
- if (!isset ($obj[1][1]['/Parent'])) {
- return false;
- } else {
- $res = $this->_getPageRotation($obj[1][1]['/Parent']);
- if ($res[0] == PDF_TYPE_OBJECT)
- return $res[1];
- return $res;
- }
- }
- }
-
- function read_pages (&$c, &$pages, &$result) {
-
- $kids = $this->pdf_resolve_object ($c, $pages[1][1]['/Kids']);
- if (!is_array($kids)) {
-
- $this->success = false;
- $this->errormsg = sprintf("Cannot find /Kids in current /Page-Dictionary");
- return false;
- }
- foreach ($kids[1] as $v) {
- $pg = $this->pdf_resolve_object ($c, $v);
- if ($pg[1][1]['/Type'][1] === '/Pages') {
-
-
- $this->read_pages ($c, $pg, $result);
- } else {
- $result[] = $pg;
- }
- }
- }
- }
- ?>
|