/** * Object constructor. * * If resource is not a Zend_Pdf_Element object, then stream object with specified value is * generated. * * @param Zend_Pdf_Element|string $resource */ public function __construct($resource) { $this->_processedFacories = array(); $this->_objectFactory = new Zend_Pdf_ElementFactory(1); if ($resource instanceof Zend_Pdf_Element) { $this->_resource = $this->_objectFactory->newObject($resource); } else { $this->_resource = $this->_objectFactory->newStreamObject($resource); } }
/** * Object constructor. * Constructor signatures: * * 1. Load PDF page from a parsed PDF file. * Object factory is created by PDF parser. * --------------------------------------------------------- * new Zend_Pdf_Page(Zend_Pdf_Element_Dictionary $pageDict, * Zend_Pdf_ElementFactory $factory); * --------------------------------------------------------- * * 2. Clone PDF page. * New page is created in the same context as source page. Object factory is shared. * Thus it will be attached to the document, but need to be placed into Zend_Pdf::$pages array * to be included into output. * --------------------------------------------------------- * new Zend_Pdf_Page(Zend_Pdf_Page $page); * --------------------------------------------------------- * * 3. Create new page with a specified pagesize. * If $factory is null then it will be created and page must be attached to the document to be * included into output. * --------------------------------------------------------- * new Zend_Pdf_Page(string $pagesize, Zend_Pdf_ElementFactory $factory = null); * --------------------------------------------------------- * * 4. Create new page with a specified pagesize (in default user space units). * If $factory is null then it will be created and page must be attached to the document to be * included into output. * --------------------------------------------------------- * new Zend_Pdf_Page(numeric $width, numeric $height, Zend_Pdf_ElementFactory $factory = null); * --------------------------------------------------------- * * * @param mixed $param1 * @param mixed $param2 * @param mixed $param3 * @throws Zend_Pdf_Exception */ public function __construct($param1, $param2 = null, $param3 = null) { if ($param1 instanceof Zend_Pdf_Element_Reference && $param1->getType() == Zend_Pdf_Element::TYPE_DICTIONARY && $param2 instanceof Zend_Pdf_ElementFactory && $param3 === null) { $this->_pageDictionary = $param1; $this->_objFactory = $param2; $this->_attached = true; return; } else { if ($param1 instanceof Zend_Pdf_Page && $param2 === null && $param3 === null) { /** @todo implementation */ throw new Zend_Pdf_Exception('Not implemented yet.'); } else { if (is_string($param1) && ($param2 === null || $param2 instanceof Zend_Pdf_ElementFactory) && $param3 === null) { $this->_objFactory = $param2 !== null ? $param2 : new Zend_Pdf_ElementFactory(1); $this->_attached = false; if (array_key_exists($param1, Zend_Pdf_Const::$pageSizeAliases)) { $param1 = Zend_Pdf_Const::$pageSizeAliases[$param1]; } $pageDim = explode(':', $param1); if (count($pageDim) == 3) { $pageWidth = $pageDim[0]; $pageHeight = $pageDim[1]; } else { /** * @todo support of user defined pagesize notations, like: * "210x297mm", "595x842", "8.5x11in", "612x792" */ throw new Zend_Pdf_Exception('Wrong pagesize notation.'); } /** * @todo support of pagesize recalculation to "default user space units" */ } else { if (is_numeric($param1) && is_numeric($param2) && ($param3 === null || $param3 instanceof Zend_Pdf_ElementFactory)) { $this->_objFactory = $param3 !== null ? $param3 : new Zend_Pdf_ElementFactory(1); $this->_attached = false; $pageWidth = $param1; $pageHeight = $param2; } else { throw new Zend_Pdf_Exception('Unrecognized method signature, wrong number of arguments or wrong argument types.'); } } } } $this->_pageDictionary = $this->_objFactory->newObject(new Zend_Pdf_Element_Dictionary()); $this->_pageDictionary->Type = new Zend_Pdf_Element_Name('Page'); $this->_pageDictionary->LastModified = new Zend_Pdf_Element_String(Zend_Pdf_Const::pdfDate()); $this->_pageDictionary->Resources = new Zend_Pdf_Element_Dictionary(); $this->_pageDictionary->MediaBox = new Zend_Pdf_Element_Array(); $this->_pageDictionary->MediaBox->items[] = new Zend_Pdf_Element_Numeric(0); $this->_pageDictionary->MediaBox->items[] = new Zend_Pdf_Element_Numeric(0); $this->_pageDictionary->MediaBox->items[] = new Zend_Pdf_Element_Numeric($pageWidth); $this->_pageDictionary->MediaBox->items[] = new Zend_Pdf_Element_Numeric($pageHeight); $this->_pageDictionary->Contents = new Zend_Pdf_Element_Array(); }
/** * Detach PDF object from the factory (if applicable), clone it and attach to new factory. * * @param Zend_Pdf_ElementFactory $factory The factory to attach * @param array &$processed List of already processed indirect objects, used to avoid objects duplication * @param integer $mode Cloning mode (defines filter for objects cloning) * @returns Zend_Pdf_Element */ public function makeClone(Zend_Pdf_ElementFactory $factory, array &$processed, $mode) { $id = spl_object_hash($this); if (isset($processed[$id])) { // Do nothing if object is already processed // return it return $processed[$id]; } // Create obect with null value and register it in $processed container $processed[$id] = $clonedObject = $factory->newObject(new Zend_Pdf_Element_Null()); // Pecursively process actual data $clonedObject->_value = $this->_value->makeClone($factory, $processed, $mode); if ($clonedObject->_value instanceof Zend_Pdf_Element_Null) { // Do not store null objects within $processed container since it may be filtered // by $mode parameter but used in some future pass unset($processed[$id]); // Return direct null object return $clonedObject->_value; } return $clonedObject; }
/** * Load pages recursively * * @param Zend_Pdf_Element_Reference $pages * @param array|null $attributes */ private function _loadPages(Zend_Pdf_Element_Reference $pages, $attributes = null) { static $inheritable = array('Resources', 'MediaBox', 'CropBox', 'Rotate'); if ($pages->getType() != Zend_Pdf_Element::TYPE_DICTIONARY) { throw new Zend_Pdf_Exception('Wrong argument'); } if ($attributes === null) { $attributes = array(); } foreach ($pages->getKeys() as $property) { if (in_array($property, $inheritable)) { $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 ($inheritable 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) { $child->{$property} = $attributes[$property]; } else { $child->{$property} = $this->_objFactory->newObject($attributes[$property]); } } } $this->pages[] = new Zend_Pdf_Page($child, $this->_objFactory); } } } }
/** * Creates or loads PDF document. * * If $source is null, then it creates a new document. * * If $source is a string and $load is false, then it loads document * from a binary string. * * If $source is a string and $load is true, then it loads document * from a file. * $revision used to roll back document to specified version * (0 - currtent version, 1 - previous version, 2 - ...) * * @param string $source - PDF file to load * @param integer $revision * @throws Zend_Pdf_Exception */ public function __construct(&$source = null, $revision = null, $load = false) { $this->_objFactory = new Zend_Pdf_ElementFactory(1); if ($source !== null) { $parser = new Zend_Pdf_Parser($source, $this->_objFactory, $load); $this->_trailer = $parser->getTrailer(); if ($revision !== null) { $this->rollback($revision); } else { $this->_loadPages($this->_trailer->Root->Pages); } } else { $trailerDictionary = new Zend_Pdf_Element_Dictionary(); /** * Document id */ $docId = md5(uniqid(rand(), true)); // 32 byte (128 bit) identifier $docIdLow = substr($docId, 0, 16); // first 16 bytes $docIdHigh = substr($docId, 16, 16); // second 16 bytes $trailerDictionary->ID = new Zend_Pdf_Element_Array(); $trailerDictionary->ID->items[] = new Zend_Pdf_Element_String_Binary($docIdLow); $trailerDictionary->ID->items[] = new Zend_Pdf_Element_String_Binary($docIdHigh); $this->_trailer = new Zend_Pdf_Trailer_Generator($trailerDictionary); /** * Document catalog indirect object. */ $docCatalog = $this->_objFactory->newObject(new Zend_Pdf_Element_Dictionary()); $docCatalog->Type = new Zend_Pdf_Element_Name('Catalog'); $docCatalog->Version = new Zend_Pdf_Element_Name(Zend_Pdf_Const::PDF_VERSION); $this->_trailer->Root = $docCatalog; /** * Pages container */ $docPages = $this->_objFactory->newObject(new Zend_Pdf_Element_Dictionary()); $docPages->Type = new Zend_Pdf_Element_Name('Pages'); $docPages->Kids = new Zend_Pdf_Element_Array(); $docPages->Count = new Zend_Pdf_Element_Numeric(0); $docCatalog->Pages = $docPages; } }
public function makeClone(Zend_Pdf_ElementFactory $factory, array &$processed, $mode) { $id = spl_object_hash($this); if (isset($processed[$id])) { return $processed[$id]; } $processed[$id] = $clonedObject = $factory->newObject(new Zend_Pdf_Element_Null()); $clonedObject->_value = $this->_value->makeClone($factory, $processed, $mode); if ($clonedObject->_value instanceof Zend_Pdf_Element_Null) { unset($processed[$id]); return $clonedObject->_value; } return $clonedObject; }