| Total Complexity | 179 |
| Total Lines | 789 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| Bugs | 2 | Features | 1 |
Complex classes like RawDataParser often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use RawDataParser, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 45 | class RawDataParser |
||
| 46 | { |
||
| 47 | /** |
||
| 48 | * Configuration array. |
||
| 49 | */ |
||
| 50 | protected $cfg = [ |
||
| 51 | // if `true` ignore filter decoding errors |
||
| 52 | 'ignore_filter_decoding_errors' => true, |
||
| 53 | // if `true` ignore missing filter decoding errors |
||
| 54 | 'ignore_missing_filter_decoders' => true, |
||
| 55 | ]; |
||
| 56 | |||
| 57 | protected $filterHelper; |
||
| 58 | protected $objects; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @param array $cfg Configuration array, default is [] |
||
| 62 | */ |
||
| 63 | public function __construct($cfg = []) |
||
| 64 | { |
||
| 65 | // merge given array with default values |
||
| 66 | $this->cfg = array_merge($this->cfg, $cfg); |
||
| 67 | |||
| 68 | $this->filterHelper = new FilterHelper(); |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Decode the specified stream. |
||
| 73 | * |
||
| 74 | * @param string $pdfData PDF data |
||
| 75 | * @param array $xref |
||
| 76 | * @param array $sdic Stream's dictionary array |
||
| 77 | * @param string $stream Stream to decode |
||
| 78 | * |
||
| 79 | * @return array containing decoded stream data and remaining filters |
||
| 80 | */ |
||
| 81 | protected function decodeStream($pdfData, $xref, $sdic, $stream) |
||
| 82 | { |
||
| 83 | // get stream length and filters |
||
| 84 | $slength = \strlen($stream); |
||
| 85 | if ($slength <= 0) { |
||
| 86 | return ['', []]; |
||
| 87 | } |
||
| 88 | $filters = []; |
||
| 89 | foreach ($sdic as $k => $v) { |
||
| 90 | if ('/' == $v[0]) { |
||
| 91 | if (('Length' == $v[1]) and (isset($sdic[($k + 1)])) and ('numeric' == $sdic[($k + 1)][0])) { |
||
| 92 | // get declared stream length |
||
| 93 | $declength = (int) ($sdic[($k + 1)][1]); |
||
| 94 | if ($declength < $slength) { |
||
| 95 | $stream = substr($stream, 0, $declength); |
||
| 96 | $slength = $declength; |
||
| 97 | } |
||
| 98 | } elseif (('Filter' == $v[1]) and (isset($sdic[($k + 1)]))) { |
||
| 99 | // resolve indirect object |
||
| 100 | $objval = $this->getObjectVal($pdfData, $xref, $sdic[($k + 1)]); |
||
| 101 | if ('/' == $objval[0]) { |
||
| 102 | // single filter |
||
| 103 | $filters[] = $objval[1]; |
||
| 104 | } elseif ('[' == $objval[0]) { |
||
| 105 | // array of filters |
||
| 106 | foreach ($objval[1] as $flt) { |
||
| 107 | if ('/' == $flt[0]) { |
||
| 108 | $filters[] = $flt[1]; |
||
| 109 | } |
||
| 110 | } |
||
| 111 | } |
||
| 112 | } |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 116 | // decode the stream |
||
| 117 | $remaining_filters = []; |
||
| 118 | foreach ($filters as $filter) { |
||
| 119 | if (\in_array($filter, $this->filterHelper->getAvailableFilters())) { |
||
| 120 | try { |
||
| 121 | $stream = $this->filterHelper->decodeFilter($filter, $stream); |
||
| 122 | } catch (Exception $e) { |
||
| 123 | $emsg = $e->getMessage(); |
||
| 124 | if ((('~' == $emsg[0]) && !$this->cfg['ignore_missing_filter_decoders']) |
||
| 125 | || (('~' != $emsg[0]) && !$this->cfg['ignore_filter_decoding_errors']) |
||
| 126 | ) { |
||
| 127 | throw new Exception($e->getMessage()); |
||
| 128 | } |
||
| 129 | } |
||
| 130 | } else { |
||
| 131 | // add missing filter to array |
||
| 132 | $remaining_filters[] = $filter; |
||
| 133 | } |
||
| 134 | } |
||
| 135 | |||
| 136 | return [$stream, $remaining_filters]; |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Decode the Cross-Reference section |
||
| 141 | * |
||
| 142 | * @param string $pdfData PDF data |
||
| 143 | * @param int $startxref Offset at which the xref section starts (position of the 'xref' keyword) |
||
| 144 | * @param array $xref Previous xref array (if any) |
||
| 145 | * |
||
| 146 | * @return array containing xref and trailer data |
||
| 147 | */ |
||
| 148 | protected function decodeXref($pdfData, $startxref, $xref = []) |
||
| 149 | { |
||
| 150 | $startxref += 4; // 4 is the length of the word 'xref' |
||
| 151 | // skip initial white space chars: \x00 null (NUL), \x09 horizontal tab (HT), \x0A line feed (LF), \x0C form feed (FF), \x0D carriage return (CR), \x20 space (SP) |
||
| 152 | $offset = $startxref + strspn($pdfData, "\x00\x09\x0a\x0c\x0d\x20", $startxref); |
||
| 153 | // initialize object number |
||
| 154 | $obj_num = 0; |
||
| 155 | // search for cross-reference entries or subsection |
||
| 156 | while (preg_match('/([0-9]+)[\x20]([0-9]+)[\x20]?([nf]?)(\r\n|[\x20]?[\r\n])/', $pdfData, $matches, PREG_OFFSET_CAPTURE, $offset) > 0) { |
||
| 157 | if ($matches[0][1] != $offset) { |
||
| 158 | // we are on another section |
||
| 159 | break; |
||
| 160 | } |
||
| 161 | $offset += \strlen($matches[0][0]); |
||
| 162 | if ('n' == $matches[3][0]) { |
||
| 163 | // create unique object index: [object number]_[generation number] |
||
| 164 | $index = $obj_num.'_'.(int) ($matches[2][0]); |
||
| 165 | // check if object already exist |
||
| 166 | if (!isset($xref['xref'][$index])) { |
||
| 167 | // store object offset position |
||
| 168 | $xref['xref'][$index] = (int) ($matches[1][0]); |
||
| 169 | } |
||
| 170 | ++$obj_num; |
||
| 171 | } elseif ('f' == $matches[3][0]) { |
||
| 172 | ++$obj_num; |
||
| 173 | } else { |
||
| 174 | // object number (index) |
||
| 175 | $obj_num = (int) ($matches[1][0]); |
||
| 176 | } |
||
| 177 | } |
||
| 178 | // get trailer data |
||
| 179 | if (preg_match('/trailer[\s]*<<(.*)>>/isU', $pdfData, $matches, PREG_OFFSET_CAPTURE, $offset) > 0) { |
||
| 180 | $trailer_data = $matches[1][0]; |
||
| 181 | if (!isset($xref['trailer']) or empty($xref['trailer'])) { |
||
| 182 | // get only the last updated version |
||
| 183 | $xref['trailer'] = []; |
||
| 184 | // parse trailer_data |
||
| 185 | if (preg_match('/Size[\s]+([0-9]+)/i', $trailer_data, $matches) > 0) { |
||
| 186 | $xref['trailer']['size'] = (int) ($matches[1]); |
||
| 187 | } |
||
| 188 | if (preg_match('/Root[\s]+([0-9]+)[\s]+([0-9]+)[\s]+R/i', $trailer_data, $matches) > 0) { |
||
| 189 | $xref['trailer']['root'] = (int) ($matches[1]).'_'.(int) ($matches[2]); |
||
| 190 | } |
||
| 191 | if (preg_match('/Encrypt[\s]+([0-9]+)[\s]+([0-9]+)[\s]+R/i', $trailer_data, $matches) > 0) { |
||
| 192 | $xref['trailer']['encrypt'] = (int) ($matches[1]).'_'.(int) ($matches[2]); |
||
| 193 | } |
||
| 194 | if (preg_match('/Info[\s]+([0-9]+)[\s]+([0-9]+)[\s]+R/i', $trailer_data, $matches) > 0) { |
||
| 195 | $xref['trailer']['info'] = (int) ($matches[1]).'_'.(int) ($matches[2]); |
||
| 196 | } |
||
| 197 | if (preg_match('/ID[\s]*[\[][\s]*[<]([^>]*)[>][\s]*[<]([^>]*)[>]/i', $trailer_data, $matches) > 0) { |
||
| 198 | $xref['trailer']['id'] = []; |
||
| 199 | $xref['trailer']['id'][0] = $matches[1]; |
||
| 200 | $xref['trailer']['id'][1] = $matches[2]; |
||
| 201 | } |
||
| 202 | } |
||
| 203 | if (preg_match('/Prev[\s]+([0-9]+)/i', $trailer_data, $matches) > 0) { |
||
| 204 | // get previous xref |
||
| 205 | $xref = $this->getXrefData($pdfData, (int) ($matches[1]), $xref); |
||
| 206 | } |
||
| 207 | } else { |
||
| 208 | throw new Exception('Unable to find trailer'); |
||
| 209 | } |
||
| 210 | |||
| 211 | return $xref; |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Decode the Cross-Reference Stream section |
||
| 216 | * |
||
| 217 | * @param string $pdfData PDF data |
||
| 218 | * @param int $startxref Offset at which the xref section starts |
||
| 219 | * @param array $xref Previous xref array (if any) |
||
| 220 | * |
||
| 221 | * @return array containing xref and trailer data |
||
| 222 | * |
||
| 223 | * @throws Exception if unknown PNG predictor detected |
||
| 224 | */ |
||
| 225 | protected function decodeXrefStream($pdfData, $startxref, $xref = []) |
||
| 226 | { |
||
| 227 | // try to read Cross-Reference Stream |
||
| 228 | $xrefobj = $this->getRawObject($pdfData, $startxref); |
||
| 229 | $xrefcrs = $this->getIndirectObject($pdfData, $xref, $xrefobj[1], $startxref, true); |
||
| 230 | if (!isset($xref['trailer']) or empty($xref['trailer'])) { |
||
| 231 | // get only the last updated version |
||
| 232 | $xref['trailer'] = []; |
||
| 233 | $filltrailer = true; |
||
| 234 | } else { |
||
| 235 | $filltrailer = false; |
||
| 236 | } |
||
| 237 | if (!isset($xref['xref'])) { |
||
| 238 | $xref['xref'] = []; |
||
| 239 | } |
||
| 240 | $valid_crs = false; |
||
| 241 | $columns = 0; |
||
| 242 | $sarr = $xrefcrs[0][1]; |
||
| 243 | if (!\is_array($sarr)) { |
||
| 244 | $sarr = []; |
||
| 245 | } |
||
| 246 | |||
| 247 | $wb = []; |
||
| 248 | |||
| 249 | foreach ($sarr as $k => $v) { |
||
| 250 | if ( |
||
| 251 | ('/' == $v[0]) |
||
| 252 | && ('Type' == $v[1]) |
||
| 253 | && ( |
||
| 254 | isset($sarr[($k + 1)]) |
||
| 255 | && '/' == $sarr[($k + 1)][0] |
||
| 256 | && 'XRef' == $sarr[($k + 1)][1] |
||
| 257 | ) |
||
| 258 | ) { |
||
| 259 | $valid_crs = true; |
||
| 260 | } elseif (('/' == $v[0]) and ('Index' == $v[1]) and (isset($sarr[($k + 1)]))) { |
||
| 261 | // first object number in the subsection |
||
| 262 | $index_first = (int) ($sarr[($k + 1)][1][0][1]); |
||
| 263 | } elseif (('/' == $v[0]) and ('Prev' == $v[1]) and (isset($sarr[($k + 1)]) and ('numeric' == $sarr[($k + 1)][0]))) { |
||
| 264 | // get previous xref offset |
||
| 265 | $prevxref = (int) ($sarr[($k + 1)][1]); |
||
| 266 | } elseif (('/' == $v[0]) and ('W' == $v[1]) and (isset($sarr[($k + 1)]))) { |
||
| 267 | // number of bytes (in the decoded stream) of the corresponding field |
||
| 268 | $wb[0] = (int) ($sarr[($k + 1)][1][0][1]); |
||
| 269 | $wb[1] = (int) ($sarr[($k + 1)][1][1][1]); |
||
| 270 | $wb[2] = (int) ($sarr[($k + 1)][1][2][1]); |
||
| 271 | } elseif (('/' == $v[0]) and ('DecodeParms' == $v[1]) and (isset($sarr[($k + 1)][1]))) { |
||
| 272 | $decpar = $sarr[($k + 1)][1]; |
||
| 273 | foreach ($decpar as $kdc => $vdc) { |
||
| 274 | if ( |
||
| 275 | '/' == $vdc[0] |
||
| 276 | && 'Columns' == $vdc[1] |
||
| 277 | && ( |
||
| 278 | isset($decpar[($kdc + 1)]) |
||
| 279 | && 'numeric' == $decpar[($kdc + 1)][0] |
||
| 280 | ) |
||
| 281 | ) { |
||
| 282 | $columns = (int) ($decpar[($kdc + 1)][1]); |
||
| 283 | } elseif ( |
||
| 284 | '/' == $vdc[0] |
||
| 285 | && 'Predictor' == $vdc[1] |
||
| 286 | && ( |
||
| 287 | isset($decpar[($kdc + 1)]) |
||
| 288 | && 'numeric' == $decpar[($kdc + 1)][0] |
||
| 289 | ) |
||
| 290 | ) { |
||
| 291 | $predictor = (int) ($decpar[($kdc + 1)][1]); |
||
|
|
|||
| 292 | } |
||
| 293 | } |
||
| 294 | } elseif ($filltrailer) { |
||
| 295 | if (('/' == $v[0]) and ('Size' == $v[1]) and (isset($sarr[($k + 1)]) and ('numeric' == $sarr[($k + 1)][0]))) { |
||
| 296 | $xref['trailer']['size'] = $sarr[($k + 1)][1]; |
||
| 297 | } elseif (('/' == $v[0]) and ('Root' == $v[1]) and (isset($sarr[($k + 1)]) and ('objref' == $sarr[($k + 1)][0]))) { |
||
| 298 | $xref['trailer']['root'] = $sarr[($k + 1)][1]; |
||
| 299 | } elseif (('/' == $v[0]) and ('Info' == $v[1]) and (isset($sarr[($k + 1)]) and ('objref' == $sarr[($k + 1)][0]))) { |
||
| 300 | $xref['trailer']['info'] = $sarr[($k + 1)][1]; |
||
| 301 | } elseif (('/' == $v[0]) and ('Encrypt' == $v[1]) and (isset($sarr[($k + 1)]) and ('objref' == $sarr[($k + 1)][0]))) { |
||
| 302 | $xref['trailer']['encrypt'] = $sarr[($k + 1)][1]; |
||
| 303 | } elseif (('/' == $v[0]) and ('ID' == $v[1]) and (isset($sarr[($k + 1)]))) { |
||
| 304 | $xref['trailer']['id'] = []; |
||
| 305 | $xref['trailer']['id'][0] = $sarr[($k + 1)][1][0][1]; |
||
| 306 | $xref['trailer']['id'][1] = $sarr[($k + 1)][1][1][1]; |
||
| 307 | } |
||
| 308 | } |
||
| 309 | } |
||
| 310 | |||
| 311 | // decode data |
||
| 312 | if ($valid_crs and isset($xrefcrs[1][3][0])) { |
||
| 313 | // number of bytes in a row |
||
| 314 | $rowlen = ($columns + 1); |
||
| 315 | // convert the stream into an array of integers |
||
| 316 | $sdata = unpack('C*', $xrefcrs[1][3][0]); |
||
| 317 | // split the rows |
||
| 318 | $sdata = array_chunk($sdata, $rowlen); |
||
| 319 | // initialize decoded array |
||
| 320 | $ddata = []; |
||
| 321 | // initialize first row with zeros |
||
| 322 | $prev_row = array_fill(0, $rowlen, 0); |
||
| 323 | // for each row apply PNG unpredictor |
||
| 324 | foreach ($sdata as $k => $row) { |
||
| 325 | // initialize new row |
||
| 326 | $ddata[$k] = []; |
||
| 327 | // get PNG predictor value |
||
| 328 | $predictor = (10 + $row[0]); |
||
| 329 | // for each byte on the row |
||
| 330 | for ($i = 1; $i <= $columns; ++$i) { |
||
| 331 | // new index |
||
| 332 | $j = ($i - 1); |
||
| 333 | $row_up = $prev_row[$j]; |
||
| 334 | if (1 == $i) { |
||
| 335 | $row_left = 0; |
||
| 336 | $row_upleft = 0; |
||
| 337 | } else { |
||
| 338 | $row_left = $row[($i - 1)]; |
||
| 339 | $row_upleft = $prev_row[($j - 1)]; |
||
| 340 | } |
||
| 341 | switch ($predictor) { |
||
| 342 | case 10: // PNG prediction (on encoding, PNG None on all rows) |
||
| 343 | $ddata[$k][$j] = $row[$i]; |
||
| 344 | break; |
||
| 345 | |||
| 346 | case 11: // PNG prediction (on encoding, PNG Sub on all rows) |
||
| 347 | $ddata[$k][$j] = (($row[$i] + $row_left) & 0xff); |
||
| 348 | break; |
||
| 349 | |||
| 350 | case 12: // PNG prediction (on encoding, PNG Up on all rows) |
||
| 351 | $ddata[$k][$j] = (($row[$i] + $row_up) & 0xff); |
||
| 352 | break; |
||
| 353 | |||
| 354 | case 13: // PNG prediction (on encoding, PNG Average on all rows) |
||
| 355 | $ddata[$k][$j] = (($row[$i] + (($row_left + $row_up) / 2)) & 0xff); |
||
| 356 | break; |
||
| 357 | |||
| 358 | case 14: // PNG prediction (on encoding, PNG Paeth on all rows) |
||
| 359 | // initial estimate |
||
| 360 | $p = ($row_left + $row_up - $row_upleft); |
||
| 361 | // distances |
||
| 362 | $pa = abs($p - $row_left); |
||
| 363 | $pb = abs($p - $row_up); |
||
| 364 | $pc = abs($p - $row_upleft); |
||
| 365 | $pmin = min($pa, $pb, $pc); |
||
| 366 | // return minimum distance |
||
| 367 | switch ($pmin) { |
||
| 368 | case $pa: |
||
| 369 | $ddata[$k][$j] = (($row[$i] + $row_left) & 0xff); |
||
| 370 | break; |
||
| 371 | |||
| 372 | case $pb: |
||
| 373 | $ddata[$k][$j] = (($row[$i] + $row_up) & 0xff); |
||
| 374 | break; |
||
| 375 | |||
| 376 | case $pc: |
||
| 377 | $ddata[$k][$j] = (($row[$i] + $row_upleft) & 0xff); |
||
| 378 | break; |
||
| 379 | } |
||
| 380 | break; |
||
| 381 | |||
| 382 | default: // PNG prediction (on encoding, PNG optimum) |
||
| 383 | throw new Exception('Unknown PNG predictor'); |
||
| 384 | } |
||
| 385 | } |
||
| 386 | $prev_row = $ddata[$k]; |
||
| 387 | } // end for each row |
||
| 388 | // complete decoding |
||
| 389 | $sdata = []; |
||
| 390 | // for every row |
||
| 391 | foreach ($ddata as $k => $row) { |
||
| 392 | // initialize new row |
||
| 393 | $sdata[$k] = [0, 0, 0]; |
||
| 394 | if (0 == $wb[0]) { |
||
| 395 | // default type field |
||
| 396 | $sdata[$k][0] = 1; |
||
| 397 | } |
||
| 398 | $i = 0; // count bytes in the row |
||
| 399 | // for every column |
||
| 400 | for ($c = 0; $c < 3; ++$c) { |
||
| 401 | // for every byte on the column |
||
| 402 | for ($b = 0; $b < $wb[$c]; ++$b) { |
||
| 403 | if (isset($row[$i])) { |
||
| 404 | $sdata[$k][$c] += ($row[$i] << (($wb[$c] - 1 - $b) * 8)); |
||
| 405 | } |
||
| 406 | ++$i; |
||
| 407 | } |
||
| 408 | } |
||
| 409 | } |
||
| 410 | $ddata = []; |
||
| 411 | // fill xref |
||
| 412 | if (isset($index_first)) { |
||
| 413 | $obj_num = $index_first; |
||
| 414 | } else { |
||
| 415 | $obj_num = 0; |
||
| 416 | } |
||
| 417 | foreach ($sdata as $k => $row) { |
||
| 418 | switch ($row[0]) { |
||
| 419 | case 0: // (f) linked list of free objects |
||
| 420 | break; |
||
| 421 | |||
| 422 | case 1: // (n) objects that are in use but are not compressed |
||
| 423 | // create unique object index: [object number]_[generation number] |
||
| 424 | $index = $obj_num.'_'.$row[2]; |
||
| 425 | // check if object already exist |
||
| 426 | if (!isset($xref['xref'][$index])) { |
||
| 427 | // store object offset position |
||
| 428 | $xref['xref'][$index] = $row[1]; |
||
| 429 | } |
||
| 430 | break; |
||
| 431 | |||
| 432 | case 2: // compressed objects |
||
| 433 | // $row[1] = object number of the object stream in which this object is stored |
||
| 434 | // $row[2] = index of this object within the object stream |
||
| 435 | $index = $row[1].'_0_'.$row[2]; |
||
| 436 | $xref['xref'][$index] = -1; |
||
| 437 | break; |
||
| 438 | |||
| 439 | default: // null objects |
||
| 440 | break; |
||
| 441 | } |
||
| 442 | ++$obj_num; |
||
| 443 | } |
||
| 444 | } // end decoding data |
||
| 445 | if (isset($prevxref)) { |
||
| 446 | // get previous xref |
||
| 447 | $xref = $this->getXrefData($pdfData, $prevxref, $xref); |
||
| 448 | } |
||
| 449 | |||
| 450 | return $xref; |
||
| 451 | } |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Get content of indirect object. |
||
| 455 | * |
||
| 456 | * @param string $pdfData PDF data |
||
| 457 | * @param array $xref |
||
| 458 | * @param string $obj_ref Object number and generation number separated by underscore character |
||
| 459 | * @param int $offset Object offset |
||
| 460 | * @param bool $decoding If true decode streams |
||
| 461 | * |
||
| 462 | * @return array containing object data |
||
| 463 | * |
||
| 464 | * @throws Exception if invalid object reference found |
||
| 465 | */ |
||
| 466 | protected function getIndirectObject($pdfData, $xref, $obj_ref, $offset = 0, $decoding = true) |
||
| 467 | { |
||
| 468 | $obj = explode('_', $obj_ref); |
||
| 469 | if (2 != \count($obj)) { |
||
| 470 | throw new Exception('Invalid object reference for $obj.'); |
||
| 471 | } |
||
| 472 | $objref = $obj[0].' '.$obj[1].' obj'; |
||
| 473 | // ignore leading zeros |
||
| 474 | $offset += strspn($pdfData, '0', $offset); |
||
| 475 | if (strpos($pdfData, $objref, $offset) != $offset) { |
||
| 476 | // an indirect reference to an undefined object shall be considered a reference to the null object |
||
| 477 | return ['null', 'null', $offset]; |
||
| 478 | } |
||
| 479 | // starting position of object content |
||
| 480 | $offset += \strlen($objref); |
||
| 481 | // get array of object content |
||
| 482 | $objdata = []; |
||
| 483 | $i = 0; // object main index |
||
| 484 | do { |
||
| 485 | $oldoffset = $offset; |
||
| 486 | // get element |
||
| 487 | $element = $this->getRawObject($pdfData, $offset); |
||
| 488 | $offset = $element[2]; |
||
| 489 | // decode stream using stream's dictionary information |
||
| 490 | if ($decoding and ('stream' == $element[0]) and (isset($objdata[($i - 1)][0])) and ('<<' == $objdata[($i - 1)][0])) { |
||
| 491 | $element[3] = $this->decodeStream($pdfData, $xref, $objdata[($i - 1)][1], $element[1]); |
||
| 492 | } |
||
| 493 | $objdata[$i] = $element; |
||
| 494 | ++$i; |
||
| 495 | } while (('endobj' != $element[0]) and ($offset != $oldoffset)); |
||
| 496 | |||
| 497 | // remove closing delimiter |
||
| 498 | array_pop($objdata); |
||
| 499 | |||
| 500 | // return raw object content |
||
| 501 | return $objdata; |
||
| 502 | } |
||
| 503 | |||
| 504 | /** |
||
| 505 | * Get the content of object, resolving indect object reference if necessary. |
||
| 506 | * |
||
| 507 | * @param string $pdfData PDF data |
||
| 508 | * @param array $obj Object value |
||
| 509 | * |
||
| 510 | * @return array containing object data |
||
| 511 | */ |
||
| 512 | protected function getObjectVal($pdfData, $xref, $obj) |
||
| 513 | { |
||
| 514 | if ('objref' == $obj[0]) { |
||
| 515 | // reference to indirect object |
||
| 516 | if (isset($this->objects[$obj[1]])) { |
||
| 517 | // this object has been already parsed |
||
| 518 | return $this->objects[$obj[1]]; |
||
| 519 | } elseif (isset($xref[$obj[1]])) { |
||
| 520 | // parse new object |
||
| 521 | $this->objects[$obj[1]] = $this->getIndirectObject($pdfData, $xref, $obj[1], $xref[$obj[1]], false); |
||
| 522 | |||
| 523 | return $this->objects[$obj[1]]; |
||
| 524 | } |
||
| 525 | } |
||
| 526 | |||
| 527 | return $obj; |
||
| 528 | } |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Get object type, raw value and offset to next object |
||
| 532 | * |
||
| 533 | * @param int $offset Object offset |
||
| 534 | * |
||
| 535 | * @return array containing object type, raw value and offset to next object |
||
| 536 | */ |
||
| 537 | protected function getRawObject($pdfData, $offset = 0) |
||
| 733 | } |
||
| 734 | |||
| 735 | /** |
||
| 736 | * Get Cross-Reference (xref) table and trailer data from PDF document data. |
||
| 737 | * |
||
| 738 | * @param string $pdfData |
||
| 739 | * @param int $offset xref offset (if know) |
||
| 740 | * @param array $xref previous xref array (if any) |
||
| 741 | * |
||
| 742 | * @return array containing xref and trailer data |
||
| 743 | * |
||
| 744 | * @throws Exception if it was unable to find startxref |
||
| 745 | * @throws Exception if it was unable to find xref |
||
| 746 | */ |
||
| 747 | protected function getXrefData($pdfData, $offset = 0, $xref = []) |
||
| 796 | } |
||
| 797 | |||
| 798 | /** |
||
| 799 | * Parses PDF data and returns extracted data as array. |
||
| 800 | * |
||
| 801 | * @param string $data PDF data to parse |
||
| 802 | * |
||
| 803 | * @return array array of parsed PDF document objects |
||
| 804 | * |
||
| 805 | * @throws Exception if empty PDF data given |
||
| 806 | * @throws Exception if PDF data missing %PDF header |
||
| 807 | */ |
||
| 808 | public function parseData($data) |
||
| 834 | } |
||
| 835 | } |
||
| 836 |