📄 page.php
字号:
{ throw new Zend_Pdf_Exception('Cloning Zend_Pdf_Page object using \'clone\' keyword is not supported. Use \'new Zend_Pdf_Page($srcPage)\' syntax'); } /** * Attach resource to the page * * @param string $type * @param Zend_Pdf_Resource $resource * @return string */ private function _attachResource($type, Zend_Pdf_Resource $resource) { // Check that Resources dictionary contains appropriate resource set if ($this->_pageDictionary->Resources->$type === null) { $this->_pageDictionary->Resources->touch(); $this->_pageDictionary->Resources->$type = new Zend_Pdf_Element_Dictionary(); } else { $this->_pageDictionary->Resources->$type->touch(); } // Check, that resource is already attached to resource set. $resObject = $resource->getResource(); foreach ($this->_pageDictionary->Resources->$type->getKeys() as $ResID) { if ($this->_pageDictionary->Resources->$type->$ResID === $resObject) { return $ResID; } } $idCounter = 1; do { $newResName = $type[0] . $idCounter++; } while ($this->_pageDictionary->Resources->$type->$newResName !== null); $this->_pageDictionary->Resources->$type->$newResName = $resObject; $this->_objFactory->attach($resource->getFactory()); return $newResName; }
/** * Add procedureSet to the Page description * * @param string $procSetName */ private function _addProcSet($procSetName) { // Check that Resources dictionary contains ProcSet entry if ($this->_pageDictionary->Resources->ProcSet === null) { $this->_pageDictionary->Resources->touch(); $this->_pageDictionary->Resources->ProcSet = new Zend_Pdf_Element_Array(); } else { $this->_pageDictionary->Resources->ProcSet->touch(); } foreach ($this->_pageDictionary->Resources->ProcSet->items as $procSetEntry) { if ($procSetEntry->value == $procSetName) { // Procset is already included into a ProcSet array return; } } $this->_pageDictionary->Resources->ProcSet->items[] = new Zend_Pdf_Element_Name($procSetName); } /** * Retrive PDF file reference to the page * * @return Zend_Pdf_Element_Dictionary */ public function getPageDictionary() { return $this->_pageDictionary; } /** * 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 = ''; } /** * Prepare page to be rendered into PDF. * * @todo Don't forget to close all current graphics operations (like path drawing) * * @param Zend_Pdf_ElementFactory_Interface $objFactory * @throws Zend_Pdf_Exception */ public function render(Zend_Pdf_ElementFactory_Interface $objFactory) { $this->flush(); if ($objFactory === $this->_objFactory) { // Page is already attached to the document. return; } if ($this->_attached) { throw new Zend_Pdf_Exception('Page is attached to one documen, but rendered in context of another.'); /** * @todo Page cloning must be implemented here instead of exception. * PDF objects (ex. fonts) can be shared between pages. * Thus all referenced objects, which can be modified, must be cloned recursively, * to avoid producing wrong object references in a context of source PDF. */ //... } else { $objFactory->attach($this->_objFactory); } } /** * Set fill color. * * @param Zend_Pdf_Color $color */ public function setFillColor(Zend_Pdf_Color $color) { $this->_addProcSet('PDF'); $this->_contents .= $color->instructions(false); } /** * Set line color. * * @param Zend_Pdf_Color $color */ public function setLineColor(Zend_Pdf_Color $color) { $this->_addProcSet('PDF'); $this->_contents .= $color->instructions(true); } /** * Set line width. * * @param float $width */ public function setLineWidth($width) { $this->_addProcSet('PDF'); $widthObj = new Zend_Pdf_Element_Numeric($width); $this->_contents .= $widthObj->toString() . " w\n"; } /** * Set line dashing pattern * * Pattern is an array of floats: array(on_length, off_length, on_length, off_length, ...) * Phase is shift from the beginning of line. * * @param array $pattern * @param array $phase */ public function setLineDashingPattern($pattern, $phase = 0) { $this->_addProcSet('PDF'); if ($pattern === Zend_Pdf_Page::LINE_DASHING_SOLID) { $pattern = array(); $phase = 0; } $dashPattern = new Zend_Pdf_Element_Array(); $phaseEleemnt = new Zend_Pdf_Element_Numeric($phase); foreach ($pattern as $dashItem) { $dashElement = new Zend_Pdf_Element_Numeric($dashItem); $dashPattern->items[] = $dashElement; } $this->_contents .= $dashPattern->toString() . ' ' . $phaseEleemnt->toString() . " d\n"; } /** * Set current font. * * @param Zend_Pdf_Resource_Font $font * @param float $fontSize */ public function setFont(Zend_Pdf_Resource_Font $font, $fontSize) { $this->_addProcSet('Text'); $fontName = $this->_attachResource('Font', $font); $this->_font = $font; $this->_fontSize = $fontSize; $fontNameObj = new Zend_Pdf_Element_Name($fontName); $fontSizeObj = new Zend_Pdf_Element_Numeric($fontSize); $this->_contents .= $fontNameObj->toString() . ' ' . $fontSizeObj->toString() . " Tf\n"; } /** * Set the style to use for future drawing operations on this page * * @param Zend_Pdf_Style $style */ public function setStyle(Zend_Pdf_Style $style) { $this->_style = $style; $this->_addProcSet('Text'); $this->_addProcSet('PDF'); if ($style->getFont() !== null) { $this->setFont($style->getFont(), $style->getFontSize()); } $this->_contents .= $style->instructions($this->_pageDictionary->Resources); } /** * Get current font. * * @return Zend_Pdf_Resource_Font $font */ public function getFont() { return $this->_font; }
/**
* Extract resources attached to the page
*
* This method is not intended to be used in userland, but helps to optimize some document wide operations
*
* returns array of Zend_Pdf_Element_Dictionary objects
*
* @internal
* @return array
*/
public function extractResources()
{
return $this->_pageDictionary->Resources;
}
/**
* Extract fonts attached to the page
*
* returns array of Zend_Pdf_Resource_Font_Extracted objects
*
* @return array
*/
public function extractFonts()
{
if ($this->_pageDictionary->Resources->Font === null) {
// Page doesn't have any font attached
// Return empty array
return array();
}
$fontResources = $this->_pageDictionary->Resources->Font;
$fontResourcesUnique = array();
foreach ($fontResources->getKeys() as $fontResourceName) {
$fontDictionary = $fontResources->$fontResourceName;
if (! ($fontDictionary instanceof Zend_Pdf_Element_Reference ||
$fontDictionary instanceof Zend_Pdf_Element_Object) ) {
// Font dictionary has to be an indirect object or object reference
continue;
}
$fontResourcesUnique[$fontDictionary->toString($this->_objFactory)] = $fontDictionary;
}
$fonts = array();
foreach ($fontResourcesUnique as $resourceReference => $fontDictionary) {
try {
// Try to extract font
$extractedFont = new Zend_Pdf_Resource_Font_Extracted($fontDictionary);
$fonts[$resourceReference] = $extractedFont;
} catch (Zend_Pdf_Exception $e) {
if ($e->getMessage() != 'Unsupported font type.') {
throw $e;
}
}
}
return $fonts;
}
/**
* Extract font attached to the page by specific font name
*
* $fontName should be specified in UTF-8 encoding
*
* @return Zend_Pdf_Resource_Font_Extracted|null
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -