PageRenderTime 44ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/Pdf.php

https://bitbucket.org/goldie/zend-framework1
PHP | 1428 lines | 756 code | 194 blank | 478 comment | 167 complexity | 20f59d0d5435052f1c6a00525db2cc96 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Pdf
  17. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Pdf.php 23775 2011-03-01 17:25:24Z ralph $
  20. */
  21. /** User land classes and interfaces turned on by Zend/Pdf.php file inclusion. */
  22. /** @todo Section should be removed with ZF 2.0 release as obsolete */
  23. /** Zend_Pdf_Page */
  24. require_once 'Zend/Pdf/Page.php';
  25. /** Zend_Pdf_Style */
  26. require_once 'Zend/Pdf/Style.php';
  27. /** Zend_Pdf_Color_GrayScale */
  28. require_once 'Zend/Pdf/Color/GrayScale.php';
  29. /** Zend_Pdf_Color_Rgb */
  30. require_once 'Zend/Pdf/Color/Rgb.php';
  31. /** Zend_Pdf_Color_Cmyk */
  32. require_once 'Zend/Pdf/Color/Cmyk.php';
  33. /** Zend_Pdf_Color_Html */
  34. require_once 'Zend/Pdf/Color/Html.php';
  35. /** Zend_Pdf_Image */
  36. require_once 'Zend/Pdf/Image.php';
  37. /** Zend_Pdf_Font */
  38. require_once 'Zend/Pdf/Font.php';
  39. /** Zend_Pdf_Resource_Extractor */
  40. require_once 'Zend/Pdf/Resource/Extractor.php';
  41. /** Zend_Pdf_Canvas */
  42. require_once 'Zend/Pdf/Canvas.php';
  43. /** Internally used classes */
  44. require_once 'Zend/Pdf/Element.php';
  45. require_once 'Zend/Pdf/Element/Array.php';
  46. require_once 'Zend/Pdf/Element/String/Binary.php';
  47. require_once 'Zend/Pdf/Element/Boolean.php';
  48. require_once 'Zend/Pdf/Element/Dictionary.php';
  49. require_once 'Zend/Pdf/Element/Name.php';
  50. require_once 'Zend/Pdf/Element/Null.php';
  51. require_once 'Zend/Pdf/Element/Numeric.php';
  52. require_once 'Zend/Pdf/Element/String.php';
  53. /**
  54. * General entity which describes PDF document.
  55. * It implements document abstraction with a document level operations.
  56. *
  57. * Class is used to create new PDF document or load existing document.
  58. * See details in a class constructor description
  59. *
  60. * Class agregates document level properties and entities (pages, bookmarks,
  61. * document level actions, attachments, form object, etc)
  62. *
  63. * @category Zend
  64. * @package Zend_Pdf
  65. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  66. * @license http://framework.zend.com/license/new-bsd New BSD License
  67. */
  68. class Zend_Pdf
  69. {
  70. /**** Class Constants ****/
  71. /**
  72. * Version number of generated PDF documents.
  73. */
  74. const PDF_VERSION = '1.4';
  75. /**
  76. * PDF file header.
  77. */
  78. const PDF_HEADER = "%PDF-1.4\n%\xE2\xE3\xCF\xD3\n";
  79. /**
  80. * Pages collection
  81. *
  82. * @todo implement it as a class, which supports ArrayAccess and Iterator interfaces,
  83. * to provide incremental parsing and pages tree updating.
  84. * That will give good performance and memory (PDF size) benefits.
  85. *
  86. * @var array - array of Zend_Pdf_Page object
  87. */
  88. public $pages = array();
  89. /**
  90. * Document properties
  91. *
  92. * It's an associative array with PDF meta information, values may
  93. * be string, boolean or float.
  94. * Returned array could be used directly to access, add, modify or remove
  95. * document properties.
  96. *
  97. * Standard document properties: Title (must be set for PDF/X documents), Author,
  98. * Subject, Keywords (comma separated list), Creator (the name of the application,
  99. * that created document, if it was converted from other format), Trapped (must be
  100. * true, false or null, can not be null for PDF/X documents)
  101. *
  102. * @var array
  103. */
  104. public $properties = array();
  105. /**
  106. * Original properties set.
  107. *
  108. * Used for tracking properties changes
  109. *
  110. * @var array
  111. */
  112. protected $_originalProperties = array();
  113. /**
  114. * Document level javascript
  115. *
  116. * @var string
  117. */
  118. protected $_javaScript = null;
  119. /**
  120. * Document named destinations or "GoTo..." actions, used to refer
  121. * document parts from outside PDF
  122. *
  123. * @var array - array of Zend_Pdf_Target objects
  124. */
  125. protected $_namedTargets = array();
  126. /**
  127. * Document outlines
  128. *
  129. * @var array - array of Zend_Pdf_Outline objects
  130. */
  131. public $outlines = array();
  132. /**
  133. * Original document outlines list
  134. * Used to track outlines update
  135. *
  136. * @var array - array of Zend_Pdf_Outline objects
  137. */
  138. protected $_originalOutlines = array();
  139. /**
  140. * Original document outlines open elements count
  141. * Used to track outlines update
  142. *
  143. * @var integer
  144. */
  145. protected $_originalOpenOutlinesCount = 0;
  146. /**
  147. * Pdf trailer (last or just created)
  148. *
  149. * @var Zend_Pdf_Trailer
  150. */
  151. protected $_trailer = null;
  152. /**
  153. * PDF objects factory.
  154. *
  155. * @var Zend_Pdf_ElementFactory_Interface
  156. */
  157. protected $_objFactory = null;
  158. /**
  159. * Memory manager for stream objects
  160. *
  161. * @var Zend_Memory_Manager|null
  162. */
  163. protected static $_memoryManager = null;
  164. /**
  165. * Pdf file parser.
  166. * It's not used, but has to be destroyed only with Zend_Pdf object
  167. *
  168. * @var Zend_Pdf_Parser
  169. */
  170. protected $_parser;
  171. /**
  172. * List of inheritable attributesfor pages tree
  173. *
  174. * @var array
  175. */
  176. protected static $_inheritableAttributes = array('Resources', 'MediaBox', 'CropBox', 'Rotate');
  177. /**
  178. * True if the object is a newly created PDF document (affects save() method behavior)
  179. * False otherwise
  180. *
  181. * @var boolean
  182. */
  183. protected $_isNewDocument = true;
  184. /**
  185. * Request used memory manager
  186. *
  187. * @return Zend_Memory_Manager
  188. */
  189. static public function getMemoryManager()
  190. {
  191. if (self::$_memoryManager === null) {
  192. require_once 'Zend/Memory.php';
  193. self::$_memoryManager = Zend_Memory::factory('none');
  194. }
  195. return self::$_memoryManager;
  196. }
  197. /**
  198. * Set user defined memory manager
  199. *
  200. * @param Zend_Memory_Manager $memoryManager
  201. */
  202. static public function setMemoryManager(Zend_Memory_Manager $memoryManager)
  203. {
  204. self::$_memoryManager = $memoryManager;
  205. }
  206. /**
  207. * Create new PDF document from a $source string
  208. *
  209. * @param string $source
  210. * @param integer $revision
  211. * @return Zend_Pdf
  212. */
  213. public static function parse(&$source = null, $revision = null)
  214. {
  215. return new Zend_Pdf($source, $revision);
  216. }
  217. /**
  218. * Load PDF document from a file
  219. *
  220. * @param string $source
  221. * @param integer $revision
  222. * @return Zend_Pdf
  223. */
  224. public static function load($source = null, $revision = null)
  225. {
  226. return new Zend_Pdf($source, $revision, true);
  227. }
  228. /**
  229. * Render PDF document and save it.
  230. *
  231. * If $updateOnly is true and it's not a new document, then it only
  232. * appends new section to the end of file.
  233. *
  234. * @param string $filename
  235. * @param boolean $updateOnly
  236. * @throws Zend_Pdf_Exception
  237. */
  238. public function save($filename, $updateOnly = false)
  239. {
  240. if (($file = @fopen($filename, $updateOnly ? 'ab':'wb')) === false ) {
  241. require_once 'Zend/Pdf/Exception.php';
  242. throw new Zend_Pdf_Exception( "Can not open '$filename' file for writing." );
  243. }
  244. $this->render($updateOnly, $file);
  245. fclose($file);
  246. }
  247. /**
  248. * Creates or loads PDF document.
  249. *
  250. * If $source is null, then it creates a new document.
  251. *
  252. * If $source is a string and $load is false, then it loads document
  253. * from a binary string.
  254. *
  255. * If $source is a string and $load is true, then it loads document
  256. * from a file.
  257. * $revision used to roll back document to specified version
  258. * (0 - current version, 1 - previous version, 2 - ...)
  259. *
  260. * @param string $source - PDF file to load
  261. * @param integer $revision
  262. * @throws Zend_Pdf_Exception
  263. * @return Zend_Pdf
  264. */
  265. public function __construct($source = null, $revision = null, $load = false)
  266. {
  267. require_once 'Zend/Pdf/ElementFactory.php';
  268. $this->_objFactory = Zend_Pdf_ElementFactory::createFactory(1);
  269. if ($source !== null) {
  270. require_once 'Zend/Pdf/Parser.php';
  271. $this->_parser = new Zend_Pdf_Parser($source, $this->_objFactory, $load);
  272. $this->_pdfHeaderVersion = $this->_parser->getPDFVersion();
  273. $this->_trailer = $this->_parser->getTrailer();
  274. if ($this->_trailer->Encrypt !== null) {
  275. require_once 'Zend/Pdf/Exception.php';
  276. throw new Zend_Pdf_Exception('Encrypted document modification is not supported');
  277. }
  278. if ($revision !== null) {
  279. $this->rollback($revision);
  280. } else {
  281. $this->_loadPages($this->_trailer->Root->Pages);
  282. }
  283. $this->_loadNamedDestinations($this->_trailer->Root, $this->_parser->getPDFVersion());
  284. $this->_loadOutlines($this->_trailer->Root);
  285. if ($this->_trailer->Info !== null) {
  286. $this->properties = $this->_trailer->Info->toPhp();
  287. if (isset($this->properties['Trapped'])) {
  288. switch ($this->properties['Trapped']) {
  289. case 'True':
  290. $this->properties['Trapped'] = true;
  291. break;
  292. case 'False':
  293. $this->properties['Trapped'] = false;
  294. break;
  295. case 'Unknown':
  296. $this->properties['Trapped'] = null;
  297. break;
  298. default:
  299. // Wrong property value
  300. // Do nothing
  301. break;
  302. }
  303. }
  304. $this->_originalProperties = $this->properties;
  305. }
  306. $this->_isNewDocument = false;
  307. } else {
  308. $this->_pdfHeaderVersion = Zend_Pdf::PDF_VERSION;
  309. $trailerDictionary = new Zend_Pdf_Element_Dictionary();
  310. /**
  311. * Document id
  312. */
  313. $docId = md5(uniqid(rand(), true)); // 32 byte (128 bit) identifier
  314. $docIdLow = substr($docId, 0, 16); // first 16 bytes
  315. $docIdHigh = substr($docId, 16, 16); // second 16 bytes
  316. $trailerDictionary->ID = new Zend_Pdf_Element_Array();
  317. $trailerDictionary->ID->items[] = new Zend_Pdf_Element_String_Binary($docIdLow);
  318. $trailerDictionary->ID->items[] = new Zend_Pdf_Element_String_Binary($docIdHigh);
  319. $trailerDictionary->Size = new Zend_Pdf_Element_Numeric(0);
  320. require_once 'Zend/Pdf/Trailer/Generator.php';
  321. $this->_trailer = new Zend_Pdf_Trailer_Generator($trailerDictionary);
  322. /**
  323. * Document catalog indirect object.
  324. */
  325. $docCatalog = $this->_objFactory->newObject(new Zend_Pdf_Element_Dictionary());
  326. $docCatalog->Type = new Zend_Pdf_Element_Name('Catalog');
  327. $docCatalog->Version = new Zend_Pdf_Element_Name(Zend_Pdf::PDF_VERSION);
  328. $this->_trailer->Root = $docCatalog;
  329. /**
  330. * Pages container
  331. */
  332. $docPages = $this->_objFactory->newObject(new Zend_Pdf_Element_Dictionary());
  333. $docPages->Type = new Zend_Pdf_Element_Name('Pages');
  334. $docPages->Kids = new Zend_Pdf_Element_Array();
  335. $docPages->Count = new Zend_Pdf_Element_Numeric(0);
  336. $docCatalog->Pages = $docPages;
  337. }
  338. }
  339. /**
  340. * Retrive number of revisions.
  341. *
  342. * @return integer
  343. */
  344. public function revisions()
  345. {
  346. $revisions = 1;
  347. $currentTrailer = $this->_trailer;
  348. while ($currentTrailer->getPrev() !== null && $currentTrailer->getPrev()->Root !== null ) {
  349. $revisions++;
  350. $currentTrailer = $currentTrailer->getPrev();
  351. }
  352. return $revisions++;
  353. }
  354. /**
  355. * Rollback document $steps number of revisions.
  356. * This method must be invoked before any changes, applied to the document.
  357. * Otherwise behavior is undefined.
  358. *
  359. * @param integer $steps
  360. */
  361. public function rollback($steps)
  362. {
  363. for ($count = 0; $count < $steps; $count++) {
  364. if ($this->_trailer->getPrev() !== null && $this->_trailer->getPrev()->Root !== null) {
  365. $this->_trailer = $this->_trailer->getPrev();
  366. } else {
  367. break;
  368. }
  369. }
  370. $this->_objFactory->setObjectCount($this->_trailer->Size->value);
  371. // Mark content as modified to force new trailer generation at render time
  372. $this->_trailer->Root->touch();
  373. $this->pages = array();
  374. $this->_loadPages($this->_trailer->Root->Pages);
  375. }
  376. /**
  377. * Load pages recursively
  378. *
  379. * @param Zend_Pdf_Element_Reference $pages
  380. * @param array|null $attributes
  381. */
  382. protected function _loadPages(Zend_Pdf_Element_Reference $pages, $attributes = array())
  383. {
  384. if ($pages->getType() != Zend_Pdf_Element::TYPE_DICTIONARY) {
  385. require_once 'Zend/Pdf/Exception.php';
  386. throw new Zend_Pdf_Exception('Wrong argument');
  387. }
  388. foreach ($pages->getKeys() as $property) {
  389. if (in_array($property, self::$_inheritableAttributes)) {
  390. $attributes[$property] = $pages->$property;
  391. $pages->$property = null;
  392. }
  393. }
  394. foreach ($pages->Kids->items as $child) {
  395. if ($child->Type->value == 'Pages') {
  396. $this->_loadPages($child, $attributes);
  397. } else if ($child->Type->value == 'Page') {
  398. foreach (self::$_inheritableAttributes as $property) {
  399. if ($child->$property === null && array_key_exists($property, $attributes)) {
  400. /**
  401. * Important note.
  402. * If any attribute or dependant object is an indirect object, then it's still
  403. * shared between pages.
  404. */
  405. if ($attributes[$property] instanceof Zend_Pdf_Element_Object ||
  406. $attributes[$property] instanceof Zend_Pdf_Element_Reference) {
  407. $child->$property = $attributes[$property];
  408. } else {
  409. $child->$property = $this->_objFactory->newObject($attributes[$property]);
  410. }
  411. }
  412. }
  413. require_once 'Zend/Pdf/Page.php';
  414. $this->pages[] = new Zend_Pdf_Page($child, $this->_objFactory);
  415. }
  416. }
  417. }
  418. /**
  419. * Load named destinations recursively
  420. *
  421. * @param Zend_Pdf_Element_Reference $root Document catalog entry
  422. * @param string $pdfHeaderVersion
  423. * @throws Zend_Pdf_Exception
  424. */
  425. protected function _loadNamedDestinations(Zend_Pdf_Element_Reference $root, $pdfHeaderVersion)
  426. {
  427. if ($root->Version !== null && version_compare($root->Version->value, $pdfHeaderVersion, '>')) {
  428. $versionIs_1_2_plus = version_compare($root->Version->value, '1.1', '>');
  429. } else {
  430. $versionIs_1_2_plus = version_compare($pdfHeaderVersion, '1.1', '>');
  431. }
  432. if ($versionIs_1_2_plus) {
  433. // PDF version is 1.2+
  434. // Look for Destinations structure at Name dictionary
  435. if ($root->Names !== null && $root->Names->Dests !== null) {
  436. require_once 'Zend/Pdf/NameTree.php';
  437. require_once 'Zend/Pdf/Target.php';
  438. foreach (new Zend_Pdf_NameTree($root->Names->Dests) as $name => $destination) {
  439. $this->_namedTargets[$name] = Zend_Pdf_Target::load($destination);
  440. }
  441. }
  442. } else {
  443. // PDF version is 1.1 (or earlier)
  444. // Look for Destinations sructure at Dest entry of document catalog
  445. if ($root->Dests !== null) {
  446. if ($root->Dests->getType() != Zend_Pdf_Element::TYPE_DICTIONARY) {
  447. require_once 'Zend/Pdf/Exception.php';
  448. throw new Zend_Pdf_Exception('Document catalog Dests entry must be a dictionary.');
  449. }
  450. require_once 'Zend/Pdf/Target.php';
  451. foreach ($root->Dests->getKeys() as $destKey) {
  452. $this->_namedTargets[$destKey] = Zend_Pdf_Target::load($root->Dests->$destKey);
  453. }
  454. }
  455. }
  456. }
  457. /**
  458. * Load outlines recursively
  459. *
  460. * @param Zend_Pdf_Element_Reference $root Document catalog entry
  461. */
  462. protected function _loadOutlines(Zend_Pdf_Element_Reference $root)
  463. {
  464. if ($root->Outlines === null) {
  465. return;
  466. }
  467. if ($root->Outlines->getType() != Zend_Pdf_Element::TYPE_DICTIONARY) {
  468. require_once 'Zend/Pdf/Exception.php';
  469. throw new Zend_Pdf_Exception('Document catalog Outlines entry must be a dictionary.');
  470. }
  471. if ($root->Outlines->Type !== null && $root->Outlines->Type->value != 'Outlines') {
  472. require_once 'Zend/Pdf/Exception.php';
  473. throw new Zend_Pdf_Exception('Outlines Type entry must be an \'Outlines\' string.');
  474. }
  475. if ($root->Outlines->First === null) {
  476. return;
  477. }
  478. $outlineDictionary = $root->Outlines->First;
  479. $processedDictionaries = new SplObjectStorage();
  480. while ($outlineDictionary !== null && !$processedDictionaries->contains($outlineDictionary)) {
  481. $processedDictionaries->attach($outlineDictionary);
  482. require_once 'Zend/Pdf/Outline/Loaded.php';
  483. $this->outlines[] = new Zend_Pdf_Outline_Loaded($outlineDictionary);
  484. $outlineDictionary = $outlineDictionary->Next;
  485. }
  486. $this->_originalOutlines = $this->outlines;
  487. if ($root->Outlines->Count !== null) {
  488. $this->_originalOpenOutlinesCount = $root->Outlines->Count->value;
  489. }
  490. }
  491. /**
  492. * Orginize pages to tha pages tree structure.
  493. *
  494. * @todo atomatically attach page to the document, if it's not done yet.
  495. * @todo check, that page is attached to the current document
  496. *
  497. * @todo Dump pages as a balanced tree instead of a plain set.
  498. */
  499. protected function _dumpPages()
  500. {
  501. $root = $this->_trailer->Root;
  502. $pagesContainer = $root->Pages;
  503. $pagesContainer->touch();
  504. $pagesContainer->Kids->items = array();
  505. foreach ($this->pages as $page ) {
  506. $page->render($this->_objFactory);
  507. $pageDictionary = $page->getPageDictionary();
  508. $pageDictionary->touch();
  509. $pageDictionary->Parent = $pagesContainer;
  510. $pagesContainer->Kids->items[] = $pageDictionary;
  511. }
  512. $this->_refreshPagesHash();
  513. $pagesContainer->Count->touch();
  514. $pagesContainer->Count->value = count($this->pages);
  515. // Refresh named destinations list
  516. foreach ($this->_namedTargets as $name => $namedTarget) {
  517. if ($namedTarget instanceof Zend_Pdf_Destination_Explicit) {
  518. // Named target is an explicit destination
  519. if ($this->resolveDestination($namedTarget, false) === null) {
  520. unset($this->_namedTargets[$name]);
  521. }
  522. } else if ($namedTarget instanceof Zend_Pdf_Action) {
  523. // Named target is an action
  524. if ($this->_cleanUpAction($namedTarget, false) === null) {
  525. // Action is a GoTo action with an unresolved destination
  526. unset($this->_namedTargets[$name]);
  527. }
  528. } else {
  529. require_once 'Zend/Pdf/Exception.php';
  530. throw new Zend_Pdf_Exception('Wrong type of named targed (\'' . get_class($namedTarget) . '\').');
  531. }
  532. }
  533. // Refresh outlines
  534. require_once 'Zend/Pdf/RecursivelyIteratableObjectsContainer.php';
  535. $iterator = new RecursiveIteratorIterator(new Zend_Pdf_RecursivelyIteratableObjectsContainer($this->outlines), RecursiveIteratorIterator::SELF_FIRST);
  536. foreach ($iterator as $outline) {
  537. $target = $outline->getTarget();
  538. if ($target !== null) {
  539. if ($target instanceof Zend_Pdf_Destination) {
  540. // Outline target is a destination
  541. if ($this->resolveDestination($target, false) === null) {
  542. $outline->setTarget(null);
  543. }
  544. } else if ($target instanceof Zend_Pdf_Action) {
  545. // Outline target is an action
  546. if ($this->_cleanUpAction($target, false) === null) {
  547. // Action is a GoTo action with an unresolved destination
  548. $outline->setTarget(null);
  549. }
  550. } else {
  551. require_once 'Zend/Pdf/Exception.php';
  552. throw new Zend_Pdf_Exception('Wrong outline target.');
  553. }
  554. }
  555. }
  556. $openAction = $this->getOpenAction();
  557. if ($openAction !== null) {
  558. if ($openAction instanceof Zend_Pdf_Action) {
  559. // OpenAction is an action
  560. if ($this->_cleanUpAction($openAction, false) === null) {
  561. // Action is a GoTo action with an unresolved destination
  562. $this->setOpenAction(null);
  563. }
  564. } else if ($openAction instanceof Zend_Pdf_Destination) {
  565. // OpenAction target is a destination
  566. if ($this->resolveDestination($openAction, false) === null) {
  567. $this->setOpenAction(null);
  568. }
  569. } else {
  570. require_once 'Zend/Pdf/Exception.php';
  571. throw new Zend_Pdf_Exception('OpenAction has to be either PDF Action or Destination.');
  572. }
  573. }
  574. }
  575. /**
  576. * Dump named destinations
  577. *
  578. * @todo Create a balanced tree instead of plain structure.
  579. */
  580. protected function _dumpNamedDestinations()
  581. {
  582. ksort($this->_namedTargets, SORT_STRING);
  583. $destArrayItems = array();
  584. foreach ($this->_namedTargets as $name => $destination) {
  585. $destArrayItems[] = new Zend_Pdf_Element_String($name);
  586. if ($destination instanceof Zend_Pdf_Target) {
  587. $destArrayItems[] = $destination->getResource();
  588. } else {
  589. require_once 'Zend/Pdf/Exception.php';
  590. throw new Zend_Pdf_Exception('PDF named destinations must be a Zend_Pdf_Target object.');
  591. }
  592. }
  593. $destArray = $this->_objFactory->newObject(new Zend_Pdf_Element_Array($destArrayItems));
  594. $DestTree = $this->_objFactory->newObject(new Zend_Pdf_Element_Dictionary());
  595. $DestTree->Names = $destArray;
  596. $root = $this->_trailer->Root;
  597. if ($root->Names === null) {
  598. $root->touch();
  599. $root->Names = $this->_objFactory->newObject(new Zend_Pdf_Element_Dictionary());
  600. } else {
  601. $root->Names->touch();
  602. }
  603. $root->Names->Dests = $DestTree;
  604. }
  605. /**
  606. * Dump outlines recursively
  607. */
  608. protected function _dumpOutlines()
  609. {
  610. $root = $this->_trailer->Root;
  611. if ($root->Outlines === null) {
  612. if (count($this->outlines) == 0) {
  613. return;
  614. } else {
  615. $root->Outlines = $this->_objFactory->newObject(new Zend_Pdf_Element_Dictionary());
  616. $root->Outlines->Type = new Zend_Pdf_Element_Name('Outlines');
  617. $updateOutlinesNavigation = true;
  618. }
  619. } else {
  620. $updateOutlinesNavigation = false;
  621. if (count($this->_originalOutlines) != count($this->outlines)) {
  622. // If original and current outlines arrays have different size then outlines list was updated
  623. $updateOutlinesNavigation = true;
  624. } else if ( !(array_keys($this->_originalOutlines) === array_keys($this->outlines)) ) {
  625. // If original and current outlines arrays have different keys (with a glance to an order) then outlines list was updated
  626. $updateOutlinesNavigation = true;
  627. } else {
  628. foreach ($this->outlines as $key => $outline) {
  629. if ($this->_originalOutlines[$key] !== $outline) {
  630. $updateOutlinesNavigation = true;
  631. }
  632. }
  633. }
  634. }
  635. $lastOutline = null;
  636. $openOutlinesCount = 0;
  637. if ($updateOutlinesNavigation) {
  638. $root->Outlines->touch();
  639. $root->Outlines->First = null;
  640. foreach ($this->outlines as $outline) {
  641. if ($lastOutline === null) {
  642. // First pass. Update Outlines dictionary First entry using corresponding value
  643. $lastOutline = $outline->dumpOutline($this->_objFactory, $updateOutlinesNavigation, $root->Outlines);
  644. $root->Outlines->First = $lastOutline;
  645. } else {
  646. // Update previous outline dictionary Next entry (Prev is updated within dumpOutline() method)
  647. $currentOutlineDictionary = $outline->dumpOutline($this->_objFactory, $updateOutlinesNavigation, $root->Outlines, $lastOutline);
  648. $lastOutline->Next = $currentOutlineDictionary;
  649. $lastOutline = $currentOutlineDictionary;
  650. }
  651. $openOutlinesCount += $outline->openOutlinesCount();
  652. }
  653. $root->Outlines->Last = $lastOutline;
  654. } else {
  655. foreach ($this->outlines as $outline) {
  656. $lastOutline = $outline->dumpOutline($this->_objFactory, $updateOutlinesNavigation, $root->Outlines, $lastOutline);
  657. $openOutlinesCount += $outline->openOutlinesCount();
  658. }
  659. }
  660. if ($openOutlinesCount != $this->_originalOpenOutlinesCount) {
  661. $root->Outlines->touch;
  662. $root->Outlines->Count = new Zend_Pdf_Element_Numeric($openOutlinesCount);
  663. }
  664. }
  665. /**
  666. * Create page object, attached to the PDF document.
  667. * Method signatures:
  668. *
  669. * 1. Create new page with a specified pagesize.
  670. * If $factory is null then it will be created and page must be attached to the document to be
  671. * included into output.
  672. * ---------------------------------------------------------
  673. * new Zend_Pdf_Page(string $pagesize);
  674. * ---------------------------------------------------------
  675. *
  676. * 2. Create new page with a specified pagesize (in default user space units).
  677. * If $factory is null then it will be created and page must be attached to the document to be
  678. * included into output.
  679. * ---------------------------------------------------------
  680. * new Zend_Pdf_Page(numeric $width, numeric $height);
  681. * ---------------------------------------------------------
  682. *
  683. * @param mixed $param1
  684. * @param mixed $param2
  685. * @return Zend_Pdf_Page
  686. */
  687. public function newPage($param1, $param2 = null)
  688. {
  689. require_once 'Zend/Pdf/Page.php';
  690. if ($param2 === null) {
  691. return new Zend_Pdf_Page($param1, $this->_objFactory);
  692. } else {
  693. return new Zend_Pdf_Page($param1, $param2, $this->_objFactory);
  694. }
  695. }
  696. /**
  697. * Return the document-level Metadata
  698. * or null Metadata stream is not presented
  699. *
  700. * @return string
  701. */
  702. public function getMetadata()
  703. {
  704. if ($this->_trailer->Root->Metadata !== null) {
  705. return $this->_trailer->Root->Metadata->value;
  706. } else {
  707. return null;
  708. }
  709. }
  710. /**
  711. * Sets the document-level Metadata (mast be valid XMP document)
  712. *
  713. * @param string $metadata
  714. */
  715. public function setMetadata($metadata)
  716. {
  717. $metadataObject = $this->_objFactory->newStreamObject($metadata);
  718. $metadataObject->dictionary->Type = new Zend_Pdf_Element_Name('Metadata');
  719. $metadataObject->dictionary->Subtype = new Zend_Pdf_Element_Name('XML');
  720. $this->_trailer->Root->Metadata = $metadataObject;
  721. $this->_trailer->Root->touch();
  722. }
  723. /**
  724. * Return the document-level JavaScript
  725. * or null if there is no JavaScript for this document
  726. *
  727. * @return string
  728. */
  729. public function getJavaScript()
  730. {
  731. return $this->_javaScript;
  732. }
  733. /**
  734. * Get open Action
  735. * Returns Zend_Pdf_Target (Zend_Pdf_Destination or Zend_Pdf_Action object)
  736. *
  737. * @return Zend_Pdf_Target
  738. */
  739. public function getOpenAction()
  740. {
  741. if ($this->_trailer->Root->OpenAction !== null) {
  742. require_once 'Zend/Pdf/Target.php';
  743. return Zend_Pdf_Target::load($this->_trailer->Root->OpenAction);
  744. } else {
  745. return null;
  746. }
  747. }
  748. /**
  749. * Set open Action which is actually Zend_Pdf_Destination or Zend_Pdf_Action object
  750. *
  751. * @param Zend_Pdf_Target $openAction
  752. * @returns Zend_Pdf
  753. */
  754. public function setOpenAction(Zend_Pdf_Target $openAction = null)
  755. {
  756. $root = $this->_trailer->Root;
  757. $root->touch();
  758. if ($openAction === null) {
  759. $root->OpenAction = null;
  760. } else {
  761. $root->OpenAction = $openAction->getResource();
  762. if ($openAction instanceof Zend_Pdf_Action) {
  763. $openAction->dumpAction($this->_objFactory);
  764. }
  765. }
  766. return $this;
  767. }
  768. /**
  769. * Return an associative array containing all the named destinations (or GoTo actions) in the PDF.
  770. * Named targets can be used to reference from outside
  771. * the PDF, ex: 'http://www.something.com/mydocument.pdf#MyAction'
  772. *
  773. * @return array
  774. */
  775. public function getNamedDestinations()
  776. {
  777. return $this->_namedTargets;
  778. }
  779. /**
  780. * Return specified named destination
  781. *
  782. * @param string $name
  783. * @return Zend_Pdf_Destination_Explicit|Zend_Pdf_Action_GoTo
  784. */
  785. public function getNamedDestination($name)
  786. {
  787. if (isset($this->_namedTargets[$name])) {
  788. return $this->_namedTargets[$name];
  789. } else {
  790. return null;
  791. }
  792. }
  793. /**
  794. * Set specified named destination
  795. *
  796. * @param string $name
  797. * @param Zend_Pdf_Destination_Explicit|Zend_Pdf_Action_GoTo $target
  798. */
  799. public function setNamedDestination($name, $destination = null)
  800. {
  801. if ($destination !== null &&
  802. !$destination instanceof Zend_Pdf_Action_GoTo &&
  803. !$destination instanceof Zend_Pdf_Destination_Explicit) {
  804. require_once 'Zend/Pdf/Exception.php';
  805. throw new Zend_Pdf_Exception('PDF named destination must refer an explicit destination or a GoTo PDF action.');
  806. }
  807. if ($destination !== null) {
  808. $this->_namedTargets[$name] = $destination;
  809. } else {
  810. unset($this->_namedTargets[$name]);
  811. }
  812. }
  813. /**
  814. * Pages collection hash:
  815. * <page dictionary object hash id> => Zend_Pdf_Page
  816. *
  817. * @var SplObjectStorage
  818. */
  819. protected $_pageReferences = null;
  820. /**
  821. * Pages collection hash:
  822. * <page number> => Zend_Pdf_Page
  823. *
  824. * @var array
  825. */
  826. protected $_pageNumbers = null;
  827. /**
  828. * Refresh page collection hashes
  829. *
  830. * @return Zend_Pdf
  831. */
  832. protected function _refreshPagesHash()
  833. {
  834. $this->_pageReferences = array();
  835. $this->_pageNumbers = array();
  836. $count = 1;
  837. foreach ($this->pages as $page) {
  838. $pageDictionaryHashId = spl_object_hash($page->getPageDictionary()->getObject());
  839. $this->_pageReferences[$pageDictionaryHashId] = $page;
  840. $this->_pageNumbers[$count++] = $page;
  841. }
  842. return $this;
  843. }
  844. /**
  845. * Resolve destination.
  846. *
  847. * Returns Zend_Pdf_Page page object or null if destination is not found within PDF document.
  848. *
  849. * @param Zend_Pdf_Destination $destination Destination to resolve
  850. * @param boolean $refreshPagesHash Refresh page collection hashes before processing
  851. * @return Zend_Pdf_Page|null
  852. * @throws Zend_Pdf_Exception
  853. */
  854. public function resolveDestination(Zend_Pdf_Destination $destination, $refreshPageCollectionHashes = true)
  855. {
  856. if ($this->_pageReferences === null || $refreshPageCollectionHashes) {
  857. $this->_refreshPagesHash();
  858. }
  859. if ($destination instanceof Zend_Pdf_Destination_Named) {
  860. if (!isset($this->_namedTargets[$destination->getName()])) {
  861. return null;
  862. }
  863. $destination = $this->getNamedDestination($destination->getName());
  864. if ($destination instanceof Zend_Pdf_Action) {
  865. if (!$destination instanceof Zend_Pdf_Action_GoTo) {
  866. return null;
  867. }
  868. $destination = $destination->getDestination();
  869. }
  870. if (!$destination instanceof Zend_Pdf_Destination_Explicit) {
  871. require_once 'Zend/Pdf/Exception.php';
  872. throw new Zend_Pdf_Exception('Named destination target has to be an explicit destination.');
  873. }
  874. }
  875. // Named target is an explicit destination
  876. $pageElement = $destination->getResource()->items[0];
  877. if ($pageElement->getType() == Zend_Pdf_Element::TYPE_NUMERIC) {
  878. // Page reference is a PDF number
  879. if (!isset($this->_pageNumbers[$pageElement->value])) {
  880. return null;
  881. }
  882. return $this->_pageNumbers[$pageElement->value];
  883. }
  884. // Page reference is a PDF page dictionary reference
  885. $pageDictionaryHashId = spl_object_hash($pageElement->getObject());
  886. if (!isset($this->_pageReferences[$pageDictionaryHashId])) {
  887. return null;
  888. }
  889. return $this->_pageReferences[$pageDictionaryHashId];
  890. }
  891. /**
  892. * Walk through action and its chained actions tree and remove nodes
  893. * if they are GoTo actions with an unresolved target.
  894. *
  895. * Returns null if root node is deleted or updated action overwise.
  896. *
  897. * @todo Give appropriate name and make method public
  898. *
  899. * @param Zend_Pdf_Action $action
  900. * @param boolean $refreshPagesHash Refresh page collection hashes before processing
  901. * @return Zend_Pdf_Action|null
  902. */
  903. protected function _cleanUpAction(Zend_Pdf_Action $action, $refreshPageCollectionHashes = true)
  904. {
  905. if ($this->_pageReferences === null || $refreshPageCollectionHashes) {
  906. $this->_refreshPagesHash();
  907. }
  908. // Named target is an action
  909. if ($action instanceof Zend_Pdf_Action_GoTo &&
  910. $this->resolveDestination($action->getDestination(), false) === null) {
  911. // Action itself is a GoTo action with an unresolved destination
  912. return null;
  913. }
  914. // Walk through child actions
  915. $iterator = new RecursiveIteratorIterator($action, RecursiveIteratorIterator::SELF_FIRST);
  916. $actionsToClean = array();
  917. $deletionCandidateKeys = array();
  918. foreach ($iterator as $chainedAction) {
  919. if ($chainedAction instanceof Zend_Pdf_Action_GoTo &&
  920. $this->resolveDestination($chainedAction->getDestination(), false) === null) {
  921. // Some child action is a GoTo action with an unresolved destination
  922. // Mark it as a candidate for deletion
  923. $actionsToClean[] = $iterator->getSubIterator();
  924. $deletionCandidateKeys[] = $iterator->getSubIterator()->key();
  925. }
  926. }
  927. foreach ($actionsToClean as $id => $action) {
  928. unset($action->next[$deletionCandidateKeys[$id]]);
  929. }
  930. return $action;
  931. }
  932. /**
  933. * Extract fonts attached to the document
  934. *
  935. * returns array of Zend_Pdf_Resource_Font_Extracted objects
  936. *
  937. * @return array
  938. * @throws Zend_Pdf_Exception
  939. */
  940. public function extractFonts()
  941. {
  942. $fontResourcesUnique = array();
  943. foreach ($this->pages as $page) {
  944. $pageResources = $page->extractResources();
  945. if ($pageResources->Font === null) {
  946. // Page doesn't contain have any font reference
  947. continue;
  948. }
  949. $fontResources = $pageResources->Font;
  950. foreach ($fontResources->getKeys() as $fontResourceName) {
  951. $fontDictionary = $fontResources->$fontResourceName;
  952. if (! ($fontDictionary instanceof Zend_Pdf_Element_Reference ||
  953. $fontDictionary instanceof Zend_Pdf_Element_Object) ) {
  954. require_once 'Zend/Pdf/Exception.php';
  955. throw new Zend_Pdf_Exception('Font dictionary has to be an indirect object or object reference.');
  956. }
  957. $fontResourcesUnique[spl_object_hash($fontDictionary->getObject())] = $fontDictionary;
  958. }
  959. }
  960. $fonts = array();
  961. require_once 'Zend/Pdf/Exception.php';
  962. foreach ($fontResourcesUnique as $resourceId => $fontDictionary) {
  963. try {
  964. // Try to extract font
  965. require_once 'Zend/Pdf/Resource/Font/Extracted.php';
  966. $extractedFont = new Zend_Pdf_Resource_Font_Extracted($fontDictionary);
  967. $fonts[$resourceId] = $extractedFont;
  968. } catch (Zend_Pdf_Exception $e) {
  969. if ($e->getMessage() != 'Unsupported font type.') {
  970. throw $e;
  971. }
  972. }
  973. }
  974. return $fonts;
  975. }
  976. /**
  977. * Extract font attached to the page by specific font name
  978. *
  979. * $fontName should be specified in UTF-8 encoding
  980. *
  981. * @return Zend_Pdf_Resource_Font_Extracted|null
  982. * @throws Zend_Pdf_Exception
  983. */
  984. public function extractFont($fontName)
  985. {
  986. $fontResourcesUnique = array();
  987. require_once 'Zend/Pdf/Exception.php';
  988. foreach ($this->pages as $page) {
  989. $pageResources = $page->extractResources();
  990. if ($pageResources->Font === null) {
  991. // Page doesn't contain have any font reference
  992. continue;
  993. }
  994. $fontResources = $pageResources->Font;
  995. foreach ($fontResources->getKeys() as $fontResourceName) {
  996. $fontDictionary = $fontResources->$fontResourceName;
  997. if (! ($fontDictionary instanceof Zend_Pdf_Element_Reference ||
  998. $fontDictionary instanceof Zend_Pdf_Element_Object) ) {
  999. require_once 'Zend/Pdf/Exception.php';
  1000. throw new Zend_Pdf_Exception('Font dictionary has to be an indirect object or object reference.');
  1001. }
  1002. $resourceId = spl_object_hash($fontDictionary->getObject());
  1003. if (isset($fontResourcesUnique[$resourceId])) {
  1004. continue;
  1005. } else {
  1006. // Mark resource as processed
  1007. $fontResourcesUnique[$resourceId] = 1;
  1008. }
  1009. if ($fontDictionary->BaseFont->value != $fontName) {
  1010. continue;
  1011. }
  1012. try {
  1013. // Try to extract font
  1014. require_once 'Zend/Pdf/Resource/Font/Extracted.php';
  1015. return new Zend_Pdf_Resource_Font_Extracted($fontDictionary);
  1016. } catch (Zend_Pdf_Exception $e) {
  1017. if ($e->getMessage() != 'Unsupported font type.') {
  1018. throw $e;
  1019. }
  1020. // Continue searhing
  1021. }
  1022. }
  1023. }
  1024. return null;
  1025. }
  1026. /**
  1027. * Render the completed PDF to a string.
  1028. * If $newSegmentOnly is true and it's not a new document,
  1029. * then only appended part of PDF is returned.
  1030. *
  1031. * @param boolean $newSegmentOnly
  1032. * @param resource $outputStream
  1033. * @return string
  1034. * @throws Zend_Pdf_Exception
  1035. */
  1036. public function render($newSegmentOnly = false, $outputStream = null)
  1037. {
  1038. if ($this->_isNewDocument) {
  1039. // Drop full document first time even $newSegmentOnly is set to true
  1040. $newSegmentOnly = false;
  1041. $this->_isNewDocument = false;
  1042. }
  1043. // Save document properties if necessary
  1044. if ($this->properties != $this->_originalProperties) {
  1045. $docInfo = $this->_objFactory->newObject(new Zend_Pdf_Element_Dictionary());
  1046. foreach ($this->properties as $key => $value) {
  1047. switch ($key) {
  1048. case 'Trapped':
  1049. switch ($value) {
  1050. case true:
  1051. $docInfo->$key = new Zend_Pdf_Element_Name('True');
  1052. break;
  1053. case false:
  1054. $docInfo->$key = new Zend_Pdf_Element_Name('False');
  1055. break;
  1056. case null:
  1057. $docInfo->$key = new Zend_Pdf_Element_Name('Unknown');
  1058. break;
  1059. default:
  1060. require_once 'Zend/Pdf/Exception.php';
  1061. throw new Zend_Pdf_Exception('Wrong Trapped document property vale: \'' . $value . '\'. Only true, false and null values are allowed.');
  1062. break;
  1063. }
  1064. case 'CreationDate':
  1065. // break intentionally omitted
  1066. case 'ModDate':
  1067. $docInfo->$key = new Zend_Pdf_Element_String((string)$value);
  1068. break;
  1069. case 'Title':
  1070. // break intentionally omitted
  1071. case 'Author':
  1072. // break intentionally omitted
  1073. case 'Subject':
  1074. // break intentionally omitted
  1075. case 'Keywords':
  1076. // break intentionally omitted
  1077. case 'Creator':
  1078. // break intentionally omitted
  1079. case 'Producer':
  1080. if (extension_loaded('mbstring') === true) {
  1081. $detected = mb_detect_encoding($value);
  1082. if ($detected !== 'ASCII') {
  1083. $value = "\xfe\xff" . mb_convert_encoding($value, 'UTF-16', $detected);
  1084. }
  1085. }
  1086. $docInfo->$key = new Zend_Pdf_Element_String((string)$value);
  1087. break;
  1088. default:
  1089. // Set property using PDF type based on PHP type
  1090. $docInfo->$key = Zend_Pdf_Element::phpToPdf($value);
  1091. break;
  1092. }
  1093. }
  1094. $this->_trailer->Info = $docInfo;
  1095. }
  1096. $this->_dumpPages();
  1097. $this->_dumpNamedDestinations();
  1098. $this->_dumpOutlines();
  1099. // Check, that PDF file was modified
  1100. // File is always modified by _dumpPages() now, but future implementations may eliminate this.
  1101. if (!$this->_objFactory->isModified()) {
  1102. if ($newSegmentOnly) {
  1103. // Do nothing, return
  1104. return '';
  1105. }
  1106. if ($outputStream === null) {
  1107. return $this->_trailer->getPDFString();
  1108. } else {
  1109. $pdfData = $this->_trailer->getPDFString();
  1110. while ( strlen($pdfData) > 0 && ($byteCount = fwrite($outputStream, $pdfData)) != false ) {
  1111. $pdfData = substr($pdfData, $byteCount);
  1112. }
  1113. return '';
  1114. }
  1115. }
  1116. // offset (from a start of PDF file) of new PDF file segment
  1117. $offset = $this->_trailer->getPDFLength();
  1118. // Last Object number in a list of free objects
  1119. $lastFreeObject = $this->_trailer->getLastFreeObject();
  1120. // Array of cross-reference table subsections
  1121. $xrefTable = array();
  1122. // Object numbers of first objects in each subsection
  1123. $xrefSectionStartNums = array();
  1124. // Last cross-reference table subsection
  1125. $xrefSection = array();
  1126. // Dummy initialization of the first element (specail case - header of linked list of free objects).
  1127. $xrefSection[] = 0;
  1128. $xrefSectionStartNums[] = 0;
  1129. // Object number of last processed PDF object.
  1130. // Used to manage cross-reference subsections.
  1131. // Initialized by zero (specail case - header of linked list of free objects).
  1132. $lastObjNum = 0;
  1133. if ($outputStream !== null) {
  1134. if (!$newSegmentOnly) {
  1135. $pdfData = $this->_trailer->getPDFString();
  1136. while ( strlen($pdfData) > 0 && ($byteCount = fwrite($outputStream, $pdfData)) != false ) {
  1137. $pdfData = substr($pdfData, $byteCount);
  1138. }
  1139. }
  1140. } else {
  1141. $pdfSegmentBlocks = ($newSegmentOnly) ? array() : array($this->_trailer->getPDFString());
  1142. }
  1143. // Iterate objects to create new reference table
  1144. foreach ($this->_objFactory->listModifiedObjects() as $updateInfo) {
  1145. $objNum = $updateInfo->getObjNum();
  1146. if ($objNum - $lastObjNum != 1) {
  1147. // Save cross-reference table subsection and start new one
  1148. $xrefTable[] = $xrefSection;
  1149. $xrefSection = array();
  1150. $xrefSectionStartNums[] = $objNum;
  1151. }
  1152. if ($updateInfo->isFree()) {
  1153. // Free object cross-reference table entry
  1154. $xrefSection[] = sprintf("%010d %05d f \n", $lastFreeObject, $updateInfo->getGenNum());
  1155. $lastFreeObject = $objNum;
  1156. } else {
  1157. // In-use object cross-reference table entry
  1158. $xrefSection[] = sprintf("%010d %05d n \n", $offset, $updateInfo->getGenNum());
  1159. $pdfBlock = $updateInfo->getObjectDump();
  1160. $offset += strlen($pdfBlock);
  1161. if ($outputStream === null) {
  1162. $pdfSegmentBlocks[] = $pdfBlock;
  1163. } else {
  1164. while ( strlen($pdfBlock) > 0 && ($byteCount = fwrite($outputStream, $pdfBlock)) != false ) {
  1165. $pdfBlock = substr($pdfBlock, $byteCount);
  1166. }
  1167. }
  1168. }
  1169. $lastObjNum = $objNum;
  1170. }
  1171. // Save last cross-reference table subsection
  1172. $xrefTable[] = $xrefSection;
  1173. // Modify first entry (specail case - header of linked list of free objects).
  1174. $xrefTable[0][0] = sprintf("%010d 65535 f \n", $lastFreeObject);
  1175. $xrefTableStr = "xref\n";
  1176. foreach ($xrefTable as $sectId => $xrefSection) {
  1177. $xrefTableStr .= sprintf("%d %d \n", $xrefSectionStartNums[$sectId], count($xrefSection));
  1178. foreach ($xrefSection as $xrefTableEntry) {
  1179. $xrefTableStr .= $xrefTableEntry;
  1180. }
  1181. }
  1182. $this->_trailer->Size->value = $this->_objFactory->getObjectCount();
  1183. $pdfBlock = $xrefTableStr
  1184. . $this->_trailer->toString()
  1185. . "startxref\n" . $offset . "\n"
  1186. . "%%EOF\n";
  1187. $this->_objFactory->cleanEnumerationShiftCache();
  1188. if ($outputStream === null) {
  1189. $pdfSegmentBlocks[] = $pdfBlock;
  1190. return implode('', $pdfSegmentBlocks);
  1191. } else {
  1192. while ( strlen($pdfBlock) > 0 && ($byteCount = fwrite($outputStream, $pdfBlock)) != false ) {
  1193. $pdfBlock = substr($pdfBlock, $byteCount);
  1194. }
  1195. return '';
  1196. }
  1197. }
  1198. /**
  1199. * Set the document-level JavaScript
  1200. *
  1201. * @param string $javascript
  1202. */
  1203. public function setJavaScript($javascript)
  1204. {
  1205. $this->_javaScript = $javascript;
  1206. }
  1207. /**
  1208. * Convert date to PDF format (it's close to ASN.1 (Abstract Syntax Notation
  1209. * One) defined in ISO/IEC 8824).
  1210. *
  1211. * @todo This really isn't the best location for this method. It should
  1212. * probably actually exist as Zend_Pdf_Element_Date or something like that.
  1213. *
  1214. * @todo Address the following E_STRICT issue:
  1215. * PHP Strict Standards: date(): It is not safe to rely on the system's
  1216. * timezone settings. Please use the date.timezone setting, the TZ
  1217. * environment variable or the date_default_timezone_set() function. In
  1218. * case you used any of those methods and you are still getting this
  1219. * warning, you most likely misspelled the timezone identifier.
  1220. *
  1221. * @param integer $timestamp (optional) If omitted, uses the current time.
  1222. * @return string
  1223. */
  1224. public static function pdfDate($timestamp = null)
  1225. {
  1226. if ($timestamp === null) {
  1227. $date = date('\D\:YmdHisO');
  1228. } else {
  1229. $date = date('\D\:YmdHisO', $timestamp);
  1230. }
  1231. return substr_replace($date, '\'', -2, 0) . '\'';
  1232. }
  1233. }