Пример #1
0
 /**
  * Load pages recursively
  *
  * @param Zend_Pdf_Element_Reference $pages
  * @param array|null $attributes
  */
 protected function _loadPages(Zend_Pdf_Element_Reference $pages, $attributes = array())
 {
     if ($pages->getType() != Zend_Pdf_Element::TYPE_DICTIONARY) {
         throw new Zend_Pdf_Exception('Wrong argument');
     }
     foreach ($pages->getKeys() as $property) {
         if (in_array($property, self::$_inheritableAttributes)) {
             $attributes[$property] = $pages->{$property};
             $pages->{$property} = null;
         }
     }
     foreach ($pages->Kids->items as $child) {
         if ($child->Type->value == 'Pages') {
             $this->_loadPages($child, $attributes);
         } else {
             if ($child->Type->value == 'Page') {
                 foreach (self::$_inheritableAttributes as $property) {
                     if ($child->{$property} === null && array_key_exists($property, $attributes)) {
                         /**
                          * Important note.
                          * If any attribute or dependant object is an indirect object, then it's still
                          * shared between pages.
                          */
                         if ($attributes[$property] instanceof Zend_Pdf_Element_Object || $attributes[$property] instanceof Zend_Pdf_Element_Reference) {
                             $child->{$property} = $attributes[$property];
                         } else {
                             $child->{$property} = $this->_objFactory->newObject($attributes[$property]);
                         }
                     }
                 }
                 $this->pages[] = new Core_Pdf_Page($child, $this->_objFactory);
             }
         }
     }
 }
Пример #2
0
 /**
  * Dump Action and its child actions into PDF structures
  *
  * Returns dictionary indirect object or reference
  *
  * @internal
  * @param Zend_Pdf_ElementFactory $factory   Object factory for newly created indirect objects
  * @param SplObjectStorage $processedActions  list of already processed actions (used to prevent infinity loop caused by cyclic references)
  * @return Zend_Pdf_Element_Object|Zend_Pdf_Element_Reference   Dictionary indirect object
  */
 public function dumpAction(Zend_Pdf_ElementFactory_Interface $factory, SplObjectStorage $processedActions = null)
 {
     if ($processedActions === null) {
         $processedActions = new SplObjectStorage();
     }
     if ($processedActions->contains($this)) {
         require_once 'Zend/Pdf/Exception.php';
         throw new Zend_Pdf_Exception('Action chain cyclyc reference is detected.');
     }
     $processedActions->attach($this);
     $childListUpdated = false;
     if (count($this->_originalNextList) != count($this->next)) {
         // If original and current children arrays have different size then children list was updated
         $childListUpdated = true;
     } else {
         if (!(array_keys($this->_originalNextList) === array_keys($this->next))) {
             // If original and current children arrays have different keys (with a glance to an order) then children list was updated
             $childListUpdated = true;
         } else {
             foreach ($this->next as $key => $childAction) {
                 if ($this->_originalNextList[$key] !== $childAction) {
                     $childListUpdated = true;
                     break;
                 }
             }
         }
     }
     if ($childListUpdated) {
         $this->_actionDictionary->touch();
         switch (count($this->next)) {
             case 0:
                 $this->_actionDictionary->Next = null;
                 break;
             case 1:
                 $child = reset($this->next);
                 $this->_actionDictionary->Next = $child->dumpAction($factory, $processedActions);
                 break;
             default:
                 require_once 'Zend/Pdf/Element/Array.php';
                 $pdfChildArray = new Zend_Pdf_Element_Array();
                 foreach ($this->next as $child) {
                     $pdfChildArray->items[] = $child->dumpAction($factory, $processedActions);
                 }
                 $this->_actionDictionary->Next = $pdfChildArray;
                 break;
         }
     } else {
         foreach ($this->next as $child) {
             $child->dumpAction($factory, $processedActions);
         }
     }
     if ($this->_actionDictionary instanceof Zend_Pdf_Element_Dictionary) {
         // It's a newly created action. Register it within object factory and return indirect object
         return $factory->newObject($this->_actionDictionary);
     } else {
         // It's a loaded object
         return $this->_actionDictionary;
     }
 }
Пример #3
0
 /**
  * Set text to be displayed for the annotation or, if this type of annotation
  * does not display text, an alternate description of the annotation’s contents
  * in human-readable form.
  *
  * @param string $text
  * @return Zend_Pdf_Annotation
  */
 public function setText($text)
 {
     if ($this->_annotationDictionary->Contents === null) {
         $this->_annotationDictionary->touch();
         $this->_annotationDictionary->Contents = new Zend_Pdf_Element_String($text);
     } else {
         $this->_annotationDictionary->Contents->touch();
         $this->_annotationDictionary->Contents->value = new Zend_Pdf_Element_String($text);
     }
     return $this;
 }
