Example #1
0
 /**
  * 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;
 }
Example #2
0
 /**
  * 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;
 }
Example #3
0
 /**
  * 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;
 }
Example #4
0
 /**
  * Parse a page object from a string
  *
  * @param  string $stream
  * @return PageObject
  */
 public static function parse($stream)
 {
     $page = new self();
     $page->setIndex(substr($stream, 0, strpos($stream, ' ')));
     // Determine the page parent object index.
     $parent = substr($stream, strpos($stream, '/Parent') + 7);
     $parent = trim(substr($parent, 0, strpos($parent, '0 R')));
     $page->setParentIndex($parent);
     // Determine the page width and height.
     $dims = substr($stream, strpos($stream, '/MediaBox') + 9);
     $dims = substr($dims, 0, strpos($dims, ']'));
     $dims = trim(str_replace('[', '', $dims));
     $dims = explode(' ', $dims);
     $page->setWidth($dims[2]);
     $page->setHeight($dims[3]);
     // Determine the page content objects.
     if (strpos($stream, '/Contents') !== false) {
         $contents = substr($stream, strpos($stream, '/Contents') + 9);
         $contents = $page->getDictionaryReferences($contents);
         foreach ($contents as $content) {
             $page->addContentIndex($content);
         }
         // Set placeholder
         $contents = substr($stream, strpos($stream, '/Contents') + 9);
         if (strpos($contents, '[') !== false) {
             $contents = substr($contents, 0, strpos($contents, ']') + 1);
             $stream = str_replace($contents, '[{content_objects}]', $stream);
         } else {
             $contents = strpos($contents, '/') !== false ? substr($contents, 0, strpos($contents, '/')) : substr($contents, 0, strpos($contents, '>'));
             $stream = str_replace($contents, '[{content_objects}]', $stream);
         }
     }
     // If they exist, determine the page annotation objects.
     if (strpos($stream, '/Annots') !== false) {
         $annots = substr($stream, strpos($stream, '/Annots') + 7);
         $annots = $page->getDictionaryReferences($annots);
         foreach ($annots as $annot) {
             $page->addAnnotIndex($annot);
         }
         // Set placeholder
         $annots = substr($stream, strpos($stream, '/Annots') + 7);
         if (strpos($annots, '[') !== false) {
             $annots = substr($annots, 0, strpos($annots, ']') + 1);
             $stream = str_replace($annots, '[{annotations}]', $stream);
         } else {
             $annots = strpos($annots, '/') !== false ? substr($annots, 0, strpos($annots, '/')) : substr($annots, 0, strpos($annots, '>'));
             $stream = str_replace($annots, '[{annotations}]', $stream);
         }
     }
     // If they exist, determine the page font references.
     if (strpos($stream, '/Font') !== false) {
         $fonts = substr($stream, strpos($stream, 'Font'));
         $fonts = substr($fonts, 0, strpos($fonts, '>>') + 2);
         $stream = str_replace('/' . $fonts, '[{fonts}]', $stream);
         $fonts = str_replace('Font<<', '', $fonts);
         $fonts = str_replace('>>', '', $fonts);
         $fonts = explode('/', $fonts);
         foreach ($fonts as $value) {
             if ($value != '') {
                 $page->addFontReference('/' . $value);
             }
         }
     }
     // If they exist, determine the page XObjects references.
     if (strpos($stream, '/XObject') !== false) {
         $xo = substr($stream, strpos($stream, 'XObject'));
         $xo = substr($xo, 0, strpos($xo, '>>') + 2);
         $stream = str_replace('/' . $xo, '[{xobjects}]', $stream);
         $xo = str_replace('XObject<<', '', $xo);
         $xo = str_replace('>>', '', $xo);
         $xo = explode('/', $xo);
         foreach ($xo as $value) {
             if ($value != '') {
                 $page->addXObjectReference('/' . $value);
             }
         }
     }
     // If they exist, determine the page graphic states.
     if (strpos($stream, '/ExtGState') !== false) {
         $gState = substr($stream, strpos($stream, 'ExtGState'));
         $gState = '/' . substr($gState, 0, strpos($gState, '>>') + 2);
     } else {
         $gState = '';
     }
     // If any groups exist
     if (strpos($stream, '/Group') !== false) {
         $group = substr($stream, strpos($stream, 'Group'));
         $group = '/' . substr($group, 0, strpos($group, '>>') + 2);
     } else {
         $group = '';
     }
     // If resources exists
     if (strpos($stream, '/Resources') !== false) {
         $resources = substr($stream, strpos($stream, 'Resources'));
         if (strpos($resources, ' R') !== false) {
             $resources = '/' . substr($resources, 0, strpos($resources, ' R') + 2);
         } else {
             if (strpos($resources, '>>') !== false) {
                 $resources = '/' . substr($resources, 0, strpos($resources, '>>') + 2);
             } else {
                 $resources = "/Resources<</ProcSet[/PDF/Text/ImageB/ImageC/ImageI][{xobjects}][{fonts}]{$gState}>>";
             }
         }
     } else {
         $resources = "/Resources<</ProcSet[/PDF/Text/ImageB/ImageC/ImageI][{xobjects}][{fonts}]{$gState}>>";
     }
     if (substr_count($resources, '<<') > substr_count($resources, '>>')) {
         $resources .= str_repeat('>>', substr_count($resources, '<<') - substr_count($resources, '>>'));
     }
     $page->setData("\n[{page_index}] 0 obj\n<</Type/Page/Parent [{parent}] 0 R[{annotations}]/MediaBox" . "[0 0 [{width}] [{height}]]{$group}[{content_objects}]{$resources}>>\nendobj\n");
     return $page;
 }
Example #5
0
 /**
  * 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;
 }
Example #6
0
 public function createTask()
 {
     $newTask = new self();
     $newTask->setIndex($this->getTaskCount());
     $this->taskCollection[] = $newTask;
     $this->activeTaskIndex = $this->getTaskCount() - 1;
     return $newTask;
 }