fpdi_pdf_parser.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. <?php
  2. /**
  3. * This file is part of FPDI
  4. *
  5. * @package FPDI
  6. * @copyright Copyright (c) 2017 Setasign - Jan Slabon (http://www.setasign.com)
  7. * @license http://opensource.org/licenses/mit-license The MIT License
  8. * @version 1.6.2
  9. */
  10. if (!class_exists('pdf_parser')) {
  11. require_once('pdf_parser.php');
  12. }
  13. /**
  14. * Class fpdi_pdf_parser
  15. */
  16. class fpdi_pdf_parser extends pdf_parser
  17. {
  18. /**
  19. * Pages
  20. *
  21. * Index begins at 0
  22. *
  23. * @var array
  24. */
  25. protected $_pages;
  26. /**
  27. * Page count
  28. *
  29. * @var integer
  30. */
  31. protected $_pageCount;
  32. /**
  33. * Current page number
  34. *
  35. * @var integer
  36. */
  37. public $pageNo;
  38. /**
  39. * PDF version of imported document
  40. *
  41. * @var string
  42. */
  43. public $_pdfVersion;
  44. /**
  45. * Available BoxTypes
  46. *
  47. * @var array
  48. */
  49. public $availableBoxes = array('/MediaBox', '/CropBox', '/BleedBox', '/TrimBox', '/ArtBox');
  50. /**
  51. * The constructor.
  52. *
  53. * @param string $filename The source filename
  54. */
  55. public function __construct($filename)
  56. {
  57. parent::__construct($filename);
  58. // resolve Pages-Dictonary
  59. $pages = $this->resolveObject($this->_root[1][1]['/Pages']);
  60. // Read pages
  61. $this->_readPages($pages, $this->_pages);
  62. // count pages;
  63. $this->_pageCount = count($this->_pages);
  64. }
  65. /**
  66. * Get page count from source file.
  67. *
  68. * @return int
  69. */
  70. public function getPageCount()
  71. {
  72. return $this->_pageCount;
  73. }
  74. /**
  75. * Set the page number.
  76. *
  77. * @param int $pageNo Page number to use
  78. * @throws InvalidArgumentException
  79. */
  80. public function setPageNo($pageNo)
  81. {
  82. $pageNo = ((int) $pageNo) - 1;
  83. if ($pageNo < 0 || $pageNo >= $this->getPageCount()) {
  84. throw new InvalidArgumentException('Invalid page number!');
  85. }
  86. $this->pageNo = $pageNo;
  87. }
  88. /**
  89. * Get page-resources from current page
  90. *
  91. * @return array|boolean
  92. */
  93. public function getPageResources()
  94. {
  95. return $this->_getPageResources($this->_pages[$this->pageNo]);
  96. }
  97. /**
  98. * Get page-resources from a /Page dictionary.
  99. *
  100. * @param array $obj Array of pdf-data
  101. * @return array|boolean
  102. */
  103. protected function _getPageResources($obj)
  104. {
  105. $obj = $this->resolveObject($obj);
  106. // If the current object has a resources
  107. // dictionary associated with it, we use
  108. // it. Otherwise, we move back to its
  109. // parent object.
  110. if (isset($obj[1][1]['/Resources'])) {
  111. $res = $this->resolveObject($obj[1][1]['/Resources']);
  112. if ($res[0] == pdf_parser::TYPE_OBJECT)
  113. return $res[1];
  114. return $res;
  115. }
  116. if (!isset($obj[1][1]['/Parent'])) {
  117. return false;
  118. }
  119. $res = $this->_getPageResources($obj[1][1]['/Parent']);
  120. if ($res[0] == pdf_parser::TYPE_OBJECT)
  121. return $res[1];
  122. return $res;
  123. }
  124. /**
  125. * Get content of current page.
  126. *
  127. * If /Contents is an array, the streams are concatenated
  128. *
  129. * @return string
  130. */
  131. public function getContent()
  132. {
  133. $buffer = '';
  134. if (isset($this->_pages[$this->pageNo][1][1]['/Contents'])) {
  135. $contents = $this->_getPageContent($this->_pages[$this->pageNo][1][1]['/Contents']);
  136. foreach ($contents AS $tmpContent) {
  137. if ($tmpContent[0] !== pdf_parser::TYPE_STREAM) {
  138. continue;
  139. }
  140. $buffer .= $this->_unFilterStream($tmpContent) . ' ';
  141. }
  142. }
  143. return $buffer;
  144. }
  145. /**
  146. * Resolve all content objects.
  147. *
  148. * @param array $contentRef
  149. * @return array
  150. */
  151. protected function _getPageContent($contentRef)
  152. {
  153. $contents = array();
  154. if ($contentRef[0] == pdf_parser::TYPE_OBJREF) {
  155. $content = $this->resolveObject($contentRef);
  156. if ($content[1][0] == pdf_parser::TYPE_ARRAY) {
  157. $contents = $this->_getPageContent($content[1]);
  158. } else {
  159. $contents[] = $content;
  160. }
  161. } else if ($contentRef[0] == pdf_parser::TYPE_ARRAY) {
  162. foreach ($contentRef[1] AS $tmp_content_ref) {
  163. $contents = array_merge($contents, $this->_getPageContent($tmp_content_ref));
  164. }
  165. }
  166. return $contents;
  167. }
  168. /**
  169. * Get a boundary box from a page
  170. *
  171. * Array format is same as used by FPDF_TPL.
  172. *
  173. * @param array $page a /Page dictionary
  174. * @param string $boxIndex Type of box {see {@link $availableBoxes})
  175. * @param float Scale factor from user space units to points
  176. *
  177. * @return array|boolean
  178. */
  179. protected function _getPageBox($page, $boxIndex, $k)
  180. {
  181. $page = $this->resolveObject($page);
  182. $box = null;
  183. if (isset($page[1][1][$boxIndex])) {
  184. $box = $page[1][1][$boxIndex];
  185. }
  186. if (!is_null($box) && $box[0] == pdf_parser::TYPE_OBJREF) {
  187. $tmp_box = $this->resolveObject($box);
  188. $box = $tmp_box[1];
  189. }
  190. if (!is_null($box) && $box[0] == pdf_parser::TYPE_ARRAY) {
  191. $b = $box[1];
  192. return array(
  193. 'x' => $b[0][1] / $k,
  194. 'y' => $b[1][1] / $k,
  195. 'w' => abs($b[0][1] - $b[2][1]) / $k,
  196. 'h' => abs($b[1][1] - $b[3][1]) / $k,
  197. 'llx' => min($b[0][1], $b[2][1]) / $k,
  198. 'lly' => min($b[1][1], $b[3][1]) / $k,
  199. 'urx' => max($b[0][1], $b[2][1]) / $k,
  200. 'ury' => max($b[1][1], $b[3][1]) / $k,
  201. );
  202. } else if (!isset($page[1][1]['/Parent'])) {
  203. return false;
  204. } else {
  205. return $this->_getPageBox($this->resolveObject($page[1][1]['/Parent']), $boxIndex, $k);
  206. }
  207. }
  208. /**
  209. * Get all page boundary boxes by page number
  210. *
  211. * @param int $pageNo The page number
  212. * @param float $k Scale factor from user space units to points
  213. * @return array
  214. * @throws InvalidArgumentException
  215. */
  216. public function getPageBoxes($pageNo, $k)
  217. {
  218. if (!isset($this->_pages[$pageNo - 1])) {
  219. throw new InvalidArgumentException('Page ' . $pageNo . ' does not exists.');
  220. }
  221. return $this->_getPageBoxes($this->_pages[$pageNo - 1], $k);
  222. }
  223. /**
  224. * Get all boxes from /Page dictionary
  225. *
  226. * @param array $page A /Page dictionary
  227. * @param float $k Scale factor from user space units to points
  228. * @return array
  229. */
  230. protected function _getPageBoxes($page, $k)
  231. {
  232. $boxes = array();
  233. foreach($this->availableBoxes AS $box) {
  234. if ($_box = $this->_getPageBox($page, $box, $k)) {
  235. $boxes[$box] = $_box;
  236. }
  237. }
  238. return $boxes;
  239. }
  240. /**
  241. * Get the page rotation by page number
  242. *
  243. * @param integer $pageNo
  244. * @throws InvalidArgumentException
  245. * @return array
  246. */
  247. public function getPageRotation($pageNo)
  248. {
  249. if (!isset($this->_pages[$pageNo - 1])) {
  250. throw new InvalidArgumentException('Page ' . $pageNo . ' does not exists.');
  251. }
  252. return $this->_getPageRotation($this->_pages[$pageNo - 1]);
  253. }
  254. /**
  255. * Get the rotation value of a page
  256. *
  257. * @param array $obj A /Page dictionary
  258. * @return array|bool
  259. */
  260. protected function _getPageRotation($obj)
  261. {
  262. $obj = $this->resolveObject($obj);
  263. if (isset($obj[1][1]['/Rotate'])) {
  264. $res = $this->resolveObject($obj[1][1]['/Rotate']);
  265. if ($res[0] == pdf_parser::TYPE_OBJECT)
  266. return $res[1];
  267. return $res;
  268. }
  269. if (!isset($obj[1][1]['/Parent'])) {
  270. return false;
  271. }
  272. $res = $this->_getPageRotation($obj[1][1]['/Parent']);
  273. if ($res[0] == pdf_parser::TYPE_OBJECT)
  274. return $res[1];
  275. return $res;
  276. }
  277. /**
  278. * Read all pages
  279. *
  280. * @param array $pages /Pages dictionary
  281. * @param array $result The result array
  282. * @throws Exception
  283. */
  284. protected function _readPages(&$pages, &$result)
  285. {
  286. // Get the kids dictionary
  287. $_kids = $this->resolveObject($pages[1][1]['/Kids']);
  288. if (!is_array($_kids)) {
  289. throw new Exception('Cannot find /Kids in current /Page-Dictionary');
  290. }
  291. if ($_kids[0] === self::TYPE_OBJECT) {
  292. $_kids = $_kids[1];
  293. }
  294. $kids = $_kids[1];
  295. foreach ($kids as $v) {
  296. $pg = $this->resolveObject($v);
  297. if ($pg[0] !== pdf_parser::TYPE_OBJECT) {
  298. throw new Exception('Invalid data type in page tree.');
  299. }
  300. if ($pg[1][1]['/Type'][1] === '/Pages') {
  301. // If one of the kids is an embedded
  302. // /Pages array, resolve it as well.
  303. $this->_readPages($pg, $result);
  304. } else {
  305. $result[] = $pg;
  306. }
  307. }
  308. }
  309. }