PageRenderTime 58ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/app/protected/extensions/pdf/dompdf/lib/class.pdf.php

https://bitbucket.org/sanbrar/zurmo_invoice
PHP | 4969 lines | 3043 code | 784 blank | 1142 comment | 596 complexity | dc0a5e54a94bd696ac39d9c914ad1655 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1, LGPL-3.0, BSD-2-Clause, GPL-3.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * A PHP class to provide the basic functionality to create a pdf document without
  4. * any requirement for additional modules.
  5. *
  6. * Extended by Orion Richardson to support Unicode / UTF-8 characters using
  7. * TCPDF and others as a guide.
  8. *
  9. * @author Wayne Munro <pdf@ros.co.nz>
  10. * @author Orion Richardson <orionr@yahoo.com>
  11. * @author Helmut Tischer <htischer@weihenstephan.org>
  12. * @author Ryan H. Masten <ryan.masten@gmail.com>
  13. * @author Brian Sweeney <eclecticgeek@gmail.com>
  14. * @author Fabien Ménager <fabien.menager@gmail.com>
  15. * @version $Id: class.pdf.php 469 2012-02-05 22:25:30Z fabien.menager $
  16. * @license Public Domain http://creativecommons.org/licenses/publicdomain/
  17. * @package Cpdf
  18. */
  19. class Cpdf {
  20. /**
  21. * @var integer The current number of pdf objects in the document
  22. */
  23. public $numObj = 0;
  24. /**
  25. * @var array This array contains all of the pdf objects, ready for final assembly
  26. */
  27. public $objects = array();
  28. /**
  29. * @var integer The objectId (number within the objects array) of the document catalog
  30. */
  31. public $catalogId;
  32. /**
  33. * @var array Array carrying information about the fonts that the system currently knows about
  34. * Used to ensure that a font is not loaded twice, among other things
  35. */
  36. public $fonts = array();
  37. /**
  38. * @var string The default font metrics file to use if no other font has been loaded.
  39. * The path to the directory containing the font metrics should be included
  40. */
  41. public $defaultFont = './fonts/Helvetica.afm';
  42. /**
  43. * @string A record of the current font
  44. */
  45. public $currentFont = '';
  46. /**
  47. * @var string The current base font
  48. */
  49. public $currentBaseFont = '';
  50. /**
  51. * @var integer The number of the current font within the font array
  52. */
  53. public $currentFontNum = 0;
  54. /**
  55. * @var integer
  56. */
  57. public $currentNode;
  58. /**
  59. * @var integer Object number of the current page
  60. */
  61. public $currentPage;
  62. /**
  63. * @var integer Object number of the currently active contents block
  64. */
  65. public $currentContents;
  66. /**
  67. * @var integer Number of fonts within the system
  68. */
  69. public $numFonts = 0;
  70. /**
  71. * @var integer Number of graphic state resources used
  72. */
  73. private $numStates = 0;
  74. /**
  75. * @var array Current colour for fill operations, defaults to inactive value,
  76. * all three components should be between 0 and 1 inclusive when active
  77. */
  78. public $currentColour = null;
  79. /**
  80. * @var array Current colour for stroke operations (lines etc.)
  81. */
  82. public $currentStrokeColour = null;
  83. /**
  84. * @var string Current style that lines are drawn in
  85. */
  86. public $currentLineStyle = '';
  87. /**
  88. * @var array Current line transparency (partial graphics state)
  89. */
  90. public $currentLineTransparency = array("mode" => "Normal", "opacity" => 1.0);
  91. /**
  92. * array Current fill transparency (partial graphics state)
  93. */
  94. public $currentFillTransparency = array("mode" => "Normal", "opacity" => 1.0);
  95. /**
  96. * @var array An array which is used to save the state of the document, mainly the colours and styles
  97. * it is used to temporarily change to another state, the change back to what it was before
  98. */
  99. public $stateStack = array();
  100. /**
  101. * @var integer Number of elements within the state stack
  102. */
  103. public $nStateStack = 0;
  104. /**
  105. * @var integer Number of page objects within the document
  106. */
  107. public $numPages = 0;
  108. /**
  109. * @var array Object Id storage stack
  110. */
  111. public $stack = array();
  112. /**
  113. * @var integer Number of elements within the object Id storage stack
  114. */
  115. public $nStack = 0;
  116. /**
  117. * an array which contains information about the objects which are not firmly attached to pages
  118. * these have been added with the addObject function
  119. */
  120. public $looseObjects = array();
  121. /**
  122. * array contains infomation about how the loose objects are to be added to the document
  123. */
  124. public $addLooseObjects = array();
  125. /**
  126. * @var integer The objectId of the information object for the document
  127. * this contains authorship, title etc.
  128. */
  129. public $infoObject = 0;
  130. /**
  131. * @var integer Number of images being tracked within the document
  132. */
  133. public $numImages = 0;
  134. /**
  135. * @var array An array containing options about the document
  136. * it defaults to turning on the compression of the objects
  137. */
  138. public $options = array('compression'=>true);
  139. /**
  140. * @var integer The objectId of the first page of the document
  141. */
  142. public $firstPageId;
  143. /**
  144. * @var float Used to track the last used value of the inter-word spacing, this is so that it is known
  145. * when the spacing is changed.
  146. */
  147. public $wordSpaceAdjust = 0;
  148. /**
  149. * @var float Used to track the last used value of the inter-letter spacing, this is so that it is known
  150. * when the spacing is changed.
  151. */
  152. public $charSpaceAdjust = 0;
  153. /**
  154. * @var integer The object Id of the procset object
  155. */
  156. public $procsetObjectId;
  157. /**
  158. * @var array Store the information about the relationship between font families
  159. * this used so that the code knows which font is the bold version of another font, etc.
  160. * the value of this array is initialised in the constuctor function.
  161. */
  162. public $fontFamilies = array();
  163. /**
  164. * @var string Folder for php serialized formats of font metrics files.
  165. * If empty string, use same folder as original metrics files.
  166. * This can be passed in from class creator.
  167. * If this folder does not exist or is not writable, Cpdf will be **much** slower.
  168. * Because of potential trouble with php safe mode, folder cannot be created at runtime.
  169. */
  170. public $fontcache = '';
  171. /**
  172. * @var integer The version of the font metrics cache file.
  173. * This value must be manually incremented whenever the internal font data structure is modified.
  174. */
  175. public $fontcacheVersion = 5;
  176. /**
  177. * @var string Temporary folder.
  178. * If empty string, will attempty system tmp folder.
  179. * This can be passed in from class creator.
  180. * Only used for conversion of gd images to jpeg images.
  181. */
  182. public $tmp = '';
  183. /**
  184. * @var string Track if the current font is bolded or italicised
  185. */
  186. public $currentTextState = '';
  187. /**
  188. * @var string Messages are stored here during processing, these can be selected afterwards to give some useful debug information
  189. */
  190. public $messages = '';
  191. /**
  192. * @var string The ancryption array for the document encryption is stored here
  193. */
  194. public $arc4 = '';
  195. /**
  196. * @var integer The object Id of the encryption information
  197. */
  198. public $arc4_objnum = 0;
  199. /**
  200. * @var string The file identifier, used to uniquely identify a pdf document
  201. */
  202. public $fileIdentifier = '';
  203. /**
  204. * @var boolean A flag to say if a document is to be encrypted or not
  205. */
  206. public $encrypted = false;
  207. /**
  208. * @var string The encryption key for the encryption of all the document content (structure is not encrypted)
  209. */
  210. public $encryptionKey = '';
  211. /**
  212. * @var array Array which forms a stack to keep track of nested callback functions
  213. */
  214. public $callback = array();
  215. /**
  216. * @var integer The number of callback functions in the callback array
  217. */
  218. public $nCallback = 0;
  219. /**
  220. * @var array Store label->id pairs for named destinations, these will be used to replace internal links
  221. * done this way so that destinations can be defined after the location that links to them
  222. */
  223. public $destinations = array();
  224. /**
  225. * @var array Store the stack for the transaction commands, each item in here is a record of the values of all the
  226. * publiciables within the class, so that the user can rollback at will (from each 'start' command)
  227. * note that this includes the objects array, so these can be large.
  228. */
  229. public $checkpoint = '';
  230. /**
  231. * @var array Table of Image origin filenames and image labels which were already added with o_image().
  232. * Allows to merge identical images
  233. */
  234. public $imagelist = array();
  235. /**
  236. * @var boolean Whether the text passed in should be treated as Unicode or just local character set.
  237. */
  238. public $isUnicode = false;
  239. /**
  240. * @var string the JavaScript code of the document
  241. */
  242. public $javascript = '';
  243. /**
  244. * @var boolean whether the compression is possible
  245. */
  246. protected $compressionReady = false;
  247. /**
  248. * @var array Current page size
  249. */
  250. protected $currentPageSize = array("width" => 0, "height" => 0);
  251. /**
  252. * @var array All the chars that will be required in the font subsets
  253. */
  254. protected $stringSubsets = array();
  255. /**
  256. * @var string The target internal encoding
  257. */
  258. static protected $targetEncoding = 'iso-8859-1';
  259. /**
  260. * @var array The list of the core fonts
  261. */
  262. static protected $coreFonts = array(
  263. 'courier', 'courier-bold', 'courier-oblique', 'courier-boldoblique',
  264. 'helvetica', 'helvetica-bold', 'helvetica-oblique', 'helvetica-boldoblique',
  265. 'times-roman', 'times-bold', 'times-italic', 'times-bolditalic',
  266. 'symbol', 'zapfdingbats'
  267. );
  268. /**
  269. * class constructor
  270. * this will start a new document
  271. * @var array $pageSize Array of 4 numbers, defining the bottom left and upper right corner of the page. first two are normally zero.
  272. * @var boolean $isUnicode Whether text will be treated as Unicode or not.
  273. * @var string $fontcache The font cache folder
  274. * @var string $tmp The temporary folder
  275. */
  276. function __construct ($pageSize = array(0, 0, 612, 792), $isUnicode = false, $fontcache = '', $tmp = '') {
  277. $this->isUnicode = $isUnicode;
  278. $this->fontcache = $fontcache;
  279. $this->tmp = $tmp;
  280. $this->newDocument($pageSize);
  281. $this->compressionReady = function_exists('gzcompress');
  282. if ( in_array('Windows-1252', mb_list_encodings()) ) {
  283. self::$targetEncoding = 'Windows-1252';
  284. }
  285. // also initialize the font families that are known about already
  286. $this->setFontFamily('init');
  287. // $this->fileIdentifier = md5('xxxxxxxx'.time());
  288. }
  289. /**
  290. * Document object methods (internal use only)
  291. *
  292. * There is about one object method for each type of object in the pdf document
  293. * Each function has the same call list ($id,$action,$options).
  294. * $id = the object ID of the object, or what it is to be if it is being created
  295. * $action = a string specifying the action to be performed, though ALL must support:
  296. * 'new' - create the object with the id $id
  297. * 'out' - produce the output for the pdf object
  298. * $options = optional, a string or array containing the various parameters for the object
  299. *
  300. * These, in conjunction with the output function are the ONLY way for output to be produced
  301. * within the pdf 'file'.
  302. */
  303. /**
  304. *destination object, used to specify the location for the user to jump to, presently on opening
  305. */
  306. protected function o_destination($id, $action, $options = '') {
  307. if ($action !== 'new') {
  308. $o = & $this->objects[$id];
  309. }
  310. switch ($action) {
  311. case 'new':
  312. $this->objects[$id] = array('t'=>'destination', 'info'=>array());
  313. $tmp = '';
  314. switch ($options['type']) {
  315. case 'XYZ':
  316. case 'FitR':
  317. $tmp = ' '.$options['p3'].$tmp;
  318. case 'FitH':
  319. case 'FitV':
  320. case 'FitBH':
  321. case 'FitBV':
  322. $tmp = ' '.$options['p1'].' '.$options['p2'].$tmp;
  323. case 'Fit':
  324. case 'FitB':
  325. $tmp = $options['type'].$tmp;
  326. $this->objects[$id]['info']['string'] = $tmp;
  327. $this->objects[$id]['info']['page'] = $options['page'];
  328. }
  329. break;
  330. case 'out':
  331. $tmp = $o['info'];
  332. $res = "\n$id 0 obj\n".'['.$tmp['page'].' 0 R /'.$tmp['string']."]\nendobj";
  333. return $res;
  334. }
  335. }
  336. /**
  337. * set the viewer preferences
  338. */
  339. protected function o_viewerPreferences($id, $action, $options = '') {
  340. if ($action !== 'new') {
  341. $o = & $this->objects[$id];
  342. }
  343. switch ($action) {
  344. case 'new':
  345. $this->objects[$id] = array('t'=>'viewerPreferences', 'info'=>array());
  346. break;
  347. case 'add':
  348. foreach($options as $k=>$v) {
  349. switch ($k) {
  350. case 'HideToolbar':
  351. case 'HideMenubar':
  352. case 'HideWindowUI':
  353. case 'FitWindow':
  354. case 'CenterWindow':
  355. case 'NonFullScreenPageMode':
  356. case 'Direction':
  357. $o['info'][$k] = $v;
  358. break;
  359. }
  360. }
  361. break;
  362. case 'out':
  363. $res = "\n$id 0 obj\n<< ";
  364. foreach($o['info'] as $k=>$v) {
  365. $res.= "\n/$k $v";
  366. }
  367. $res.= "\n>>\n";
  368. return $res;
  369. }
  370. }
  371. /**
  372. * define the document catalog, the overall controller for the document
  373. */
  374. protected function o_catalog($id, $action, $options = '') {
  375. if ($action !== 'new') {
  376. $o = & $this->objects[$id];
  377. }
  378. switch ($action) {
  379. case 'new':
  380. $this->objects[$id] = array('t'=>'catalog', 'info'=>array());
  381. $this->catalogId = $id;
  382. break;
  383. case 'outlines':
  384. case 'pages':
  385. case 'openHere':
  386. case 'javascript':
  387. $o['info'][$action] = $options;
  388. break;
  389. case 'viewerPreferences':
  390. if (!isset($o['info']['viewerPreferences'])) {
  391. $this->numObj++;
  392. $this->o_viewerPreferences($this->numObj, 'new');
  393. $o['info']['viewerPreferences'] = $this->numObj;
  394. }
  395. $vp = $o['info']['viewerPreferences'];
  396. $this->o_viewerPreferences($vp, 'add', $options);
  397. break;
  398. case 'out':
  399. $res = "\n$id 0 obj\n<< /Type /Catalog";
  400. foreach($o['info'] as $k=>$v) {
  401. switch ($k) {
  402. case 'outlines':
  403. $res.= "\n/Outlines $v 0 R";
  404. break;
  405. case 'pages':
  406. $res.= "\n/Pages $v 0 R";
  407. break;
  408. case 'viewerPreferences':
  409. $res.= "\n/ViewerPreferences $v 0 R";
  410. break;
  411. case 'openHere':
  412. $res.= "\n/OpenAction $v 0 R";
  413. break;
  414. case 'javascript':
  415. $res.= "\n/Names <</JavaScript $v 0 R>>";
  416. break;
  417. }
  418. }
  419. $res.= " >>\nendobj";
  420. return $res;
  421. }
  422. }
  423. /**
  424. * object which is a parent to the pages in the document
  425. */
  426. protected function o_pages($id, $action, $options = '') {
  427. if ($action !== 'new') {
  428. $o = & $this->objects[$id];
  429. }
  430. switch ($action) {
  431. case 'new':
  432. $this->objects[$id] = array('t'=>'pages', 'info'=>array());
  433. $this->o_catalog($this->catalogId, 'pages', $id);
  434. break;
  435. case 'page':
  436. if (!is_array($options)) {
  437. // then it will just be the id of the new page
  438. $o['info']['pages'][] = $options;
  439. } else {
  440. // then it should be an array having 'id','rid','pos', where rid=the page to which this one will be placed relative
  441. // and pos is either 'before' or 'after', saying where this page will fit.
  442. if (isset($options['id']) && isset($options['rid']) && isset($options['pos'])) {
  443. $i = array_search($options['rid'], $o['info']['pages']);
  444. if (isset($o['info']['pages'][$i]) && $o['info']['pages'][$i] == $options['rid']) {
  445. // then there is a match
  446. // make a space
  447. switch ($options['pos']) {
  448. case 'before':
  449. $k = $i;
  450. break;
  451. case 'after':
  452. $k = $i+1;
  453. break;
  454. default:
  455. $k = -1;
  456. break;
  457. }
  458. if ($k >= 0) {
  459. for ($j = count($o['info']['pages']) -1;$j >= $k;$j--) {
  460. $o['info']['pages'][$j+1] = $o['info']['pages'][$j];
  461. }
  462. $o['info']['pages'][$k] = $options['id'];
  463. }
  464. }
  465. }
  466. }
  467. break;
  468. case 'procset':
  469. $o['info']['procset'] = $options;
  470. break;
  471. case 'mediaBox':
  472. $o['info']['mediaBox'] = $options;
  473. // which should be an array of 4 numbers
  474. $this->currentPageSize = array('width' => $options[2], 'height' => $options[3]);
  475. break;
  476. case 'font':
  477. $o['info']['fonts'][] = array('objNum'=>$options['objNum'], 'fontNum'=>$options['fontNum']);
  478. break;
  479. case 'extGState':
  480. $o['info']['extGStates'][] = array('objNum' => $options['objNum'], 'stateNum' => $options['stateNum']);
  481. break;
  482. case 'xObject':
  483. $o['info']['xObjects'][] = array('objNum'=>$options['objNum'], 'label'=>$options['label']);
  484. break;
  485. case 'out':
  486. if (count($o['info']['pages'])) {
  487. $res = "\n$id 0 obj\n<< /Type /Pages\n/Kids [";
  488. foreach($o['info']['pages'] as $v) {
  489. $res.= "$v 0 R\n";
  490. }
  491. $res.= "]\n/Count ".count($this->objects[$id]['info']['pages']);
  492. if ( (isset($o['info']['fonts']) && count($o['info']['fonts'])) ||
  493. isset($o['info']['procset']) ||
  494. (isset($o['info']['extGStates']) && count($o['info']['extGStates']))) {
  495. $res.= "\n/Resources <<";
  496. if (isset($o['info']['procset'])) {
  497. $res.= "\n/ProcSet ".$o['info']['procset']." 0 R";
  498. }
  499. if (isset($o['info']['fonts']) && count($o['info']['fonts'])) {
  500. $res.= "\n/Font << ";
  501. foreach($o['info']['fonts'] as $finfo) {
  502. $res.= "\n/F".$finfo['fontNum']." ".$finfo['objNum']." 0 R";
  503. }
  504. $res.= "\n>>";
  505. }
  506. if (isset($o['info']['xObjects']) && count($o['info']['xObjects'])) {
  507. $res.= "\n/XObject << ";
  508. foreach($o['info']['xObjects'] as $finfo) {
  509. $res.= "\n/".$finfo['label']." ".$finfo['objNum']." 0 R";
  510. }
  511. $res.= "\n>>";
  512. }
  513. if ( isset($o['info']['extGStates']) && count($o['info']['extGStates'])) {
  514. $res.= "\n/ExtGState << ";
  515. foreach ($o['info']['extGStates'] as $gstate) {
  516. $res.= "\n/GS" . $gstate['stateNum'] . " " . $gstate['objNum'] . " 0 R";
  517. }
  518. $res.= "\n>>";
  519. }
  520. $res.= "\n>>";
  521. if (isset($o['info']['mediaBox'])) {
  522. $tmp = $o['info']['mediaBox'];
  523. $res.= "\n/MediaBox [".sprintf('%.3F %.3F %.3F %.3F', $tmp[0], $tmp[1], $tmp[2], $tmp[3]) .']';
  524. }
  525. }
  526. $res.= "\n >>\nendobj";
  527. } else {
  528. $res = "\n$id 0 obj\n<< /Type /Pages\n/Count 0\n>>\nendobj";
  529. }
  530. return $res;
  531. }
  532. }
  533. /**
  534. * define the outlines in the doc, empty for now
  535. */
  536. protected function o_outlines($id, $action, $options = '') {
  537. if ($action !== 'new') {
  538. $o = & $this->objects[$id];
  539. }
  540. switch ($action) {
  541. case 'new':
  542. $this->objects[$id] = array('t'=>'outlines', 'info'=>array('outlines'=>array()));
  543. $this->o_catalog($this->catalogId, 'outlines', $id);
  544. break;
  545. case 'outline':
  546. $o['info']['outlines'][] = $options;
  547. break;
  548. case 'out':
  549. if (count($o['info']['outlines'])) {
  550. $res = "\n$id 0 obj\n<< /Type /Outlines /Kids [";
  551. foreach($o['info']['outlines'] as $v) {
  552. $res.= "$v 0 R ";
  553. }
  554. $res.= "] /Count ".count($o['info']['outlines']) ." >>\nendobj";
  555. } else {
  556. $res = "\n$id 0 obj\n<< /Type /Outlines /Count 0 >>\nendobj";
  557. }
  558. return $res;
  559. }
  560. }
  561. /**
  562. * an object to hold the font description
  563. */
  564. protected function o_font($id, $action, $options = '') {
  565. if ($action !== 'new') {
  566. $o = & $this->objects[$id];
  567. }
  568. switch ($action) {
  569. case 'new':
  570. $this->objects[$id] = array('t' => 'font', 'info' => array('name' => $options['name'], 'fontFileName' => $options['fontFileName'], 'SubType' => 'Type1'));
  571. $fontNum = $this->numFonts;
  572. $this->objects[$id]['info']['fontNum'] = $fontNum;
  573. // deal with the encoding and the differences
  574. if (isset($options['differences'])) {
  575. // then we'll need an encoding dictionary
  576. $this->numObj++;
  577. $this->o_fontEncoding($this->numObj, 'new', $options);
  578. $this->objects[$id]['info']['encodingDictionary'] = $this->numObj;
  579. } else if (isset($options['encoding'])) {
  580. // we can specify encoding here
  581. switch ($options['encoding']) {
  582. case 'WinAnsiEncoding':
  583. case 'MacRomanEncoding':
  584. case 'MacExpertEncoding':
  585. $this->objects[$id]['info']['encoding'] = $options['encoding'];
  586. break;
  587. case 'none':
  588. break;
  589. default:
  590. $this->objects[$id]['info']['encoding'] = 'WinAnsiEncoding';
  591. break;
  592. }
  593. } else {
  594. $this->objects[$id]['info']['encoding'] = 'WinAnsiEncoding';
  595. }
  596. if ($this->fonts[$options['fontFileName']]['isUnicode']) {
  597. // For Unicode fonts, we need to incorporate font data into
  598. // sub-sections that are linked from the primary font section.
  599. // Look at o_fontGIDtoCID and o_fontDescendentCID functions
  600. // for more informaiton.
  601. //
  602. // All of this code is adapted from the excellent changes made to
  603. // transform FPDF to TCPDF (http://tcpdf.sourceforge.net/)
  604. $toUnicodeId = ++$this->numObj;
  605. $this->o_contents($toUnicodeId, 'new', 'raw');
  606. $this->objects[$id]['info']['toUnicode'] = $toUnicodeId;
  607. $stream = <<<EOT
  608. /CIDInit /ProcSet findresource begin
  609. 12 dict begin
  610. begincmap
  611. /CIDSystemInfo
  612. <</Registry (Adobe)
  613. /Ordering (UCS)
  614. /Supplement 0
  615. >> def
  616. /CMapName /Adobe-Identity-UCS def
  617. /CMapType 2 def
  618. 1 begincodespacerange
  619. <0000> <FFFF>
  620. endcodespacerange
  621. 1 beginbfrange
  622. <0000> <FFFF> <0000>
  623. endbfrange
  624. endcmap
  625. CMapName currentdict /CMap defineresource pop
  626. end
  627. end
  628. EOT;
  629. $res = "<</Length " . mb_strlen($stream, '8bit') . " >>\n";
  630. $res .= "stream\n" . $stream . "endstream";
  631. $this->objects[$toUnicodeId]['c'] = $res;
  632. $cidFontId = ++$this->numObj;
  633. $this->o_fontDescendentCID($cidFontId, 'new', $options);
  634. $this->objects[$id]['info']['cidFont'] = $cidFontId;
  635. }
  636. // also tell the pages node about the new font
  637. $this->o_pages($this->currentNode, 'font', array('fontNum' => $fontNum, 'objNum' => $id));
  638. break;
  639. case 'add':
  640. foreach ($options as $k => $v) {
  641. switch ($k) {
  642. case 'BaseFont':
  643. $o['info']['name'] = $v;
  644. break;
  645. case 'FirstChar':
  646. case 'LastChar':
  647. case 'Widths':
  648. case 'FontDescriptor':
  649. case 'SubType':
  650. $this->addMessage('o_font '.$k." : ".$v);
  651. $o['info'][$k] = $v;
  652. break;
  653. }
  654. }
  655. // pass values down to descendent font
  656. if (isset($o['info']['cidFont'])) {
  657. $this->o_fontDescendentCID($o['info']['cidFont'], 'add', $options);
  658. }
  659. break;
  660. case 'out':
  661. if ($this->fonts[$this->objects[$id]['info']['fontFileName']]['isUnicode']) {
  662. // For Unicode fonts, we need to incorporate font data into
  663. // sub-sections that are linked from the primary font section.
  664. // Look at o_fontGIDtoCID and o_fontDescendentCID functions
  665. // for more informaiton.
  666. //
  667. // All of this code is adapted from the excellent changes made to
  668. // transform FPDF to TCPDF (http://tcpdf.sourceforge.net/)
  669. $res = "\n$id 0 obj\n<</Type /Font\n/Subtype /Type0\n";
  670. $res.= "/BaseFont /".$o['info']['name']."\n";
  671. // The horizontal identity mapping for 2-byte CIDs; may be used
  672. // with CIDFonts using any Registry, Ordering, and Supplement values.
  673. $res.= "/Encoding /Identity-H\n";
  674. $res.= "/DescendantFonts [".$o['info']['cidFont']." 0 R]\n";
  675. $res.= "/ToUnicode ".$o['info']['toUnicode']." 0 R\n";
  676. $res.= ">>\n";
  677. $res.= "endobj";
  678. } else {
  679. $res = "\n$id 0 obj\n<< /Type /Font\n/Subtype /".$o['info']['SubType']."\n";
  680. $res.= "/Name /F".$o['info']['fontNum']."\n";
  681. $res.= "/BaseFont /".$o['info']['name']."\n";
  682. if (isset($o['info']['encodingDictionary'])) {
  683. // then place a reference to the dictionary
  684. $res.= "/Encoding ".$o['info']['encodingDictionary']." 0 R\n";
  685. } else if (isset($o['info']['encoding'])) {
  686. // use the specified encoding
  687. $res.= "/Encoding /".$o['info']['encoding']."\n";
  688. }
  689. if (isset($o['info']['FirstChar'])) {
  690. $res.= "/FirstChar ".$o['info']['FirstChar']."\n";
  691. }
  692. if (isset($o['info']['LastChar'])) {
  693. $res.= "/LastChar ".$o['info']['LastChar']."\n";
  694. }
  695. if (isset($o['info']['Widths'])) {
  696. $res.= "/Widths ".$o['info']['Widths']." 0 R\n";
  697. }
  698. if (isset($o['info']['FontDescriptor'])) {
  699. $res.= "/FontDescriptor ".$o['info']['FontDescriptor']." 0 R\n";
  700. }
  701. $res.= ">>\n";
  702. $res.= "endobj";
  703. }
  704. return $res;
  705. }
  706. }
  707. /**
  708. * a font descriptor, needed for including additional fonts
  709. */
  710. protected function o_fontDescriptor($id, $action, $options = '') {
  711. if ($action !== 'new') {
  712. $o = & $this->objects[$id];
  713. }
  714. switch ($action) {
  715. case 'new':
  716. $this->objects[$id] = array('t'=>'fontDescriptor', 'info'=>$options);
  717. break;
  718. case 'out':
  719. $res = "\n$id 0 obj\n<< /Type /FontDescriptor\n";
  720. foreach ($o['info'] as $label => $value) {
  721. switch ($label) {
  722. case 'Ascent':
  723. case 'CapHeight':
  724. case 'Descent':
  725. case 'Flags':
  726. case 'ItalicAngle':
  727. case 'StemV':
  728. case 'AvgWidth':
  729. case 'Leading':
  730. case 'MaxWidth':
  731. case 'MissingWidth':
  732. case 'StemH':
  733. case 'XHeight':
  734. case 'CharSet':
  735. if (mb_strlen($value, '8bit')) {
  736. $res.= "/$label $value\n";
  737. }
  738. break;
  739. case 'FontFile':
  740. case 'FontFile2':
  741. case 'FontFile3':
  742. $res.= "/$label $value 0 R\n";
  743. break;
  744. case 'FontBBox':
  745. $res.= "/$label [$value[0] $value[1] $value[2] $value[3]]\n";
  746. break;
  747. case 'FontName':
  748. $res.= "/$label /$value\n";
  749. break;
  750. }
  751. }
  752. $res.= ">>\nendobj";
  753. return $res;
  754. }
  755. }
  756. /**
  757. * the font encoding
  758. */
  759. protected function o_fontEncoding($id, $action, $options = '') {
  760. if ($action !== 'new') {
  761. $o = & $this->objects[$id];
  762. }
  763. switch ($action) {
  764. case 'new':
  765. // the options array should contain 'differences' and maybe 'encoding'
  766. $this->objects[$id] = array('t'=>'fontEncoding', 'info'=>$options);
  767. break;
  768. case 'out':
  769. $res = "\n$id 0 obj\n<< /Type /Encoding\n";
  770. if (!isset($o['info']['encoding'])) {
  771. $o['info']['encoding'] = 'WinAnsiEncoding';
  772. }
  773. if ($o['info']['encoding'] !== 'none') {
  774. $res.= "/BaseEncoding /".$o['info']['encoding']."\n";
  775. }
  776. $res.= "/Differences \n[";
  777. $onum = -100;
  778. foreach($o['info']['differences'] as $num=>$label) {
  779. if ($num != $onum+1) {
  780. // we cannot make use of consecutive numbering
  781. $res.= "\n$num /$label";
  782. } else {
  783. $res.= " /$label";
  784. }
  785. $onum = $num;
  786. }
  787. $res.= "\n]\n>>\nendobj";
  788. return $res;
  789. }
  790. }
  791. /**
  792. * a descendent cid font, needed for unicode fonts
  793. */
  794. protected function o_fontDescendentCID($id, $action, $options = '') {
  795. if ($action !== 'new') {
  796. $o = & $this->objects[$id];
  797. }
  798. switch ($action) {
  799. case 'new':
  800. $this->objects[$id] = array('t'=>'fontDescendentCID', 'info'=>$options);
  801. // we need a CID system info section
  802. $cidSystemInfoId = ++$this->numObj;
  803. $this->o_contents($cidSystemInfoId, 'new', 'raw');
  804. $this->objects[$id]['info']['cidSystemInfo'] = $cidSystemInfoId;
  805. $res= "<</Registry (Adobe)\n"; // A string identifying an issuer of character collections
  806. $res.= "/Ordering (UCS)\n"; // A string that uniquely names a character collection issued by a specific registry
  807. $res.= "/Supplement 0\n"; // The supplement number of the character collection.
  808. $res.= ">>";
  809. $this->objects[$cidSystemInfoId]['c'] = $res;
  810. // and a CID to GID map
  811. $cidToGidMapId = ++$this->numObj;
  812. $this->o_fontGIDtoCIDMap($cidToGidMapId, 'new', $options);
  813. $this->objects[$id]['info']['cidToGidMap'] = $cidToGidMapId;
  814. break;
  815. case 'add':
  816. foreach ($options as $k => $v) {
  817. switch ($k) {
  818. case 'BaseFont':
  819. $o['info']['name'] = $v;
  820. break;
  821. case 'FirstChar':
  822. case 'LastChar':
  823. case 'MissingWidth':
  824. case 'FontDescriptor':
  825. case 'SubType':
  826. $this->addMessage("o_fontDescendentCID $k : $v");
  827. $o['info'][$k] = $v;
  828. break;
  829. }
  830. }
  831. // pass values down to cid to gid map
  832. $this->o_fontGIDtoCIDMap($o['info']['cidToGidMap'], 'add', $options);
  833. break;
  834. case 'out':
  835. $res = "\n$id 0 obj\n";
  836. $res.= "<</Type /Font\n";
  837. $res.= "/Subtype /CIDFontType2\n";
  838. $res.= "/BaseFont /".$o['info']['name']."\n";
  839. $res.= "/CIDSystemInfo ".$o['info']['cidSystemInfo']." 0 R\n";
  840. // if (isset($o['info']['FirstChar'])) {
  841. // $res.= "/FirstChar ".$o['info']['FirstChar']."\n";
  842. // }
  843. // if (isset($o['info']['LastChar'])) {
  844. // $res.= "/LastChar ".$o['info']['LastChar']."\n";
  845. // }
  846. if (isset($o['info']['FontDescriptor'])) {
  847. $res.= "/FontDescriptor ".$o['info']['FontDescriptor']." 0 R\n";
  848. }
  849. if (isset($o['info']['MissingWidth'])) {
  850. $res.= "/DW ".$o['info']['MissingWidth']."\n";
  851. }
  852. if (isset($o['info']['fontFileName']) && isset($this->fonts[$o['info']['fontFileName']]['CIDWidths'])) {
  853. $cid_widths = &$this->fonts[$o['info']['fontFileName']]['CIDWidths'];
  854. $w = '';
  855. foreach ($cid_widths as $cid => $width) {
  856. $w .= "$cid [$width] ";
  857. }
  858. $res.= "/W [$w]\n";
  859. }
  860. $res.= "/CIDToGIDMap ".$o['info']['cidToGidMap']." 0 R\n";
  861. $res.= ">>\n";
  862. $res.= "endobj";
  863. return $res;
  864. }
  865. }
  866. /**
  867. * a font glyph to character map, needed for unicode fonts
  868. */
  869. protected function o_fontGIDtoCIDMap($id, $action, $options = '') {
  870. if ($action !== 'new') {
  871. $o = & $this->objects[$id];
  872. }
  873. switch ($action) {
  874. case 'new':
  875. $this->objects[$id] = array('t'=>'fontGIDtoCIDMap', 'info'=>$options);
  876. break;
  877. case 'out':
  878. $res = "\n$id 0 obj\n";
  879. $fontFileName = $o['info']['fontFileName'];
  880. $tmp = $this->fonts[$fontFileName]['CIDtoGID'] = base64_decode($this->fonts[$fontFileName]['CIDtoGID']);
  881. $compressed = isset($this->fonts[$fontFileName]['CIDtoGID_Compressed']) &&
  882. $this->fonts[$fontFileName]['CIDtoGID_Compressed'];
  883. if (!$compressed && isset($o['raw'])) {
  884. $res.= $tmp;
  885. } else {
  886. $res.= "<<";
  887. if (!$compressed && $this->compressionReady && $this->options['compression']) {
  888. // then implement ZLIB based compression on this content stream
  889. $compressed = true;
  890. $tmp = gzcompress($tmp, 6);
  891. }
  892. if ($compressed) {
  893. $res.= "\n/Filter /FlateDecode";
  894. }
  895. $res.= "\n/Length ".mb_strlen($tmp, '8bit') .">>\nstream\n$tmp\nendstream";
  896. }
  897. $res.= "\nendobj";
  898. return $res;
  899. }
  900. }
  901. /**
  902. * the document procset, solves some problems with printing to old PS printers
  903. */
  904. protected function o_procset($id, $action, $options = '') {
  905. if ($action !== 'new') {
  906. $o = & $this->objects[$id];
  907. }
  908. switch ($action) {
  909. case 'new':
  910. $this->objects[$id] = array('t'=>'procset', 'info'=>array('PDF'=>1, 'Text'=>1));
  911. $this->o_pages($this->currentNode, 'procset', $id);
  912. $this->procsetObjectId = $id;
  913. break;
  914. case 'add':
  915. // this is to add new items to the procset list, despite the fact that this is considered
  916. // obselete, the items are required for printing to some postscript printers
  917. switch ($options) {
  918. case 'ImageB':
  919. case 'ImageC':
  920. case 'ImageI':
  921. $o['info'][$options] = 1;
  922. break;
  923. }
  924. break;
  925. case 'out':
  926. $res = "\n$id 0 obj\n[";
  927. foreach ($o['info'] as $label=>$val) {
  928. $res.= "/$label ";
  929. }
  930. $res.= "]\nendobj";
  931. return $res;
  932. }
  933. }
  934. /**
  935. * define the document information
  936. */
  937. protected function o_info($id, $action, $options = '') {
  938. if ($action !== 'new') {
  939. $o = & $this->objects[$id];
  940. }
  941. switch ($action) {
  942. case 'new':
  943. $this->infoObject = $id;
  944. $date = 'D:'.@date('Ymd');
  945. $this->objects[$id] = array('t'=>'info', 'info'=>array('Creator'=>'R and OS php pdf writer, http://www.ros.co.nz', 'CreationDate'=>$date));
  946. break;
  947. case 'Title':
  948. case 'Author':
  949. case 'Subject':
  950. case 'Keywords':
  951. case 'Creator':
  952. case 'Producer':
  953. case 'CreationDate':
  954. case 'ModDate':
  955. case 'Trapped':
  956. $o['info'][$action] = $options;
  957. break;
  958. case 'out':
  959. if ($this->encrypted) {
  960. $this->encryptInit($id);
  961. }
  962. $res = "\n$id 0 obj\n<<\n";
  963. foreach ($o['info'] as $k=>$v) {
  964. $res.= "/$k (";
  965. // dates must be outputted as-is, without Unicode transformations
  966. $raw = ($k === 'CreationDate' || $k === 'ModDate');
  967. $c = $v;
  968. if ($this->encrypted) {
  969. $c = $this->ARC4($c);
  970. }
  971. $res.= ($raw) ? $c : $this->filterText($c);
  972. $res.= ")\n";
  973. }
  974. $res.= ">>\nendobj";
  975. return $res;
  976. }
  977. }
  978. /**
  979. * an action object, used to link to URLS initially
  980. */
  981. protected function o_action($id, $action, $options = '') {
  982. if ($action !== 'new') {
  983. $o = & $this->objects[$id];
  984. }
  985. switch ($action) {
  986. case 'new':
  987. if (is_array($options)) {
  988. $this->objects[$id] = array('t'=>'action', 'info'=>$options, 'type'=>$options['type']);
  989. } else {
  990. // then assume a URI action
  991. $this->objects[$id] = array('t'=>'action', 'info'=>$options, 'type'=>'URI');
  992. }
  993. break;
  994. case 'out':
  995. if ($this->encrypted) {
  996. $this->encryptInit($id);
  997. }
  998. $res = "\n$id 0 obj\n<< /Type /Action";
  999. switch ($o['type']) {
  1000. case 'ilink':
  1001. if (!isset($this->destinations[(string)$o['info']['label']])) break;
  1002. // there will be an 'label' setting, this is the name of the destination
  1003. $res.= "\n/S /GoTo\n/D ".$this->destinations[(string)$o['info']['label']]." 0 R";
  1004. break;
  1005. case 'URI':
  1006. $res.= "\n/S /URI\n/URI (";
  1007. if ($this->encrypted) {
  1008. $res.= $this->filterText($this->ARC4($o['info']), true, false);
  1009. } else {
  1010. $res.= $this->filterText($o['info'], true, false);
  1011. }
  1012. $res.= ")";
  1013. break;
  1014. }
  1015. $res.= "\n>>\nendobj";
  1016. return $res;
  1017. }
  1018. }
  1019. /**
  1020. * an annotation object, this will add an annotation to the current page.
  1021. * initially will support just link annotations
  1022. */
  1023. protected function o_annotation($id, $action, $options = '') {
  1024. if ($action !== 'new') {
  1025. $o = & $this->objects[$id];
  1026. }
  1027. switch ($action) {
  1028. case 'new':
  1029. // add the annotation to the current page
  1030. $pageId = $this->currentPage;
  1031. $this->o_page($pageId, 'annot', $id);
  1032. // and add the action object which is going to be required
  1033. switch ($options['type']) {
  1034. case 'link':
  1035. $this->objects[$id] = array('t'=>'annotation', 'info'=>$options);
  1036. $this->numObj++;
  1037. $this->o_action($this->numObj, 'new', $options['url']);
  1038. $this->objects[$id]['info']['actionId'] = $this->numObj;
  1039. break;
  1040. case 'ilink':
  1041. // this is to a named internal link
  1042. $label = $options['label'];
  1043. $this->objects[$id] = array('t'=>'annotation', 'info'=>$options);
  1044. $this->numObj++;
  1045. $this->o_action($this->numObj, 'new', array('type'=>'ilink', 'label'=>$label));
  1046. $this->objects[$id]['info']['actionId'] = $this->numObj;
  1047. break;
  1048. }
  1049. break;
  1050. case 'out':
  1051. $res = "\n$id 0 obj\n<< /Type /Annot";
  1052. switch ($o['info']['type']) {
  1053. case 'link':
  1054. case 'ilink':
  1055. $res.= "\n/Subtype /Link";
  1056. break;
  1057. }
  1058. $res.= "\n/A ".$o['info']['actionId']." 0 R";
  1059. $res.= "\n/Border [0 0 0]";
  1060. $res.= "\n/H /I";
  1061. $res.= "\n/Rect [ ";
  1062. foreach($o['info']['rect'] as $v) {
  1063. $res.= sprintf("%.4F ", $v);
  1064. }
  1065. $res.= "]";
  1066. $res.= "\n>>\nendobj";
  1067. return $res;
  1068. }
  1069. }
  1070. /**
  1071. * a page object, it also creates a contents object to hold its contents
  1072. */
  1073. protected function o_page($id, $action, $options = '') {
  1074. if ($action !== 'new') {
  1075. $o = & $this->objects[$id];
  1076. }
  1077. switch ($action) {
  1078. case 'new':
  1079. $this->numPages++;
  1080. $this->objects[$id] = array('t'=>'page', 'info'=>array('parent'=>$this->currentNode, 'pageNum'=>$this->numPages));
  1081. if (is_array($options)) {
  1082. // then this must be a page insertion, array should contain 'rid','pos'=[before|after]
  1083. $options['id'] = $id;
  1084. $this->o_pages($this->currentNode, 'page', $options);
  1085. } else {
  1086. $this->o_pages($this->currentNode, 'page', $id);
  1087. }
  1088. $this->currentPage = $id;
  1089. //make a contents object to go with this page
  1090. $this->numObj++;
  1091. $this->o_contents($this->numObj, 'new', $id);
  1092. $this->currentContents = $this->numObj;
  1093. $this->objects[$id]['info']['contents'] = array();
  1094. $this->objects[$id]['info']['contents'][] = $this->numObj;
  1095. $match = ($this->numPages%2 ? 'odd' : 'even');
  1096. foreach($this->addLooseObjects as $oId=>$target) {
  1097. if ($target === 'all' || $match === $target) {
  1098. $this->objects[$id]['info']['contents'][] = $oId;
  1099. }
  1100. }
  1101. break;
  1102. case 'content':
  1103. $o['info']['contents'][] = $options;
  1104. break;
  1105. case 'annot':
  1106. // add an annotation to this page
  1107. if (!isset($o['info']['annot'])) {
  1108. $o['info']['annot'] = array();
  1109. }
  1110. // $options should contain the id of the annotation dictionary
  1111. $o['info']['annot'][] = $options;
  1112. break;
  1113. case 'out':
  1114. $res = "\n$id 0 obj\n<< /Type /Page";
  1115. $res.= "\n/Parent ".$o['info']['parent']." 0 R";
  1116. if (isset($o['info']['annot'])) {
  1117. $res.= "\n/Annots [";
  1118. foreach($o['info']['annot'] as $aId) {
  1119. $res.= " $aId 0 R";
  1120. }
  1121. $res.= " ]";
  1122. }
  1123. $count = count($o['info']['contents']);
  1124. if ($count == 1) {
  1125. $res.= "\n/Contents ".$o['info']['contents'][0]." 0 R";
  1126. } else if ($count>1) {
  1127. $res.= "\n/Contents [\n";
  1128. // reverse the page contents so added objects are below normal content
  1129. //foreach (array_reverse($o['info']['contents']) as $cId) {
  1130. // Back to normal now that I've got transparency working --Benj
  1131. foreach ($o['info']['contents'] as $cId) {
  1132. $res.= "$cId 0 R\n";
  1133. }
  1134. $res.= "]";
  1135. }
  1136. $res.= "\n>>\nendobj";
  1137. return $res;
  1138. }
  1139. }
  1140. /**
  1141. * the contents objects hold all of the content which appears on pages
  1142. */
  1143. protected function o_contents($id, $action, $options = '') {
  1144. if ($action !== 'new') {
  1145. $o = & $this->objects[$id];
  1146. }
  1147. switch ($action) {
  1148. case 'new':
  1149. $this->objects[$id] = array('t'=>'contents', 'c'=>'', 'info'=>array());
  1150. if (mb_strlen($options, '8bit') && intval($options)) {
  1151. // then this contents is the primary for a page
  1152. $this->objects[$id]['onPage'] = $options;
  1153. } else if ($options === 'raw') {
  1154. // then this page contains some other type of system object
  1155. $this->objects[$id]['raw'] = 1;
  1156. }
  1157. break;
  1158. case 'add':
  1159. // add more options to the decleration
  1160. foreach ($options as $k=>$v) {
  1161. $o['info'][$k] = $v;
  1162. }
  1163. case 'out':
  1164. $tmp = $o['c'];
  1165. $res = "\n$id 0 obj\n";
  1166. if (isset($this->objects[$id]['raw'])) {
  1167. $res.= $tmp;
  1168. } else {
  1169. $res.= "<<";
  1170. if ($this->compressionReady && $this->options['compression']) {
  1171. // then implement ZLIB based compression on this content stream
  1172. $res.= " /Filter /FlateDecode";
  1173. $tmp = gzcompress($tmp, 6);
  1174. }
  1175. if ($this->encrypted) {
  1176. $this->encryptInit($id);
  1177. $tmp = $this->ARC4($tmp);
  1178. }
  1179. foreach($o['info'] as $k=>$v) {
  1180. $res.= "\n/$k $v";
  1181. }
  1182. $res.= "\n/Length ".mb_strlen($tmp, '8bit') ." >>\nstream\n$tmp\nendstream";
  1183. }
  1184. $res.= "\nendobj";
  1185. return $res;
  1186. }
  1187. }
  1188. protected function o_embedjs($id, $action, $code = '') {
  1189. if ($action !== 'new') {
  1190. $o = & $this->objects[$id];
  1191. }
  1192. switch ($action) {
  1193. case 'new':
  1194. $this->objects[$id] = array('t'=>'embedjs', 'info'=>array(
  1195. 'Names' => '[(EmbeddedJS) '.($id+1).' 0 R]'
  1196. ));
  1197. break;
  1198. case 'out':
  1199. $res = "\n$id 0 obj\n<< ";
  1200. foreach($o['info'] as $k=>$v) {
  1201. $res.= "\n/$k $v";
  1202. }
  1203. $res.= "\n>>\nendobj";
  1204. return $res;
  1205. }
  1206. }
  1207. protected function o_javascript($id, $action, $code = '') {
  1208. if ($action !== 'new') {
  1209. $o = & $this->objects[$id];
  1210. }
  1211. switch ($action) {
  1212. case 'new':
  1213. $this->objects[$id] = array('t'=>'javascript', 'info'=>array(
  1214. 'S' => '/JavaScript',
  1215. 'JS' => '('.$this->filterText($code).')',
  1216. ));
  1217. break;
  1218. case 'out':
  1219. $res = "\n$id 0 obj\n<< ";
  1220. foreach($o['info'] as $k=>$v) {
  1221. $res.= "\n/$k $v";
  1222. }
  1223. $res.= "\n>>\nendobj";
  1224. return $res;
  1225. }
  1226. }
  1227. /**
  1228. * an image object, will be an XObject in the document, includes description and data
  1229. */
  1230. protected function o_image($id, $action, $options = '') {
  1231. if ($action !== 'new') {
  1232. $o = & $this->objects[$id];
  1233. }
  1234. switch ($action) {
  1235. case 'new':
  1236. // make the new object
  1237. $this->objects[$id] = array('t'=>'image', 'data'=>&$options['data'], 'info'=>array());
  1238. $info =& $this->objects[$id]['info'];
  1239. $info['Type'] = '/XObject';
  1240. $info['Subtype'] = '/Image';
  1241. $info['Width'] = $options['iw'];
  1242. $info['Height'] = $options['ih'];
  1243. if (isset($options['masked']) && $options['masked']) {
  1244. $info['SMask'] = ($this->numObj-1).' 0 R';
  1245. }
  1246. if (!isset($options['type']) || $options['type'] === 'jpg') {
  1247. if (!isset($options['channels'])) {
  1248. $options['channels'] = 3;
  1249. }
  1250. switch ($options['channels']) {
  1251. case 1: $info['ColorSpace'] = '/DeviceGray'; break;
  1252. case 4: $info['ColorSpace'] = '/DeviceCMYK'; break;
  1253. default: $info['ColorSpace'] = '/DeviceRGB'; break;
  1254. }
  1255. if ($info['ColorSpace'] === '/DeviceCMYK') {
  1256. $info['Decode'] = '[1 0 1 0 1 0 1 0]';
  1257. }
  1258. $info['Filter'] = '/DCTDecode';
  1259. $info['BitsPerComponent'] = 8;
  1260. }
  1261. else if ($options['type'] === 'png') {
  1262. $info['Filter'] = '/FlateDecode';
  1263. $info['DecodeParms'] = '<< /Predictor 15 /Colors '.$options['ncolor'].' /Columns '.$options['iw'].' /BitsPerComponent '.$options['bitsPerComponent'].'>>';
  1264. if ($options['isMask']) {
  1265. $info['ColorSpace'] = '/DeviceGray';
  1266. }
  1267. else {
  1268. if (mb_strlen($options['pdata'], '8bit')) {
  1269. $tmp = ' [ /Indexed /DeviceRGB '.(mb_strlen($options['pdata'], '8bit') /3-1) .' ';
  1270. $this->numObj++;
  1271. $this->o_contents($this->numObj, 'new');
  1272. $this->objects[$this->numObj]['c'] = $options['pdata'];
  1273. $tmp.= $this->numObj.' 0 R';
  1274. $tmp.= ' ]';
  1275. $info['ColorSpace'] = $tmp;
  1276. if (isset($options['transparency'])) {
  1277. $transparency = $options['transparency'];
  1278. switch ($transparency['type']) {
  1279. case 'indexed':
  1280. $tmp = ' [ '.$transparency['data'].' '.$transparency['data'].'] ';
  1281. $info['Mask'] = $tmp;
  1282. break;
  1283. case 'color-key':
  1284. $tmp = ' [ '.
  1285. $transparency['r'] . ' ' . $transparency['r'] .
  1286. $transparency['g'] . ' ' . $transparency['g'] .
  1287. $transparency['b'] . ' ' . $transparency['b'] .
  1288. ' ] ';
  1289. $info['Mask'] = $tmp;
  1290. break;
  1291. }
  1292. }
  1293. } else {
  1294. if (isset($options['transparency'])) {
  1295. $transparency = $options['transparency'];
  1296. switch ($transparency['type']) {
  1297. case 'indexed':
  1298. $tmp = ' [ '.$transparency['data'].' '.$transparency['data'].'] ';
  1299. $info['Mask'] = $tmp;
  1300. break;
  1301. case 'color-key':
  1302. $tmp = ' [ '.
  1303. $transparency['r'] . ' ' . $transparency['r'] . ' ' .
  1304. $transparency['g'] . ' ' . $transparency['g'] . ' ' .
  1305. $transparency['b'] . ' ' . $transparency['b'] .
  1306. ' ] ';
  1307. $info['Mask'] = $tmp;
  1308. break;
  1309. }
  1310. }
  1311. $info['ColorSpace'] = '/'.$options['color'];
  1312. }
  1313. }
  1314. $info['BitsPerComponent'] = $options['bitsPerComponent'];
  1315. }
  1316. // assign it a place in the named resource dictionary as an external object, according to
  1317. // the label passed in with it.
  1318. $this->o_pages($this->currentNode, 'xObject', array('label'=>$options['label'], 'objNum'=>$id));
  1319. // also make sure that we have the right procset object for it.
  1320. $this->o_procset($this->procsetObjectId, 'add', 'ImageC');
  1321. break;
  1322. case 'out':
  1323. $tmp = &$o['data'];
  1324. $res = "\n$id 0 obj\n<<";
  1325. foreach($o['info'] as $k=>$v) {
  1326. $res.= "\n/$k $v";
  1327. }
  1328. if ($this->encrypted) {
  1329. $this->encryptInit($id);
  1330. $tmp = $this->ARC4($tmp);
  1331. }
  1332. $res.= "\n/Length ".mb_strlen($tmp, '8bit') .">>\nstream\n$tmp\nendstream\nendobj";
  1333. return $res;
  1334. }
  1335. }
  1336. /**
  1337. * graphics state object
  1338. */
  1339. protected function o_extGState($id, $action, $options = "") {
  1340. static $valid_params = array("LW", "LC", "LC", "LJ", "ML",
  1341. "D", "RI", "OP", "op", "OPM",
  1342. "Font", "BG", "BG2", "UCR",
  1343. "TR", "TR2", "HT", "FL",
  1344. "SM", "SA", "BM", "SMask",
  1345. "CA", "ca", "AIS", "TK");
  1346. if ($action !== "new") {
  1347. $o = & $this->objects[$id];
  1348. }
  1349. switch ($action) {
  1350. case "new":
  1351. $this->objects[$id] = array('t' => 'extGState', 'info' => $options);
  1352. // Tell the pages about the new resource
  1353. $this->numStates++;
  1354. $this->o_pages($this->currentNode, 'extGState', array("objNum" => $id, "stateNum" => $this->numStates));
  1355. break;
  1356. case "out":
  1357. $res = "\n$id 0 obj\n<< /Type /ExtGState\n";
  1358. foreach ($o["info"] as $k => $v) {
  1359. if ( !in_array($k, $valid_params))
  1360. continue;
  1361. $res.= "/$k $v\n";
  1362. }
  1363. $res.= ">>\nendobj";
  1364. return $res;
  1365. }
  1366. }
  1367. /**
  1368. * encryption object.
  1369. */
  1370. protected function o_encryption($id, $action, $options = '') {
  1371. if ($action !== 'new') {
  1372. $o = & $this->objects[$id];
  1373. }
  1374. switch ($action) {
  1375. case 'new':
  1376. // make the new object
  1377. $this->objects[$id] = array('t'=>'encryption', 'info'=>$options);
  1378. $this->arc4_objnum = $id;
  1379. // figure out the additional paramaters required
  1380. $pad = chr(0x28) .chr(0xBF) .chr(0x4E) .chr(0x5E) .chr(0x4E) .chr(0x75) .chr(0x8A) .chr(0x41)
  1381. .chr(0x64) .chr(0x00) .chr(0x4E) .chr(0x56) .chr(0xFF) .chr(0xFA) .chr(0x01) .chr(0x08)
  1382. .chr(0x2E) .chr(0x2E) .chr(0x00) .chr(0xB6) .chr(0xD0) .chr(0x68) .chr(0x3E) .chr(0x80)
  1383. .chr(0x2F) .chr(0x0C) .chr(0xA9) .chr(0xFE) .chr(0x64) .chr(0x53) .chr(0x69) .chr(0x7A);
  1384. $len = mb_strlen($options['owner'], '8bit');
  1385. if ($len>32) {
  1386. $owner = substr($options['owner'], 0, 32);
  1387. } else if ($len<32) {
  1388. $owner = $options['owner'].substr($pad, 0, 32-$len);
  1389. } else {
  1390. $owner = $options['owner'];
  1391. }
  1392. $len = mb_strlen($options['user'], '8bit');
  1393. if ($len>32) {
  1394. $user = substr($options['user'], 0, 32);
  1395. } else if ($len<32) {
  1396. $user = $options['user'].substr($pad, 0, 32-$len);
  1397. } else {
  1398. $user = $options['user'];
  1399. }
  1400. $tmp = $this->md5_16($owner);
  1401. $okey = substr($tmp, 0, 5);
  1402. $this->ARC4_init($okey);
  1403. $ovalue = $this->ARC4($user);
  1404. $this->objects[$id]['info']['O'] = $ovalue;
  1405. // now make the u value, phew.
  1406. $tmp = $this->md5_16($user.$ovalue.chr($options['p']) .chr(255) .chr(255) .chr(255) .$this->fileIdentifier);
  1407. $ukey = substr($tmp, 0, 5);
  1408. $this->ARC4_init($ukey);
  1409. $this->encryptionKey = $ukey;
  1410. $this->encrypted = true;
  1411. $uvalue = $this->ARC4($pad);
  1412. $this->objects[$id]['info']['U'] = $uvalue;
  1413. $this->encryptionKey = $ukey;
  1414. // initialize the arc4 array
  1415. break;
  1416. case 'out':
  1417. $res = "\n$id 0 obj\n<<";
  1418. $res.= "\n/Filter /Standard";
  1419. $res.= "\n/V 1";
  1420. $res.= "\n/R 2";
  1421. $res.

Large files files are truncated, but you can click here to view the full file