/framework/vendor/zend/Zend/Pdf.php

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