123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635 |
- <?php
- /**
- * Zend Framework
- *
- * LICENSE
- *
- * This source file is subject to the new BSD license that is bundled
- * with this package in the file LICENSE.txt.
- * It is also available through the world-wide-web at this URL:
- * http://framework.zend.com/license/new-bsd
- * If you did not receive a copy of the license and are unable to
- * obtain it through the world-wide-web, please send an email
- * to license@zend.com so we can send you a copy immediately.
- *
- * @category Zend
- * @package Zend_Pdf
- * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @version $Id$
- */
- /** User land classes and interfaces turned on by Zend/Pdf.php file inclusion. */
- /** @todo Section should be removed with ZF 2.0 release as obsolete */
- /** Zend_Pdf_Page */
- #require_once 'Zend/Pdf/Page.php';
- /** Zend_Pdf_Style */
- #require_once 'Zend/Pdf/Style.php';
- /** Zend_Pdf_Color_GrayScale */
- #require_once 'Zend/Pdf/Color/GrayScale.php';
- /** Zend_Pdf_Color_Rgb */
- #require_once 'Zend/Pdf/Color/Rgb.php';
- /** Zend_Pdf_Color_Cmyk */
- #require_once 'Zend/Pdf/Color/Cmyk.php';
- /** Zend_Pdf_Color_Html */
- #require_once 'Zend/Pdf/Color/Html.php';
- /** Zend_Pdf_Image */
- #require_once 'Zend/Pdf/Image.php';
- /** Zend_Pdf_Font */
- #require_once 'Zend/Pdf/Font.php';
- /** Zend_Pdf_Resource_Extractor */
- #require_once 'Zend/Pdf/Resource/Extractor.php';
- /** Zend_Pdf_Canvas */
- #require_once 'Zend/Pdf/Canvas.php';
- /** Internally used classes */
- #require_once 'Zend/Pdf/Element.php';
- #require_once 'Zend/Pdf/Element/Array.php';
- #require_once 'Zend/Pdf/Element/String/Binary.php';
- #require_once 'Zend/Pdf/Element/Boolean.php';
- #require_once 'Zend/Pdf/Element/Dictionary.php';
- #require_once 'Zend/Pdf/Element/Name.php';
- #require_once 'Zend/Pdf/Element/Null.php';
- #require_once 'Zend/Pdf/Element/Numeric.php';
- #require_once 'Zend/Pdf/Element/String.php';
- /**
- * General entity which describes PDF document.
- * It implements document abstraction with a document level operations.
- *
- * Class is used to create new PDF document or load existing document.
- * See details in a class constructor description
- *
- * Class agregates document level properties and entities (pages, bookmarks,
- * document level actions, attachments, form object, etc)
- *
- * @category Zend
- * @package Zend_Pdf
- * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- */
- class Zend_Pdf
- {
- /**** Class Constants ****/
- /**
- * Version number of generated PDF documents.
- */
- const PDF_VERSION = '1.4';
- /**
- * PDF file header.
- */
- const PDF_HEADER = "%PDF-1.4\n%\xE2\xE3\xCF\xD3\n";
- /**
- * Form field options
- */
- const PDF_FORM_FIELD_READONLY = 1;
- const PDF_FORM_FIELD_REQUIRED = 2;
- const PDF_FORM_FIELD_NOEXPORT = 4;
- /**
- * Pages collection
- *
- * @todo implement it as a class, which supports ArrayAccess and Iterator interfaces,
- * to provide incremental parsing and pages tree updating.
- * That will give good performance and memory (PDF size) benefits.
- *
- * @var array - array of Zend_Pdf_Page object
- */
- public $pages = array();
- /**
- * Document properties
- *
- * It's an associative array with PDF meta information, values may
- * be string, boolean or float.
- * Returned array could be used directly to access, add, modify or remove
- * document properties.
- *
- * Standard document properties: Title (must be set for PDF/X documents), Author,
- * Subject, Keywords (comma separated list), Creator (the name of the application,
- * that created document, if it was converted from other format), Trapped (must be
- * true, false or null, can not be null for PDF/X documents)
- *
- * @var array
- */
- public $properties = array();
- /**
- * Original properties set.
- *
- * Used for tracking properties changes
- *
- * @var array
- */
- protected $_originalProperties = array();
- /**
- * Document level javascript
- *
- * @var string
- */
- protected $_javaScript = null;
- /**
- * Document named destinations or "GoTo..." actions, used to refer
- * document parts from outside PDF
- *
- * @var array - array of Zend_Pdf_Target objects
- */
- protected $_namedTargets = array();
- /**
- * Document outlines
- *
- * @var array - array of Zend_Pdf_Outline objects
- */
- public $outlines = array();
- /**
- * Original document outlines list
- * Used to track outlines update
- *
- * @var array - array of Zend_Pdf_Outline objects
- */
- protected $_originalOutlines = array();
- /**
- * Original document outlines open elements count
- * Used to track outlines update
- *
- * @var integer
- */
- protected $_originalOpenOutlinesCount = 0;
- /**
- * Pdf trailer (last or just created)
- *
- * @var Zend_Pdf_Trailer
- */
- protected $_trailer = null;
- /**
- * PDF objects factory.
- *
- * @var Zend_Pdf_ElementFactory_Interface
- */
- protected $_objFactory = null;
- /**
- * Memory manager for stream objects
- *
- * @var Zend_Memory_Manager|null
- */
- protected static $_memoryManager = null;
- /**
- * Pdf file parser.
- * It's not used, but has to be destroyed only with Zend_Pdf object
- *
- * @var Zend_Pdf_Parser
- */
- protected $_parser;
- /**
- * List of inheritable attributesfor pages tree
- *
- * @var array
- */
- protected static $_inheritableAttributes = array('Resources', 'MediaBox', 'CropBox', 'Rotate');
- /**
- * List of form fields
- *
- * @var array - Associative array, key: name of form field, value: Zend_Pdf_Element
- */
- protected $_formFields = array();
- /**
- * True if the object is a newly created PDF document (affects save() method behavior)
- * False otherwise
- *
- * @var boolean
- */
- protected $_isNewDocument = true;
- /**
- * Request used memory manager
- *
- * @return Zend_Memory_Manager
- */
- static public function getMemoryManager()
- {
- if (self::$_memoryManager === null) {
- #require_once 'Zend/Memory.php';
- self::$_memoryManager = Zend_Memory::factory('none');
- }
- return self::$_memoryManager;
- }
- /**
- * Set user defined memory manager
- *
- * @param Zend_Memory_Manager $memoryManager
- */
- static public function setMemoryManager(Zend_Memory_Manager $memoryManager)
- {
- self::$_memoryManager = $memoryManager;
- }
- /**
- * Create new PDF document from a $source string
- *
- * @param string $source
- * @param integer $revision
- * @return Zend_Pdf
- */
- public static function parse(&$source = null, $revision = null)
- {
- return new Zend_Pdf($source, $revision);
- }
- /**
- * Load PDF document from a file
- *
- * @param string $source
- * @param integer $revision
- * @return Zend_Pdf
- */
- public static function load($source = null, $revision = null)
- {
- return new Zend_Pdf($source, $revision, true);
- }
- /**
- * Render PDF document and save it.
- *
- * If $updateOnly is true and it's not a new document, then it only
- * appends new section to the end of file.
- *
- * @param string $filename
- * @param boolean $updateOnly
- * @throws Zend_Pdf_Exception
- */
- public function save($filename, $updateOnly = false)
- {
- if (($file = @fopen($filename, $updateOnly ? 'ab':'wb')) === false ) {
- #require_once 'Zend/Pdf/Exception.php';
- throw new Zend_Pdf_Exception( "Can not open '$filename' file for writing." );
- }
- $this->render($updateOnly, $file);
- fclose($file);
- }
- /**
- * 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 - current version, 1 - previous version, 2 - ...)
- *
- * @param string $source - PDF file to load
- * @param integer $revision
- * @param bool $load
- * @throws Zend_Pdf_Exception
- * @return Zend_Pdf
- */
- public function __construct($source = null, $revision = null, $load = false)
- {
- #require_once 'Zend/Pdf/ElementFactory.php';
- $this->_objFactory = Zend_Pdf_ElementFactory::createFactory(1);
- if ($source !== null) {
- #require_once 'Zend/Pdf/Parser.php';
- $this->_parser = new Zend_Pdf_Parser($source, $this->_objFactory, $load);
- $this->_pdfHeaderVersion = $this->_parser->getPDFVersion();
- $this->_trailer = $this->_parser->getTrailer();
- if ($this->_trailer->Encrypt !== null) {
- #require_once 'Zend/Pdf/Exception.php';
- throw new Zend_Pdf_Exception('Encrypted document modification is not supported');
- }
- if ($revision !== null) {
- $this->rollback($revision);
- } else {
- $this->_loadPages($this->_trailer->Root->Pages);
- }
- $this->_loadNamedDestinations($this->_trailer->Root, $this->_parser->getPDFVersion());
- $this->_loadOutlines($this->_trailer->Root);
- $this->_loadJavaScript($this->_trailer->Root);
- $this->_loadFormFields($this->_trailer->Root);
- if ($this->_trailer->Info !== null) {
- $this->properties = $this->_trailer->Info->toPhp();
- if (isset($this->properties['Trapped'])) {
- switch ($this->properties['Trapped']) {
- case 'True':
- $this->properties['Trapped'] = true;
- break;
- case 'False':
- $this->properties['Trapped'] = false;
- break;
- case 'Unknown':
- $this->properties['Trapped'] = null;
- break;
- default:
- // Wrong property value
- // Do nothing
- break;
- }
- }
- $this->_originalProperties = $this->properties;
- }
- $this->_isNewDocument = false;
- } else {
- $this->_pdfHeaderVersion = Zend_Pdf::PDF_VERSION;
- $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);
- $trailerDictionary->Size = new Zend_Pdf_Element_Numeric(0);
- #require_once 'Zend/Pdf/Trailer/Generator.php';
- $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::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;
- }
- }
- /**
- * Retrive number of revisions.
- *
- * @return integer
- */
- public function revisions()
- {
- $revisions = 1;
- $currentTrailer = $this->_trailer;
- while ($currentTrailer->getPrev() !== null && $currentTrailer->getPrev()->Root !== null ) {
- $revisions++;
- $currentTrailer = $currentTrailer->getPrev();
- }
- return $revisions++;
- }
- /**
- * Rollback document $steps number of revisions.
- * This method must be invoked before any changes, applied to the document.
- * Otherwise behavior is undefined.
- *
- * @param integer $steps
- */
- public function rollback($steps)
- {
- for ($count = 0; $count < $steps; $count++) {
- if ($this->_trailer->getPrev() !== null && $this->_trailer->getPrev()->Root !== null) {
- $this->_trailer = $this->_trailer->getPrev();
- } else {
- break;
- }
- }
- $this->_objFactory->setObjectCount($this->_trailer->Size->value);
- // Mark content as modified to force new trailer generation at render time
- $this->_trailer->Root->touch();
- $this->pages = array();
- $this->_loadPages($this->_trailer->Root->Pages);
- }
- /**
- * Load pages recursively
- *
- * @param Zend_Pdf_Element_Reference $pages
- * @param array|null $attributes
- * @throws Zend_Pdf_Exception
- */
- protected function _loadPages(Zend_Pdf_Element_Reference $pages, $attributes = array())
- {
- if ($pages->getType() != Zend_Pdf_Element::TYPE_DICTIONARY) {
- #require_once 'Zend/Pdf/Exception.php';
- 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]);
- }
- }
- }
- #require_once 'Zend/Pdf/Page.php';
- $this->pages[] = new Zend_Pdf_Page($child, $this->_objFactory);
- }
- }
- }
- /**
- * Load named destinations recursively
- *
- * @param Zend_Pdf_Element_Reference $root Document catalog entry
- * @param string $pdfHeaderVersion
- * @throws Zend_Pdf_Exception
- */
- protected function _loadNamedDestinations(Zend_Pdf_Element_Reference $root, $pdfHeaderVersion)
- {
- if ($root->Version !== null && version_compare($root->Version->value, $pdfHeaderVersion, '>')) {
- $versionIs_1_2_plus = version_compare($root->Version->value, '1.1', '>');
- } else {
- $versionIs_1_2_plus = version_compare($pdfHeaderVersion, '1.1', '>');
- }
- if ($versionIs_1_2_plus) {
- // PDF version is 1.2+
- // Look for Destinations structure at Name dictionary
- if ($root->Names !== null && $root->Names->Dests !== null) {
- #require_once 'Zend/Pdf/NameTree.php';
- #require_once 'Zend/Pdf/Target.php';
- foreach (new Zend_Pdf_NameTree($root->Names->Dests) as $name => $destination) {
- $this->_namedTargets[$name] = Zend_Pdf_Target::load($destination);
- }
- }
- } else {
- // PDF version is 1.1 (or earlier)
- // Look for Destinations sructure at Dest entry of document catalog
- if ($root->Dests !== null) {
- if ($root->Dests->getType() != Zend_Pdf_Element::TYPE_DICTIONARY) {
- #require_once 'Zend/Pdf/Exception.php';
- throw new Zend_Pdf_Exception('Document catalog Dests entry must be a dictionary.');
- }
- #require_once 'Zend/Pdf/Target.php';
- foreach ($root->Dests->getKeys() as $destKey) {
- $this->_namedTargets[$destKey] = Zend_Pdf_Target::load($root->Dests->$destKey);
- }
- }
- }
- }
- /**
- * Load outlines recursively
- *
- * @param Zend_Pdf_Element_Reference $root Document catalog entry
- * @throws Zend_Pdf_Exception
- */
- protected function _loadOutlines(Zend_Pdf_Element_Reference $root)
- {
- if ($root->Outlines === null) {
- return;
- }
- if ($root->Outlines->getType() != Zend_Pdf_Element::TYPE_DICTIONARY) {
- #require_once 'Zend/Pdf/Exception.php';
- throw new Zend_Pdf_Exception('Document catalog Outlines entry must be a dictionary.');
- }
- if ($root->Outlines->Type !== null && $root->Outlines->Type->value != 'Outlines') {
- #require_once 'Zend/Pdf/Exception.php';
- throw new Zend_Pdf_Exception('Outlines Type entry must be an \'Outlines\' string.');
- }
- if ($root->Outlines->First === null) {
- return;
- }
- $outlineDictionary = $root->Outlines->First;
- $processedDictionaries = new SplObjectStorage();
- while ($outlineDictionary !== null && !$processedDictionaries->contains($outlineDictionary)) {
- $processedDictionaries->attach($outlineDictionary);
- #require_once 'Zend/Pdf/Outline/Loaded.php';
- $this->outlines[] = new Zend_Pdf_Outline_Loaded($outlineDictionary);
- $outlineDictionary = $outlineDictionary->Next;
- }
- $this->_originalOutlines = $this->outlines;
- if ($root->Outlines->Count !== null) {
- $this->_originalOpenOutlinesCount = $root->Outlines->Count->value;
- }
- }
- /**
- * Load JavaScript
- *
- * Populates the _javaScript string, for later use of getJavaScript method.
- *
- * @param Zend_Pdf_Element_Reference $root Document catalog entry
- */
- protected function _loadJavaScript(Zend_Pdf_Element_Reference $root)
- {
- if (null === $root->Names || null === $root->Names->JavaScript
- || null === $root->Names->JavaScript->Names
- ) {
- return;
- }
- foreach ($root->Names->JavaScript->Names->items as $item) {
- if ($item instanceof Zend_Pdf_Element_Reference
- && $item->S->value === 'JavaScript'
- ) {
- $this->_javaScript[] = $item->JS->value;
- }
- }
- }
- /**
- * Load form fields
- *
- * Populates the _formFields array, for later lookup of fields by name
- *
- * @param Zend_Pdf_Element_Reference $root Document catalog entry
- */
- protected function _loadFormFields(Zend_Pdf_Element_Reference $root)
- {
- if ($root->AcroForm === null || $root->AcroForm->Fields === null) {
- return;
- }
- foreach ($root->AcroForm->Fields->items as $field) {
- /* We only support fields that are textfields and have a name */
- if ($field->FT && $field->FT->value == 'Tx' && $field->T
- && $field->T !== null
- ) {
- $this->_formFields[$field->T->value] = $field;
- }
- }
- if (!$root->AcroForm->NeedAppearances
- || !$root->AcroForm->NeedAppearances->value
- ) {
- /* Ask the .pdf viewer to generate its own appearance data, so we do not have to */
- $root->AcroForm->add(
- new Zend_Pdf_Element_Name('NeedAppearances'),
- new Zend_Pdf_Element_Boolean(true)
- );
- $root->AcroForm->touch();
- }
- }
- /**
- * Retrieves a list with the names of the AcroForm textfields in the PDF
- *
- * @return array of strings
- */
- public function getTextFieldNames()
- {
- return array_keys($this->_formFields);
- }
- /**
- * Sets the value of an AcroForm text field
- *
- * @param string $name Name of textfield
- * @param string $value Value
- * @throws Zend_Pdf_Exception if the textfield does not exist in the pdf
- */
- public function setTextField($name, $value)
- {
- if (!isset($this->_formFields[$name])) {
- throw new Zend_Pdf_Exception(
- "Field '$name' does not exist or is not a textfield"
- );
- }
- /** @var Zend_Pdf_Element $field */
- $field = $this->_formFields[$name];
- $field->add(
- new Zend_Pdf_Element_Name('V'), new Zend_Pdf_Element_String($value)
- );
- $field->touch();
- }
- /**
- * Sets the properties for an AcroForm text field
- *
- * @param string $name
- * @param mixed $bitmask
- * @throws Zend_Pdf_Exception
- */
- public function setTextFieldProperties($name, $bitmask)
- {
- if (!isset($this->_formFields[$name])) {
- throw new Zend_Pdf_Exception(
- "Field '$name' does not exist or is not a textfield"
- );
- }
- $field = $this->_formFields[$name];
- $field->add(
- new Zend_Pdf_Element_Name('Ff'),
- new Zend_Pdf_Element_Numeric($bitmask)
- );
- $field->touch();
- }
- /**
- * Marks an AcroForm text field as read only
- *
- * @param string $name
- */
- public function markTextFieldAsReadOnly($name)
- {
- $this->setTextFieldProperties($name, self::PDF_FORM_FIELD_READONLY);
- }
- /**
- * Orginize pages to tha pages tree structure.
- *
- * @todo atomatically attach page to the document, if it's not done yet.
- * @todo check, that page is attached to the current document
- *
- * @todo Dump pages as a balanced tree instead of a plain set.
- */
- protected function _dumpPages()
- {
- $root = $this->_trailer->Root;
- $pagesContainer = $root->Pages;
- $pagesContainer->touch();
- $pagesContainer->Kids->items = array();
- foreach ($this->pages as $page ) {
- $page->render($this->_objFactory);
- $pageDictionary = $page->getPageDictionary();
- $pageDictionary->touch();
- $pageDictionary->Parent = $pagesContainer;
- $pagesContainer->Kids->items[] = $pageDictionary;
- }
- $this->_refreshPagesHash();
- $pagesContainer->Count->touch();
- $pagesContainer->Count->value = count($this->pages);
- // Refresh named destinations list
- foreach ($this->_namedTargets as $name => $namedTarget) {
- if ($namedTarget instanceof Zend_Pdf_Destination_Explicit) {
- // Named target is an explicit destination
- if ($this->resolveDestination($namedTarget, false) === null) {
- unset($this->_namedTargets[$name]);
- }
- } else if ($namedTarget instanceof Zend_Pdf_Action) {
- // Named target is an action
- if ($this->_cleanUpAction($namedTarget, false) === null) {
- // Action is a GoTo action with an unresolved destination
- unset($this->_namedTargets[$name]);
- }
- } else {
- #require_once 'Zend/Pdf/Exception.php';
- throw new Zend_Pdf_Exception('Wrong type of named targed (\'' . get_class($namedTarget) . '\').');
- }
- }
- // Refresh outlines
- #require_once 'Zend/Pdf/RecursivelyIteratableObjectsContainer.php';
- $iterator = new RecursiveIteratorIterator(new Zend_Pdf_RecursivelyIteratableObjectsContainer($this->outlines), RecursiveIteratorIterator::SELF_FIRST);
- foreach ($iterator as $outline) {
- $target = $outline->getTarget();
- if ($target !== null) {
- if ($target instanceof Zend_Pdf_Destination) {
- // Outline target is a destination
- if ($this->resolveDestination($target, false) === null) {
- $outline->setTarget(null);
- }
- } else if ($target instanceof Zend_Pdf_Action) {
- // Outline target is an action
- if ($this->_cleanUpAction($target, false) === null) {
- // Action is a GoTo action with an unresolved destination
- $outline->setTarget(null);
- }
- } else {
- #require_once 'Zend/Pdf/Exception.php';
- throw new Zend_Pdf_Exception('Wrong outline target.');
- }
- }
- }
- $openAction = $this->getOpenAction();
- if ($openAction !== null) {
- if ($openAction instanceof Zend_Pdf_Action) {
- // OpenAction is an action
- if ($this->_cleanUpAction($openAction, false) === null) {
- // Action is a GoTo action with an unresolved destination
- $this->setOpenAction(null);
- }
- } else if ($openAction instanceof Zend_Pdf_Destination) {
- // OpenAction target is a destination
- if ($this->resolveDestination($openAction, false) === null) {
- $this->setOpenAction(null);
- }
- } else {
- #require_once 'Zend/Pdf/Exception.php';
- throw new Zend_Pdf_Exception('OpenAction has to be either PDF Action or Destination.');
- }
- }
- }
- /**
- * Dump named destinations
- *
- * @todo Create a balanced tree instead of plain structure.
- */
- protected function _dumpNamedDestinations()
- {
- ksort($this->_namedTargets, SORT_STRING);
- $destArrayItems = array();
- foreach ($this->_namedTargets as $name => $destination) {
- $destArrayItems[] = new Zend_Pdf_Element_String($name);
- if ($destination instanceof Zend_Pdf_Target) {
- $destArrayItems[] = $destination->getResource();
- } else {
- #require_once 'Zend/Pdf/Exception.php';
- throw new Zend_Pdf_Exception('PDF named destinations must be a Zend_Pdf_Target object.');
- }
- }
- $destArray = $this->_objFactory->newObject(new Zend_Pdf_Element_Array($destArrayItems));
- $DestTree = $this->_objFactory->newObject(new Zend_Pdf_Element_Dictionary());
- $DestTree->Names = $destArray;
- $root = $this->_trailer->Root;
- if ($root->Names === null) {
- $root->touch();
- $root->Names = $this->_objFactory->newObject(new Zend_Pdf_Element_Dictionary());
- } else {
- $root->Names->touch();
- }
- $root->Names->Dests = $DestTree;
- }
- /**
- * Dump outlines recursively
- */
- protected function _dumpOutlines()
- {
- $root = $this->_trailer->Root;
- if ($root->Outlines === null) {
- if (count($this->outlines) == 0) {
- return;
- } else {
- $root->Outlines = $this->_objFactory->newObject(new Zend_Pdf_Element_Dictionary());
- $root->Outlines->Type = new Zend_Pdf_Element_Name('Outlines');
- $updateOutlinesNavigation = true;
- }
- } else {
- $updateOutlinesNavigation = false;
- if (count($this->_originalOutlines) != count($this->outlines)) {
- // If original and current outlines arrays have different size then outlines list was updated
- $updateOutlinesNavigation = true;
- } else if ( !(array_keys($this->_originalOutlines) === array_keys($this->outlines)) ) {
- // If original and current outlines arrays have different keys (with a glance to an order) then outlines list was updated
- $updateOutlinesNavigation = true;
- } else {
- foreach ($this->outlines as $key => $outline) {
- if ($this->_originalOutlines[$key] !== $outline) {
- $updateOutlinesNavigation = true;
- }
- }
- }
- }
- $lastOutline = null;
- $openOutlinesCount = 0;
- if ($updateOutlinesNavigation) {
- $root->Outlines->touch();
- $root->Outlines->First = null;
- foreach ($this->outlines as $outline) {
- if ($lastOutline === null) {
- // First pass. Update Outlines dictionary First entry using corresponding value
- $lastOutline = $outline->dumpOutline($this->_objFactory, $updateOutlinesNavigation, $root->Outlines);
- $root->Outlines->First = $lastOutline;
- } else {
- // Update previous outline dictionary Next entry (Prev is updated within dumpOutline() method)
- $currentOutlineDictionary = $outline->dumpOutline($this->_objFactory, $updateOutlinesNavigation, $root->Outlines, $lastOutline);
- $lastOutline->Next = $currentOutlineDictionary;
- $lastOutline = $currentOutlineDictionary;
- }
- $openOutlinesCount += $outline->openOutlinesCount();
- }
- $root->Outlines->Last = $lastOutline;
- } else {
- foreach ($this->outlines as $outline) {
- $lastOutline = $outline->dumpOutline($this->_objFactory, $updateOutlinesNavigation, $root->Outlines, $lastOutline);
- $openOutlinesCount += $outline->openOutlinesCount();
- }
- }
- if ($openOutlinesCount != $this->_originalOpenOutlinesCount) {
- $root->Outlines->touch;
- $root->Outlines->Count = new Zend_Pdf_Element_Numeric($openOutlinesCount);
- }
- }
- /**
- * Create page object, attached to the PDF document.
- * Method signatures:
- *
- * 1. 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);
- * ---------------------------------------------------------
- *
- * 2. 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);
- * ---------------------------------------------------------
- *
- * @param mixed $param1
- * @param mixed $param2
- * @return Zend_Pdf_Page
- */
- public function newPage($param1, $param2 = null)
- {
- #require_once 'Zend/Pdf/Page.php';
- if ($param2 === null) {
- return new Zend_Pdf_Page($param1, $this->_objFactory);
- } else {
- return new Zend_Pdf_Page($param1, $param2, $this->_objFactory);
- }
- }
- /**
- * Return the document-level Metadata
- * or null Metadata stream is not presented
- *
- * @return string
- */
- public function getMetadata()
- {
- if ($this->_trailer->Root->Metadata !== null) {
- return $this->_trailer->Root->Metadata->value;
- } else {
- return null;
- }
- }
- /**
- * Sets the document-level Metadata (mast be valid XMP document)
- *
- * @param string $metadata
- */
- public function setMetadata($metadata)
- {
- $metadataObject = $this->_objFactory->newStreamObject($metadata);
- $metadataObject->dictionary->Type = new Zend_Pdf_Element_Name('Metadata');
- $metadataObject->dictionary->Subtype = new Zend_Pdf_Element_Name('XML');
- $this->_trailer->Root->Metadata = $metadataObject;
- $this->_trailer->Root->touch();
- }
- /**
- * Return the document-level JavaScript
- * or null if there is no JavaScript for this document
- *
- * @return string
- */
- public function getJavaScript()
- {
- return $this->_javaScript;
- }
- /**
- * Get open Action
- * Returns Zend_Pdf_Target (Zend_Pdf_Destination or Zend_Pdf_Action object)
- *
- * @return Zend_Pdf_Target
- */
- public function getOpenAction()
- {
- if ($this->_trailer->Root->OpenAction !== null) {
- #require_once 'Zend/Pdf/Target.php';
- return Zend_Pdf_Target::load($this->_trailer->Root->OpenAction);
- } else {
- return null;
- }
- }
- /**
- * Set open Action which is actually Zend_Pdf_Destination or Zend_Pdf_Action object
- *
- * @param Zend_Pdf_Target $openAction
- * @returns Zend_Pdf
- */
- public function setOpenAction(Zend_Pdf_Target $openAction = null)
- {
- $root = $this->_trailer->Root;
- $root->touch();
- if ($openAction === null) {
- $root->OpenAction = null;
- } else {
- $root->OpenAction = $openAction->getResource();
- if ($openAction instanceof Zend_Pdf_Action) {
- $openAction->dumpAction($this->_objFactory);
- }
- }
- return $this;
- }
- /**
- * Return an associative array containing all the named destinations (or GoTo actions) in the PDF.
- * Named targets can be used to reference from outside
- * the PDF, ex: 'http://www.something.com/mydocument.pdf#MyAction'
- *
- * @return array
- */
- public function getNamedDestinations()
- {
- return $this->_namedTargets;
- }
- /**
- * Return specified named destination
- *
- * @param string $name
- * @return Zend_Pdf_Destination_Explicit|Zend_Pdf_Action_GoTo
- */
- public function getNamedDestination($name)
- {
- if (isset($this->_namedTargets[$name])) {
- return $this->_namedTargets[$name];
- } else {
- return null;
- }
- }
- /**
- * Set specified named destination
- *
- * @param string $name
- * @param Zend_Pdf_Destination_Explicit|Zend_Pdf_Action_GoTo $destination
- * @throws Zend_Pdf_Exception
- */
- public function setNamedDestination($name, $destination = null)
- {
- if ($destination !== null &&
- !$destination instanceof Zend_Pdf_Action_GoTo &&
- !$destination instanceof Zend_Pdf_Destination_Explicit) {
- #require_once 'Zend/Pdf/Exception.php';
- throw new Zend_Pdf_Exception('PDF named destination must refer an explicit destination or a GoTo PDF action.');
- }
- if ($destination !== null) {
- $this->_namedTargets[$name] = $destination;
- } else {
- unset($this->_namedTargets[$name]);
- }
- }
- /**
- * Pages collection hash:
- * <page dictionary object hash id> => Zend_Pdf_Page
- *
- * @var SplObjectStorage
- */
- protected $_pageReferences = null;
- /**
- * Pages collection hash:
- * <page number> => Zend_Pdf_Page
- *
- * @var array
- */
- protected $_pageNumbers = null;
- /**
- * Refresh page collection hashes
- *
- * @return Zend_Pdf
- */
- protected function _refreshPagesHash()
- {
- $this->_pageReferences = array();
- $this->_pageNumbers = array();
- $count = 1;
- foreach ($this->pages as $page) {
- $pageDictionaryHashId = spl_object_hash($page->getPageDictionary()->getObject());
- $this->_pageReferences[$pageDictionaryHashId] = $page;
- $this->_pageNumbers[$count++] = $page;
- }
- return $this;
- }
- /**
- * Resolve destination.
- *
- * Returns Zend_Pdf_Page page object or null if destination is not found within PDF document.
- *
- * @param Zend_Pdf_Destination $destination Destination to resolve
- * @param bool $refreshPageCollectionHashes Refresh page collection hashes before processing
- * @return Zend_Pdf_Page|null
- * @throws Zend_Pdf_Exception
- */
- public function resolveDestination(Zend_Pdf_Destination $destination, $refreshPageCollectionHashes = true)
- {
- if ($this->_pageReferences === null || $refreshPageCollectionHashes) {
- $this->_refreshPagesHash();
- }
- if ($destination instanceof Zend_Pdf_Destination_Named) {
- if (!isset($this->_namedTargets[$destination->getName()])) {
- return null;
- }
- $destination = $this->getNamedDestination($destination->getName());
- if ($destination instanceof Zend_Pdf_Action) {
- if (!$destination instanceof Zend_Pdf_Action_GoTo) {
- return null;
- }
- $destination = $destination->getDestination();
- }
- if (!$destination instanceof Zend_Pdf_Destination_Explicit) {
- #require_once 'Zend/Pdf/Exception.php';
- throw new Zend_Pdf_Exception('Named destination target has to be an explicit destination.');
- }
- }
- // Named target is an explicit destination
- $pageElement = $destination->getResource()->items[0];
- if ($pageElement->getType() == Zend_Pdf_Element::TYPE_NUMERIC) {
- // Page reference is a PDF number
- if (!isset($this->_pageNumbers[$pageElement->value])) {
- return null;
- }
- return $this->_pageNumbers[$pageElement->value];
- }
- // Page reference is a PDF page dictionary reference
- $pageDictionaryHashId = spl_object_hash($pageElement->getObject());
- if (!isset($this->_pageReferences[$pageDictionaryHashId])) {
- return null;
- }
- return $this->_pageReferences[$pageDictionaryHashId];
- }
- /**
- * Walk through action and its chained actions tree and remove nodes
- * if they are GoTo actions with an unresolved target.
- *
- * Returns null if root node is deleted or updated action overwise.
- *
- * @todo Give appropriate name and make method public
- *
- * @param Zend_Pdf_Action $action
- * @param bool $refreshPageCollectionHashes Refresh page collection hashes before processing
- * @return Zend_Pdf_Action|null
- */
- protected function _cleanUpAction(Zend_Pdf_Action $action, $refreshPageCollectionHashes = true)
- {
- if ($this->_pageReferences === null || $refreshPageCollectionHashes) {
- $this->_refreshPagesHash();
- }
- // Named target is an action
- if ($action instanceof Zend_Pdf_Action_GoTo &&
- $this->resolveDestination($action->getDestination(), false) === null) {
- // Action itself is a GoTo action with an unresolved destination
- return null;
- }
- // Walk through child actions
- $iterator = new RecursiveIteratorIterator($action, RecursiveIteratorIterator::SELF_FIRST);
- $actionsToClean = array();
- $deletionCandidateKeys = array();
- foreach ($iterator as $chainedAction) {
- if ($chainedAction instanceof Zend_Pdf_Action_GoTo &&
- $this->resolveDestination($chainedAction->getDestination(), false) === null) {
- // Some child action is a GoTo action with an unresolved destination
- // Mark it as a candidate for deletion
- $actionsToClean[] = $iterator->getSubIterator();
- $deletionCandidateKeys[] = $iterator->getSubIterator()->key();
- }
- }
- foreach ($actionsToClean as $id => $action) {
- unset($action->next[$deletionCandidateKeys[$id]]);
- }
- return $action;
- }
- /**
- * Extract fonts attached to the document
- *
- * returns array of Zend_Pdf_Resource_Font_Extracted objects
- *
- * @return array
- * @throws Zend_Pdf_Exception
- */
- public function extractFonts()
- {
- $fontResourcesUnique = array();
- foreach ($this->pages as $page) {
- $pageResources = $page->extractResources();
- if ($pageResources->Font === null) {
- // Page doesn't contain have any font reference
- continue;
- }
- $fontResources = $pageResources->Font;
- foreach ($fontResources->getKeys() as $fontResourceName) {
- $fontDictionary = $fontResources->$fontResourceName;
- if (! ($fontDictionary instanceof Zend_Pdf_Element_Reference ||
- $fontDictionary instanceof Zend_Pdf_Element_Object) ) {
- #require_once 'Zend/Pdf/Exception.php';
- throw new Zend_Pdf_Exception('Font dictionary has to be an indirect object or object reference.');
- }
- $fontResourcesUnique[spl_object_hash($fontDictionary->getObject())] = $fontDictionary;
- }
- }
- $fonts = array();
- #require_once 'Zend/Pdf/Exception.php';
- foreach ($fontResourcesUnique as $resourceId => $fontDictionary) {
- try {
- // Try to extract font
- #require_once 'Zend/Pdf/Resource/Font/Extracted.php';
- $extractedFont = new Zend_Pdf_Resource_Font_Extracted($fontDictionary);
- $fonts[$resourceId] = $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
- *
- * @param string $fontName
- * @return Zend_Pdf_Resource_Font_Extracted|null
- * @throws Zend_Pdf_Exception
- */
- public function extractFont($fontName)
- {
- $fontResourcesUnique = array();
- #require_once 'Zend/Pdf/Exception.php';
- foreach ($this->pages as $page) {
- $pageResources = $page->extractResources();
- if ($pageResources->Font === null) {
- // Page doesn't contain have any font reference
- continue;
- }
- $fontResources = $pageResources->Font;
- foreach ($fontResources->getKeys() as $fontResourceName) {
- $fontDictionary = $fontResources->$fontResourceName;
- if (! ($fontDictionary instanceof Zend_Pdf_Element_Reference ||
- $fontDictionary instanceof Zend_Pdf_Element_Object) ) {
- #require_once 'Zend/Pdf/Exception.php';
- throw new Zend_Pdf_Exception('Font dictionary has to be an indirect object or object reference.');
- }
- $resourceId = spl_object_hash($fontDictionary->getObject());
- if (isset($fontResourcesUnique[$resourceId])) {
- continue;
- } else {
- // Mark resource as processed
- $fontResourcesUnique[$resourceId] = 1;
- }
- if ($fontDictionary->BaseFont->value != $fontName) {
- continue;
- }
- try {
- // Try to extract font
- #require_once 'Zend/Pdf/Resource/Font/Extracted.php';
- return new Zend_Pdf_Resource_Font_Extracted($fontDictionary);
- } catch (Zend_Pdf_Exception $e) {
- if ($e->getMessage() != 'Unsupported font type.') {
- throw $e;
- }
- // Continue searhing
- }
- }
- }
- return null;
- }
- /**
- * Render the completed PDF to a string.
- * If $newSegmentOnly is true and it's not a new document,
- * then only appended part of PDF is returned.
- *
- * @param boolean $newSegmentOnly
- * @param resource $outputStream
- * @return string
- * @throws Zend_Pdf_Exception
- */
- public function render($newSegmentOnly = false, $outputStream = null)
- {
- if ($this->_isNewDocument) {
- // Drop full document first time even $newSegmentOnly is set to true
- $newSegmentOnly = false;
- $this->_isNewDocument = false;
- }
- // Save document properties if necessary
- if ($this->properties != $this->_originalProperties) {
- $docInfo = $this->_objFactory->newObject(new Zend_Pdf_Element_Dictionary());
- foreach ($this->properties as $key => $value) {
- switch ($key) {
- case 'Trapped':
- switch ($value) {
- case true:
- $docInfo->$key = new Zend_Pdf_Element_Name('True');
- break;
- case false:
- $docInfo->$key = new Zend_Pdf_Element_Name('False');
- break;
- case null:
- $docInfo->$key = new Zend_Pdf_Element_Name('Unknown');
- break;
- default:
- #require_once 'Zend/Pdf/Exception.php';
- throw new Zend_Pdf_Exception('Wrong Trapped document property vale: \'' . $value . '\'. Only true, false and null values are allowed.');
- break;
- }
- case 'CreationDate':
- // break intentionally omitted
- case 'ModDate':
- $docInfo->$key = new Zend_Pdf_Element_String((string)$value);
- break;
- case 'Title':
- // break intentionally omitted
- case 'Author':
- // break intentionally omitted
- case 'Subject':
- // break intentionally omitted
- case 'Keywords':
- // break intentionally omitted
- case 'Creator':
- // break intentionally omitted
- case 'Producer':
- if (extension_loaded('mbstring') === true) {
- $detected = mb_detect_encoding($value);
- if ($detected !== 'ASCII') {
- $value = "\xfe\xff" . mb_convert_encoding($value, 'UTF-16', $detected);
- }
- }
- $docInfo->$key = new Zend_Pdf_Element_String((string)$value);
- break;
- default:
- // Set property using PDF type based on PHP type
- $docInfo->$key = Zend_Pdf_Element::phpToPdf($value);
- break;
- }
- }
- $this->_trailer->Info = $docInfo;
- }
- $this->_dumpPages();
- $this->_dumpNamedDestinations();
- $this->_dumpOutlines();
- // Check, that PDF file was modified
- // File is always modified by _dumpPages() now, but future implementations may eliminate this.
- if (!$this->_objFactory->isModified()) {
- if ($newSegmentOnly) {
- // Do nothing, return
- return '';
- }
- if ($outputStream === null) {
- return $this->_trailer->getPDFString();
- } else {
- $pdfData = $this->_trailer->getPDFString();
- while ( strlen($pdfData) > 0 && ($byteCount = fwrite($outputStream, $pdfData)) != false ) {
- $pdfData = substr($pdfData, $byteCount);
- }
- return '';
- }
- }
- // offset (from a start of PDF file) of new PDF file segment
- $offset = $this->_trailer->getPDFLength();
- // Last Object number in a list of free objects
- $lastFreeObject = $this->_trailer->getLastFreeObject();
- // Array of cross-reference table subsections
- $xrefTable = array();
- // Object numbers of first objects in each subsection
- $xrefSectionStartNums = array();
- // Last cross-reference table subsection
- $xrefSection = array();
- // Dummy initialization of the first element (specail case - header of linked list of free objects).
- $xrefSection[] = 0;
- $xrefSectionStartNums[] = 0;
- // Object number of last processed PDF object.
- // Used to manage cross-reference subsections.
- // Initialized by zero (specail case - header of linked list of free objects).
- $lastObjNum = 0;
- if ($outputStream !== null) {
- if (!$newSegmentOnly) {
- $pdfData = $this->_trailer->getPDFString();
- while ( strlen($pdfData) > 0 && ($byteCount = fwrite($outputStream, $pdfData)) != false ) {
- $pdfData = substr($pdfData, $byteCount);
- }
- }
- } else {
- $pdfSegmentBlocks = ($newSegmentOnly) ? array() : array($this->_trailer->getPDFString());
- }
- // Iterate objects to create new reference table
- foreach ($this->_objFactory->listModifiedObjects() as $updateInfo) {
- $objNum = $updateInfo->getObjNum();
- if ($objNum - $lastObjNum != 1) {
- // Save cross-reference table subsection and start new one
- $xrefTable[] = $xrefSection;
- $xrefSection = array();
- $xrefSectionStartNums[] = $objNum;
- }
- if ($updateInfo->isFree()) {
- // Free object cross-reference table entry
- $xrefSection[] = sprintf("%010d %05d f \n", $lastFreeObject, $updateInfo->getGenNum());
- $lastFreeObject = $objNum;
- } else {
- // In-use object cross-reference table entry
- $xrefSection[] = sprintf("%010d %05d n \n", $offset, $updateInfo->getGenNum());
- $pdfBlock = $updateInfo->getObjectDump();
- $offset += strlen($pdfBlock);
- if ($outputStream === null) {
- $pdfSegmentBlocks[] = $pdfBlock;
- } else {
- while ( strlen($pdfBlock) > 0 && ($byteCount = fwrite($outputStream, $pdfBlock)) != false ) {
- $pdfBlock = substr($pdfBlock, $byteCount);
- }
- }
- }
- $lastObjNum = $objNum;
- }
- // Save last cross-reference table subsection
- $xrefTable[] = $xrefSection;
- // Modify first entry (specail case - header of linked list of free objects).
- $xrefTable[0][0] = sprintf("%010d 65535 f \n", $lastFreeObject);
- $xrefTableStr = "xref\n";
- foreach ($xrefTable as $sectId => $xrefSection) {
- $xrefTableStr .= sprintf("%d %d \n", $xrefSectionStartNums[$sectId], count($xrefSection));
- foreach ($xrefSection as $xrefTableEntry) {
- $xrefTableStr .= $xrefTableEntry;
- }
- }
- $this->_trailer->Size->value = $this->_objFactory->getObjectCount();
- $pdfBlock = $xrefTableStr
- . $this->_trailer->toString()
- . "startxref\n" . $offset . "\n"
- . "%%EOF\n";
- $this->_objFactory->cleanEnumerationShiftCache();
- if ($outputStream === null) {
- $pdfSegmentBlocks[] = $pdfBlock;
- return implode('', $pdfSegmentBlocks);
- } else {
- while ( strlen($pdfBlock) > 0 && ($byteCount = fwrite($outputStream, $pdfBlock)) != false ) {
- $pdfBlock = substr($pdfBlock, $byteCount);
- }
- return '';
- }
- }
- /**
- * Sets the document-level JavaScript
- *
- * Resets and appends
- *
- * @param string|array $javaScript
- */
- public function setJavaScript($javaScript)
- {
- $this->resetJavaScript();
- $this->addJavaScript($javaScript);
- }
- /**
- * Resets the document-level JavaScript
- */
- public function resetJavaScript()
- {
- $this->_javaScript = null;
- $root = $this->_trailer->Root;
- if (null === $root->Names || null === $root->Names->JavaScript) {
- return;
- }
- $root->Names->JavaScript = null;
- }
- /**
- * Appends JavaScript to the document-level JavaScript
- *
- * @param string|array $javaScript
- * @throws Zend_Pdf_Exception
- */
- public function addJavaScript($javaScript)
- {
- if (empty($javaScript)) {
- throw new Zend_Pdf_Exception(
- 'JavaScript must be a non empty string or array of strings'
- );
- }
- if (!is_array($javaScript)) {
- $javaScript = array($javaScript);
- }
- if (null === $this->_javaScript) {
- $this->_javaScript = $javaScript;
- } else {
- $this->_javaScript = array_merge($this->_javaScript, $javaScript);
- }
- if (!empty($this->_javaScript)) {
- $items = array();
- foreach ($this->_javaScript as $javaScript) {
- $jsCode = array(
- 'S' => new Zend_Pdf_Element_Name('JavaScript'),
- 'JS' => new Zend_Pdf_Element_String($javaScript)
- );
- $items[] = new Zend_Pdf_Element_String('EmbeddedJS');
- $items[] = $this->_objFactory->newObject(
- new Zend_Pdf_Element_Dictionary($jsCode)
- );
- }
- $jsRef = $this->_objFactory->newObject(
- new Zend_Pdf_Element_Dictionary(
- array('Names' => new Zend_Pdf_Element_Array($items))
- )
- );
- if (null === $this->_trailer->Root->Names) {
- $this->_trailer->Root->Names = new Zend_Pdf_Element_Dictionary();
- }
- $this->_trailer->Root->Names->JavaScript = $jsRef;
- }
- }
- /**
- * Convert date to PDF format (it's close to ASN.1 (Abstract Syntax Notation
- * One) defined in ISO/IEC 8824).
- *
- * @todo This really isn't the best location for this method. It should
- * probably actually exist as Zend_Pdf_Element_Date or something like that.
- *
- * @todo Address the following E_STRICT issue:
- * PHP Strict Standards: date(): It is not safe to rely on the system's
- * timezone settings. Please use the date.timezone setting, the TZ
- * environment variable or the date_default_timezone_set() function. In
- * case you used any of those methods and you are still getting this
- * warning, you most likely misspelled the timezone identifier.
- *
- * @param integer $timestamp (optional) If omitted, uses the current time.
- * @return string
- */
- public static function pdfDate($timestamp = null)
- {
- if ($timestamp === null) {
- $date = date('\D\:YmdHisO');
- } else {
- $date = date('\D\:YmdHisO', $timestamp);
- }
- return substr_replace($date, '\'', -2, 0) . '\'';
- }
- }
|