/** * Parse a parent object from a string * * @param string $stream * @return ParentObject */ public static function parse($stream) { $parent = new self(); $parent->setIndex(substr($stream, 0, strpos($stream, ' '))); $stream = str_replace($parent->getIndex() . ' 0 obj', '[{parent_index}] 0 obj', $stream); // Determine the kids count. $matches = []; preg_match('/\\/Count\\s\\d*/', $stream, $matches); $count = $matches[0]; $count = str_replace('/Count ', '', $count); $stream = str_replace('Count ' . $count, 'Count [{count}]', $stream); // Determine the kids object indices. $kids = trim(substr($stream, strpos($stream, '/Kids') + 5)); $kids = substr($kids, 0, 1) == '[' ? substr($kids, 0, strpos($kids, ']') + 1) : substr($kids, 0, strpos($kids, ' R') + 2); $kidIndices = $parent->getDictionaryReferences(substr($stream, strpos($stream, '/Kids') + 5)); $parent->setKids($kidIndices); $parent->setData(str_replace($kids, '[[{kids}]]', $stream) . "\n"); return $parent; }
/** * Parse a stream object from a string * * @param string $stream * @return StreamObject */ public static function parse($stream) { $object = new self(); $object->setIndex(substr($stream, 0, strpos($stream, ' '))); $stream = str_replace($object->getIndex() . ' 0 obj', '[{object_index}] 0 obj', $stream); // Determine the objects definition and stream, if applicable. $s = substr($stream, strpos($stream, ' obj') + 4); $s = substr($s, 0, strpos($s, 'endobj')); if (strpos($s, 'stream') !== false) { $def = substr($s, 0, strpos($s, 'stream')); $str = substr($s, strpos($s, 'stream') + 6); $str = substr($str, 0, strpos($str, 'endstream')); $object->setDefinition($def); $object->appendStream($str); } else { $object->setDefinition($s); } $object->setData("\n[{object_index}] 0 obj\n[{definition}]\n[{stream}]\nendobj\n\n"); return $object; }
/** * Parse a root object from a string * * @param string $stream * @return RootObject */ public static function parse($stream) { $root = new self(); // Else, parse out any metadata and determine the root and parent object indices. $root->setIndex(substr($stream, 0, strpos($stream, ' '))); $stream = str_replace($root->getIndex() . ' 0 obj', '[{root_index}] 0 obj', $stream); // Strip away any metadata if (strpos($stream, '/Metadata') !== false) { $metadata = substr($stream, strpos($stream, 'Metadata')); $metadata = '/' . substr($metadata, 0, strpos($metadata, '/')); $stream = str_replace($metadata, '', $stream); } // Determine the parent index. $parent = substr($stream, strpos($stream, '/Pages') + 6); $parent = trim(substr($parent, 0, strpos($parent, '0 R'))); $root->setParentIndex($parent); $stream = str_replace('Pages ' . $root->getParentIndex() . ' 0 R', 'Pages [{parent_index}] 0 R', $stream); // Set the root object parent index and the data. $root->setData($stream . "\n"); return $root; }
/** * Parse a info object from a string * * @param string $stream * @return InfoObject */ public static function parse($stream) { $info = new self(); $info->setIndex(substr($stream, 0, strpos($stream, ' '))); $stream = str_replace($info->getIndex() . ' 0 obj', '[{info_index}] 0 obj', $stream); // Determine the Creator if (strpos($stream, '/Creator') !== false) { $creator = substr($stream, strpos($stream, '/Creator')); $creator = substr($creator, strpos($creator, '(')); $creator = substr($creator, 0, strpos($creator, ')')); $creator = str_replace('(', '', $creator); $stream = str_replace('Creator(' . $creator . ')', 'Creator([{creator}])', $stream); $info->getMetadata()->setCreator($creator); } else { $stream = str_replace('>>', '/Creator([{creator}])>>', $stream); } // Determine the CreationDate if (strpos($stream, '/CreationDate') !== false) { $creationDate = substr($stream, strpos($stream, '/CreationDate')); $creationDate = substr($creationDate, strpos($creationDate, '(')); $creationDate = substr($creationDate, 0, strpos($creationDate, ')')); $creationDate = str_replace('(', '', $creationDate); $stream = str_replace('CreationDate(' . $creationDate . ')', 'CreationDate([{creation_date}])', $stream); $info->getMetadata()->setCreationDate($creationDate); } else { $stream = str_replace('>>', '/CreationDate([{creation_date}])>>', $stream); } // Determine the ModDate if (strpos($stream, '/ModDate') !== false) { $modDate = substr($stream, strpos($stream, '/ModDate')); $modDate = substr($modDate, strpos($modDate, '(')); $modDate = substr($modDate, 0, strpos($modDate, ')')); $modDate = str_replace('(', '', $modDate); $stream = str_replace('ModDate(' . $modDate . ')', 'ModDate([{mod_date}])', $stream); $info->getMetadata()->setModDate($modDate); } else { $stream = str_replace('>>', '/ModDate([{mod_date}])>>', $stream); } // Determine the Author if (strpos($stream, '/Author') !== false) { $author = substr($stream, strpos($stream, '/Author')); $author = substr($author, strpos($author, '(')); $author = substr($author, 0, strpos($author, ')')); $author = str_replace('(', '', $author); $stream = str_replace('Author(' . $author . ')', 'Author([{author}])', $stream); $info->getMetadata()->setAuthor($author); } else { $stream = str_replace('>>', '/Author([{author}])>>', $stream); } // Determine the Title if (strpos($stream, '/Title') !== false) { $title = substr($stream, strpos($stream, '/Title')); $title = substr($title, strpos($title, '(')); $title = substr($title, 0, strpos($title, ')')); $title = str_replace('(', '', $title); $stream = str_replace('Title(' . $title . ')', 'Title([{title}])', $stream); $info->getMetadata()->setTitle($title); } else { $stream = str_replace('>>', '/Title([{title}])>>', $stream); } // Determine the Subject if (strpos($stream, '/Subject') !== false) { $subject = substr($stream, strpos($stream, '/Subject')); $subject = substr($subject, strpos($subject, '(')); $subject = substr($subject, 0, strpos($subject, ')')); $subject = str_replace('(', '', $subject); $stream = str_replace('Subject(' . $subject . ')', 'Subject([{subject}])', $stream); $info->getMetadata()->setSubject($subject); } else { $stream = str_replace('>>', '/Subject([{subject}])>>', $stream); } // Determine the Producer if (strpos($stream, '/Producer') !== false) { $producer = substr($stream, strpos($stream, '/Producer')); $producer = substr($producer, strpos($producer, '(')); $producer = substr($producer, 0, strpos($producer, ')')); $producer = str_replace('(', '', $producer); $stream = str_replace('Producer(' . $producer . ')', 'Producer([{producer}])', $stream); $info->getMetadata()->setProducer($producer); } else { $stream = str_replace('>>', '/Producer([{producer}])>>', $stream); } $info->setData($stream); return $info; }