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

/lib/class.pdf.php

https://github.com/dwagner2301/dompdf
PHP | 4607 lines | 2833 code | 736 blank | 1038 comment | 554 complexity | ed7e198371529c7aa5db09e0d9b921e0 MD5 | raw file
Possible License(s): LGPL-2.1

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

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