Пример #4
0
 /**
  * Set text to be displayed for the annotation or, if this type of annotation
  * does not display text, an alternate description of the annotation’s contents
  * in human-readable form.
  *
  * @param string $text
  * @return Zend_Pdf_Annotation
  */
 public function setText($text)
 {
     require_once 'Zend/Pdf/Element/String.php';
     if ($this->_annotationDictionary->Contents === null) {
         $this->_annotationDictionary->touch();
         $this->_annotationDictionary->Contents = new Zend_Pdf_Element_String($text);
     } else {
         $this->_annotationDictionary->Contents->touch();
         $this->_annotationDictionary->Contents->value = new Zend_Pdf_Element_String($text);
     }
     return $this;
 }
Пример #5
0
 /**
  * Dump current drawing instructions into the content stream.
  *
  * @todo Don't forget to close all current graphics operations (like path drawing)
  *
  * @throws Zend_Pdf_Exception
  */
 public function flush()
 {
     if ($this->_saveCount != 0) {
         throw new Zend_Pdf_Exception('Saved graphics state is not restored');
     }
     if ($this->_contents == '') {
         return;
     }
     if ($this->_pageDictionary->Contents->getType() != Zend_Pdf_Element::TYPE_ARRAY) {
         /**
          * It's a stream object.
          * Prepare Contents page attribute for update.
          */
         $this->_pageDictionary->touch();
         $currentPageContents = $this->_pageDictionary->Contents;
         $this->_pageDictionary->Contents = new Zend_Pdf_Element_Array();
         $this->_pageDictionary->Contents->items[] = $currentPageContents;
     } else {
         $this->_pageDictionary->Contents->touch();
     }
     $this->_pageDictionary->Contents->items[] = $this->_objFactory->newStreamObject($this->_contents);
     $this->_contents == '';
 }
Пример #6
0
 /**
  *
  * @param Zend_Pdf_Annotation $annotation
  * @return Zend_Pdf_Page
  */
 public function attachAnnotation(Zend_Pdf_Annotation $annotation)
 {
     $annotationDictionary = $annotation->getResource();
     if (!$annotationDictionary instanceof Zend_Pdf_Element_Object && !$annotationDictionary instanceof Zend_Pdf_Element_Reference) {
         $annotationDictionary = $this->_objFactory->newObject($annotationDictionary);
     }
     if ($this->_pageDictionary->Annots === null) {
         $this->_pageDictionary->touch();
         $this->_pageDictionary->Annots = new Zend_Pdf_Element_Array();
     } else {
         $this->_pageDictionary->Annots->touch();
     }
     $this->_pageDictionary->Annots->items[] = $annotationDictionary;
     $annotationDictionary->touch();
     $annotationDictionary->P = $this->_pageDictionary;
     return $this;
 }
