PageRenderTime 65ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/includes/dompdf/lib/class.pdf.php

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

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