Пример #7
0
 /**
  * Dump Outline and its child outlines into PDF structures
  *
  * Returns dictionary indirect object or reference
  *
  * @internal
  * @param Zend_Pdf_ElementFactory    $factory object factory for newly created indirect objects
  * @param boolean $updateNavigation  Update navigation flag
  * @param Zend_Pdf_Element $parent   Parent outline dictionary reference
  * @param Zend_Pdf_Element $prev     Previous outline dictionary reference
  * @param SplObjectStorage $processedOutlines  List of already processed outlines
  * @return Zend_Pdf_Element
  * @throws Zend_Pdf_Exception
  */
 public function dumpOutline(Zend_Pdf_ElementFactory_Interface $factory, $updateNavigation, Zend_Pdf_Element $parent, Zend_Pdf_Element $prev = null, SplObjectStorage $processedOutlines = null)
 {
     if ($processedOutlines === null) {
         $processedOutlines = new SplObjectStorage();
     }
     $processedOutlines->attach($this);
     if ($updateNavigation) {
         $this->_outlineDictionary->touch();
         $this->_outlineDictionary->Parent = $parent;
         $this->_outlineDictionary->Prev = $prev;
         $this->_outlineDictionary->Next = null;
     }
     $updateChildNavigation = false;
     if (count($this->_originalChildOutlines) != count($this->childOutlines)) {
         // If original and current children arrays have different size then children list was updated
         $updateChildNavigation = true;
     } else {
         if (!(array_keys($this->_originalChildOutlines) === array_keys($this->childOutlines))) {
             // If original and current children arrays have different keys (with a glance to an order) then children list was updated
             $updateChildNavigation = true;
         } else {
             foreach ($this->childOutlines as $key => $childOutline) {
                 if ($this->_originalChildOutlines[$key] !== $childOutline) {
                     $updateChildNavigation = true;
                     break;
                 }
             }
         }
     }
     $lastChild = null;
     if ($updateChildNavigation) {
         $this->_outlineDictionary->touch();
         $this->_outlineDictionary->First = null;
         foreach ($this->childOutlines as $childOutline) {
             if ($processedOutlines->contains($childOutline)) {
                 require_once 'Zend/Pdf/Exception.php';
                 throw new Zend_Pdf_Exception('Outlines cyclyc reference is detected.');
             }
             if ($lastChild === null) {
                 // First pass. Update Outlines dictionary First entry using corresponding value
                 $lastChild = $childOutline->dumpOutline($factory, $updateChildNavigation, $this->_outlineDictionary, null, $processedOutlines);
                 $this->_outlineDictionary->First = $lastChild;
             } else {
                 // Update previous outline dictionary Next entry (Prev is updated within dumpOutline() method)
                 $childOutlineDictionary = $childOutline->dumpOutline($factory, $updateChildNavigation, $this->_outlineDictionary, $lastChild, $processedOutlines);
                 $lastChild->Next = $childOutlineDictionary;
                 $lastChild = $childOutlineDictionary;
             }
         }
         $this->_outlineDictionary->Last = $lastChild;
         if (count($this->childOutlines) != 0) {
             $this->_outlineDictionary->Count = new Zend_Pdf_Element_Numeric(($this->isOpen() ? 1 : -1) * count($this->childOutlines));
         } else {
             $this->_outlineDictionary->Count = null;
         }
     } else {
         foreach ($this->childOutlines as $childOutline) {
             if ($processedOutlines->contains($childOutline)) {
                 require_once 'Zend/Pdf/Exception.php';
                 throw new Zend_Pdf_Exception('Outlines cyclyc reference is detected.');
             }
             $lastChild = $childOutline->dumpOutline($factory, $updateChildNavigation, $this->_outlineDictionary, $lastChild, $processedOutlines);
         }
     }
     return $this->_outlineDictionary;
 }
Пример #8
0
 /**
  * Dump current drawing instructions into the content stream.
  *
  * @todo Don't forget to close all current graphics operations (like path drawing)
  *
  * @throws Zend_Pdf_Exception
  */
 public function flush()
 {
     if ($this->_saveCount != 0) {
         throw new Zend_Pdf_Exception('Saved graphics state is not restored');
     }
     if ($this->_contents == '') {
         return;
     }
     if ($this->_pageDictionary->Contents->getType() != Zend_Pdf_Element::TYPE_ARRAY) {
         /**
          * It's a stream object.
          * Prepare Contents page attribute for update.
          */
         $this->_pageDictionary->touch();
         $currentPageContents = $this->_pageDictionary->Contents;
         $this->_pageDictionary->Contents = new Zend_Pdf_Element_Array();
         $this->_pageDictionary->Contents->items[] = $currentPageContents;
     } else {
         $this->_pageDictionary->Contents->touch();
     }
     if (!$this->_safeGS && count($this->_pageDictionary->Contents->items) != 0) {
         /**
          * Page already has some content which is not treated as safe.
          *
          * Add save/restore GS operators
          */
         $this->_addProcSet('PDF');
         $newContentsArray = new Zend_Pdf_Element_Array();
         $newContentsArray->items[] = $this->_objFactory->newStreamObject(" q\n");
         foreach ($this->_pageDictionary->Contents->items as $contentStream) {
             $newContentsArray->items[] = $contentStream;
         }
         $newContentsArray->items[] = $this->_objFactory->newStreamObject(" Q\n");
         $this->_pageDictionary->touch();
         $this->_pageDictionary->Contents = $newContentsArray;
         $this->_safeGS = true;
     }
     $this->_pageDictionary->Contents->items[] = $this->_objFactory->newStreamObject($this->_contents);
     $this->_contents = '';
 }
Пример #9
0
 /**
  * Load pages recursively
  *
  * @param Zend_Pdf_Element_Reference $pages
  */
 private function _loadPages(Zend_Pdf_Element_Reference $pages)
 {
     if ($pages->getType() != Zend_Pdf_Element::TYPE_DICTIONARY) {
         throw new Zend_Pdf_Exception('Wrong argument');
     }
     foreach ($pages->Kids->items as $child) {
         if ($child->Type->value == 'Pages') {
             $this->_loadPages($child);
         } else {
             if ($child->Type->value == 'Page') {
                 $this->pages[] = new Zend_Pdf_Page($child, $this->_objFactory);
             }
         }
     }
 }