PageRenderTime 78ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/library/classes/class.pdf.php

https://bitbucket.org/DenizYldrm/openemr
PHP | 3088 lines | 2179 code | 170 blank | 739 comment | 394 complexity | 4d3201419558d62a7a758d609f573604 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, MPL-2.0, LGPL-2.1
  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. var $numObj=0;
  28. /**
  29. * this array contains all of the pdf objects, ready for final assembly
  30. */
  31. var $objects = array();
  32. /**
  33. * the objectId (number within the objects array) of the document catalog
  34. */
  35. var $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. var $fonts=array();
  41. /**
  42. * a record of the current font
  43. */
  44. var $currentFont='';
  45. /**
  46. * the current base font
  47. */
  48. var $currentBaseFont='';
  49. /**
  50. * the number of the current font within the font array
  51. */
  52. var $currentFontNum=0;
  53. /**
  54. *
  55. */
  56. var $currentNode;
  57. /**
  58. * object number of the current page
  59. */
  60. var $currentPage;
  61. /**
  62. * object number of the currently active contents block
  63. */
  64. var $currentContents;
  65. /**
  66. * number of fonts within the system
  67. */
  68. var $numFonts=0;
  69. /**
  70. * current colour for fill operations, defaults to inactive value, all three components should be between 0 and 1 inclusive when active
  71. */
  72. var $currentColour=array('r'=>-1,'g'=>-1,'b'=>-1);
  73. /**
  74. * current colour for stroke operations (lines etc.)
  75. */
  76. var $currentStrokeColour=array('r'=>-1,'g'=>-1,'b'=>-1);
  77. /**
  78. * current style that lines are drawn in
  79. */
  80. var $currentLineStyle='';
  81. /**
  82. * an array which is used to save the state of the document, mainly the colours and styles
  83. * it is used to temporarily change to another state, the change back to what it was before
  84. */
  85. var $stateStack = array();
  86. /**
  87. * number of elements within the state stack
  88. */
  89. var $nStateStack = 0;
  90. /**
  91. * number of page objects within the document
  92. */
  93. var $numPages=0;
  94. /**
  95. * object Id storage stack
  96. */
  97. var $stack=array();
  98. /**
  99. * number of elements within the object Id storage stack
  100. */
  101. var $nStack=0;
  102. /**
  103. * an array which contains information about the objects which are not firmly attached to pages
  104. * these have been added with the addObject function
  105. */
  106. var $looseObjects=array();
  107. /**
  108. * array contains infomation about how the loose objects are to be added to the document
  109. */
  110. var $addLooseObjects=array();
  111. /**
  112. * the objectId of the information object for the document
  113. * this contains authorship, title etc.
  114. */
  115. var $infoObject=0;
  116. /**
  117. * number of images being tracked within the document
  118. */
  119. var $numImages=0;
  120. /**
  121. * an array containing options about the document
  122. * it defaults to turning on the compression of the objects
  123. */
  124. var $options=array('compression'=>1);
  125. /**
  126. * the objectId of the first page of the document
  127. */
  128. var $firstPageId;
  129. /**
  130. * used to track the last used value of the inter-word spacing, this is so that it is known
  131. * when the spacing is changed.
  132. */
  133. var $wordSpaceAdjust=0;
  134. /**
  135. * the object Id of the procset object
  136. */
  137. var $procsetObjectId;
  138. /**
  139. * store the information about the relationship between font families
  140. * this used so that the code knows which font is the bold version of another font, etc.
  141. * the value of this array is initialised in the constuctor function.
  142. */
  143. var $fontFamilies = array();
  144. /**
  145. * track if the current font is bolded or italicised
  146. */
  147. var $currentTextState = '';
  148. /**
  149. * messages are stored here during processing, these can be selected afterwards to give some useful debug information
  150. */
  151. var $messages='';
  152. /**
  153. * the ancryption array for the document encryption is stored here
  154. */
  155. var $arc4='';
  156. /**
  157. * the object Id of the encryption information
  158. */
  159. var $arc4_objnum=0;
  160. /**
  161. * the file identifier, used to uniquely identify a pdf document
  162. */
  163. var $fileIdentifier='';
  164. /**
  165. * a flag to say if a document is to be encrypted or not
  166. */
  167. var $encrypted=0;
  168. /**
  169. * the ancryption key for the encryption of all the document content (structure is not encrypted)
  170. */
  171. var $encryptionKey='';
  172. /**
  173. * array which forms a stack to keep track of nested callback functions
  174. */
  175. var $callback = array();
  176. /**
  177. * the number of callback functions in the callback array
  178. */
  179. var $nCallback = 0;
  180. /**
  181. * store label->id pairs for named destinations, these will be used to replace internal links
  182. * done this way so that destinations can be defined after the location that links to them
  183. */
  184. var $destinations = array();
  185. /**
  186. * store the stack for the transaction commands, each item in here is a record of the values of all the
  187. * variables within the class, so that the user can rollback at will (from each 'start' command)
  188. * note that this includes the objects array, so these can be large.
  189. */
  190. var $checkpoint = '';
  191. /**
  192. * class constructor
  193. * this will start a new document
  194. * @var array array of 4 numbers, defining the bottom left and upper right corner of the page. first two are normally zero.
  195. */
  196. function Cpdf ($pageSize=array(0,0,612,792)){
  197. $this->newDocument($pageSize);
  198. // also initialize the font families that are known about already
  199. $this->setFontFamily('init');
  200. // $this->fileIdentifier = md5('xxxxxxxx'.time());
  201. }
  202. /**
  203. * Document object methods (internal use only)
  204. *
  205. * There is about one object method for each type of object in the pdf document
  206. * Each function has the same call list ($id,$action,$options).
  207. * $id = the object ID of the object, or what it is to be if it is being created
  208. * $action = a string specifying the action to be performed, though ALL must support:
  209. * 'new' - create the object with the id $id
  210. * 'out' - produce the output for the pdf object
  211. * $options = optional, a string or array containing the various parameters for the object
  212. *
  213. * These, in conjunction with the output function are the ONLY way for output to be produced
  214. * within the pdf 'file'.
  215. */
  216. /**
  217. *destination object, used to specify the location for the user to jump to, presently on opening
  218. */
  219. function o_destination($id,$action,$options=''){
  220. if ($action!='new'){
  221. $o =& $this->objects[$id];
  222. }
  223. switch($action){
  224. case 'new':
  225. $this->objects[$id]=array('t'=>'destination','info'=>array());
  226. $tmp = '';
  227. switch ($options['type']){
  228. case 'XYZ':
  229. case 'FitR':
  230. $tmp = ' '.$options['p3'].$tmp;
  231. case 'FitH':
  232. case 'FitV':
  233. case 'FitBH':
  234. case 'FitBV':
  235. $tmp = ' '.$options['p1'].' '.$options['p2'].$tmp;
  236. case 'Fit':
  237. case 'FitB':
  238. $tmp = $options['type'].$tmp;
  239. $this->objects[$id]['info']['string']=$tmp;
  240. $this->objects[$id]['info']['page']=$options['page'];
  241. }
  242. break;
  243. case 'out':
  244. $tmp = $o['info'];
  245. $res="\n".$id." 0 obj\n".'['.$tmp['page'].' 0 R /'.$tmp['string']."]\nendobj\n";
  246. return $res;
  247. break;
  248. }
  249. }
  250. /**
  251. * set the viewer preferences
  252. */
  253. function o_viewerPreferences($id,$action,$options=''){
  254. if ($action!='new'){
  255. $o =& $this->objects[$id];
  256. }
  257. switch ($action){
  258. case 'new':
  259. $this->objects[$id]=array('t'=>'viewerPreferences','info'=>array());
  260. break;
  261. case 'add':
  262. foreach($options as $k=>$v){
  263. switch ($k){
  264. case 'HideToolbar':
  265. case 'HideMenubar':
  266. case 'HideWindowUI':
  267. case 'FitWindow':
  268. case 'CenterWindow':
  269. case 'NonFullScreenPageMode':
  270. case 'Direction':
  271. $o['info'][$k]=$v;
  272. break;
  273. }
  274. }
  275. break;
  276. case 'out':
  277. $res="\n".$id." 0 obj\n".'<< ';
  278. foreach($o['info'] as $k=>$v){
  279. $res.="\n/".$k.' '.$v;
  280. }
  281. $res.="\n>>\n";
  282. return $res;
  283. break;
  284. }
  285. }
  286. /**
  287. * define the document catalog, the overall controller for the document
  288. */
  289. function o_catalog($id,$action,$options=''){
  290. if ($action!='new'){
  291. $o =& $this->objects[$id];
  292. }
  293. switch ($action){
  294. case 'new':
  295. $this->objects[$id]=array('t'=>'catalog','info'=>array());
  296. $this->catalogId=$id;
  297. break;
  298. case 'outlines':
  299. case 'pages':
  300. case 'openHere':
  301. $o['info'][$action]=$options;
  302. break;
  303. case 'viewerPreferences':
  304. if (!isset($o['info']['viewerPreferences'])){
  305. $this->numObj++;
  306. $this->o_viewerPreferences($this->numObj,'new');
  307. $o['info']['viewerPreferences']=$this->numObj;
  308. }
  309. $vp = $o['info']['viewerPreferences'];
  310. $this->o_viewerPreferences($vp,'add',$options);
  311. break;
  312. case 'out':
  313. $res="\n".$id." 0 obj\n".'<< /Type /Catalog';
  314. foreach($o['info'] as $k=>$v){
  315. switch($k){
  316. case 'outlines':
  317. $res.="\n".'/Outlines '.$v.' 0 R';
  318. break;
  319. case 'pages':
  320. $res.="\n".'/Pages '.$v.' 0 R';
  321. break;
  322. case 'viewerPreferences':
  323. $res.="\n".'/ViewerPreferences '.$o['info']['viewerPreferences'].' 0 R';
  324. break;
  325. case 'openHere':
  326. $res.="\n".'/OpenAction '.$o['info']['openHere'].' 0 R';
  327. break;
  328. }
  329. }
  330. $res.=" >>\nendobj";
  331. return $res;
  332. break;
  333. }
  334. }
  335. /**
  336. * object which is a parent to the pages in the document
  337. */
  338. function o_pages($id,$action,$options=''){
  339. if ($action!='new'){
  340. $o =& $this->objects[$id];
  341. }
  342. switch ($action){
  343. case 'new':
  344. $this->objects[$id]=array('t'=>'pages','info'=>array());
  345. $this->o_catalog($this->catalogId,'pages',$id);
  346. break;
  347. case 'page':
  348. if (!is_array($options)){
  349. // then it will just be the id of the new page
  350. $o['info']['pages'][]=$options;
  351. } else {
  352. // then it should be an array having 'id','rid','pos', where rid=the page to which this one will be placed relative
  353. // and pos is either 'before' or 'after', saying where this page will fit.
  354. if (isset($options['id']) && isset($options['rid']) && isset($options['pos'])){
  355. $i = array_search($options['rid'],$o['info']['pages']);
  356. if (isset($o['info']['pages'][$i]) && $o['info']['pages'][$i]==$options['rid']){
  357. // then there is a match
  358. // make a space
  359. switch ($options['pos']){
  360. case 'before':
  361. $k = $i;
  362. break;
  363. case 'after':
  364. $k=$i+1;
  365. break;
  366. default:
  367. $k=-1;
  368. break;
  369. }
  370. if ($k>=0){
  371. for ($j=count($o['info']['pages'])-1;$j>=$k;$j--){
  372. $o['info']['pages'][$j+1]=$o['info']['pages'][$j];
  373. }
  374. $o['info']['pages'][$k]=$options['id'];
  375. }
  376. }
  377. }
  378. }
  379. break;
  380. case 'procset':
  381. $o['info']['procset']=$options;
  382. break;
  383. case 'mediaBox':
  384. $o['info']['mediaBox']=$options; // which should be an array of 4 numbers
  385. break;
  386. case 'font':
  387. $o['info']['fonts'][]=array('objNum'=>$options['objNum'],'fontNum'=>$options['fontNum']);
  388. break;
  389. case 'xObject':
  390. $o['info']['xObjects'][]=array('objNum'=>$options['objNum'],'label'=>$options['label']);
  391. break;
  392. case 'out':
  393. if (count($o['info']['pages'])){
  394. $res="\n".$id." 0 obj\n<< /Type /Pages\n/Kids [";
  395. foreach($o['info']['pages'] as $k=>$v){
  396. $res.=$v." 0 R\n";
  397. }
  398. $res.="]\n/Count ".count($this->objects[$id]['info']['pages']);
  399. if ((isset($o['info']['fonts']) && count($o['info']['fonts'])) || isset($o['info']['procset'])){
  400. $res.="\n/Resources <<";
  401. if (isset($o['info']['procset'])){
  402. $res.="\n/ProcSet ".$o['info']['procset']." 0 R";
  403. }
  404. if (isset($o['info']['fonts']) && count($o['info']['fonts'])){
  405. $res.="\n/Font << ";
  406. foreach($o['info']['fonts'] as $finfo){
  407. $res.="\n/F".$finfo['fontNum']." ".$finfo['objNum']." 0 R";
  408. }
  409. $res.=" >>";
  410. }
  411. if (isset($o['info']['xObjects']) && count($o['info']['xObjects'])){
  412. $res.="\n/XObject << ";
  413. foreach($o['info']['xObjects'] as $finfo){
  414. $res.="\n/".$finfo['label']." ".$finfo['objNum']." 0 R";
  415. }
  416. $res.=" >>";
  417. }
  418. $res.="\n>>";
  419. if (isset($o['info']['mediaBox'])){
  420. $tmp=$o['info']['mediaBox'];
  421. $res.="\n/MediaBox [".sprintf('%.3f',$tmp[0]).' '.sprintf('%.3f',$tmp[1]).' '.sprintf('%.3f',$tmp[2]).' '.sprintf('%.3f',$tmp[3]).']';
  422. }
  423. }
  424. $res.="\n >>\nendobj";
  425. } else {
  426. $res="\n".$id." 0 obj\n<< /Type /Pages\n/Count 0\n>>\nendobj";
  427. }
  428. return $res;
  429. break;
  430. }
  431. }
  432. /**
  433. * define the outlines in the doc, empty for now
  434. */
  435. function o_outlines($id,$action,$options=''){
  436. if ($action!='new'){
  437. $o =& $this->objects[$id];
  438. }
  439. switch ($action){
  440. case 'new':
  441. $this->objects[$id]=array('t'=>'outlines','info'=>array('outlines'=>array()));
  442. $this->o_catalog($this->catalogId,'outlines',$id);
  443. break;
  444. case 'outline':
  445. $o['info']['outlines'][]=$options;
  446. break;
  447. case 'out':
  448. if (count($o['info']['outlines'])){
  449. $res="\n".$id." 0 obj\n<< /Type /Outlines /Kids [";
  450. foreach($o['info']['outlines'] as $k=>$v){
  451. $res.=$v." 0 R ";
  452. }
  453. $res.="] /Count ".count($o['info']['outlines'])." >>\nendobj";
  454. } else {
  455. $res="\n".$id." 0 obj\n<< /Type /Outlines /Count 0 >>\nendobj";
  456. }
  457. return $res;
  458. break;
  459. }
  460. }
  461. /**
  462. * an object to hold the font description
  463. */
  464. function o_font($id,$action,$options=''){
  465. if ($action!='new'){
  466. $o =& $this->objects[$id];
  467. }
  468. switch ($action){
  469. case 'new':
  470. $this->objects[$id]=array('t'=>'font','info'=>array('name'=>$options['name'],'SubType'=>'Type1'));
  471. $fontNum=$this->numFonts;
  472. $this->objects[$id]['info']['fontNum']=$fontNum;
  473. // deal with the encoding and the differences
  474. if (isset($options['differences'])){
  475. // then we'll need an encoding dictionary
  476. $this->numObj++;
  477. $this->o_fontEncoding($this->numObj,'new',$options);
  478. $this->objects[$id]['info']['encodingDictionary']=$this->numObj;
  479. } else if (isset($options['encoding'])){
  480. // we can specify encoding here
  481. switch($options['encoding']){
  482. case 'WinAnsiEncoding':
  483. case 'MacRomanEncoding':
  484. case 'MacExpertEncoding':
  485. $this->objects[$id]['info']['encoding']=$options['encoding'];
  486. break;
  487. case 'none':
  488. break;
  489. default:
  490. $this->objects[$id]['info']['encoding']='WinAnsiEncoding';
  491. break;
  492. }
  493. } else {
  494. $this->objects[$id]['info']['encoding']='WinAnsiEncoding';
  495. }
  496. // also tell the pages node about the new font
  497. $this->o_pages($this->currentNode,'font',array('fontNum'=>$fontNum,'objNum'=>$id));
  498. break;
  499. case 'add':
  500. foreach ($options as $k=>$v){
  501. switch ($k){
  502. case 'BaseFont':
  503. $o['info']['name'] = $v;
  504. break;
  505. case 'FirstChar':
  506. case 'LastChar':
  507. case 'Widths':
  508. case 'FontDescriptor':
  509. case 'SubType':
  510. $this->addMessage('o_font '.$k." : ".$v);
  511. $o['info'][$k] = $v;
  512. break;
  513. }
  514. }
  515. break;
  516. case 'out':
  517. $res="\n".$id." 0 obj\n<< /Type /Font\n/Subtype /".$o['info']['SubType']."\n";
  518. $res.="/Name /F".$o['info']['fontNum']."\n";
  519. $res.="/BaseFont /".$o['info']['name']."\n";
  520. if (isset($o['info']['encodingDictionary'])){
  521. // then place a reference to the dictionary
  522. $res.="/Encoding ".$o['info']['encodingDictionary']." 0 R\n";
  523. } else if (isset($o['info']['encoding'])){
  524. // use the specified encoding
  525. $res.="/Encoding /".$o['info']['encoding']."\n";
  526. }
  527. if (isset($o['info']['FirstChar'])){
  528. $res.="/FirstChar ".$o['info']['FirstChar']."\n";
  529. }
  530. if (isset($o['info']['LastChar'])){
  531. $res.="/LastChar ".$o['info']['LastChar']."\n";
  532. }
  533. if (isset($o['info']['Widths'])){
  534. $res.="/Widths ".$o['info']['Widths']." 0 R\n";
  535. }
  536. if (isset($o['info']['FontDescriptor'])){
  537. $res.="/FontDescriptor ".$o['info']['FontDescriptor']." 0 R\n";
  538. }
  539. $res.=">>\nendobj";
  540. return $res;
  541. break;
  542. }
  543. }
  544. /**
  545. * a font descriptor, needed for including additional fonts
  546. */
  547. function o_fontDescriptor($id,$action,$options=''){
  548. if ($action!='new'){
  549. $o =& $this->objects[$id];
  550. }
  551. switch ($action){
  552. case 'new':
  553. $this->objects[$id]=array('t'=>'fontDescriptor','info'=>$options);
  554. break;
  555. case 'out':
  556. $res="\n".$id." 0 obj\n<< /Type /FontDescriptor\n";
  557. foreach ($o['info'] as $label => $value){
  558. switch ($label){
  559. case 'Ascent':
  560. case 'CapHeight':
  561. case 'Descent':
  562. case 'Flags':
  563. case 'ItalicAngle':
  564. case 'StemV':
  565. case 'AvgWidth':
  566. case 'Leading':
  567. case 'MaxWidth':
  568. case 'MissingWidth':
  569. case 'StemH':
  570. case 'XHeight':
  571. case 'CharSet':
  572. if (strlen($value)){
  573. $res.='/'.$label.' '.$value."\n";
  574. }
  575. break;
  576. case 'FontFile':
  577. case 'FontFile2':
  578. case 'FontFile3':
  579. $res.='/'.$label.' '.$value." 0 R\n";
  580. break;
  581. case 'FontBBox':
  582. $res.='/'.$label.' ['.$value[0].' '.$value[1].' '.$value[2].' '.$value[3]."]\n";
  583. break;
  584. case 'FontName':
  585. $res.='/'.$label.' /'.$value."\n";
  586. break;
  587. }
  588. }
  589. $res.=">>\nendobj";
  590. return $res;
  591. break;
  592. }
  593. }
  594. /**
  595. * the font encoding
  596. */
  597. function o_fontEncoding($id,$action,$options=''){
  598. if ($action!='new'){
  599. $o =& $this->objects[$id];
  600. }
  601. switch ($action){
  602. case 'new':
  603. // the options array should contain 'differences' and maybe 'encoding'
  604. $this->objects[$id]=array('t'=>'fontEncoding','info'=>$options);
  605. break;
  606. case 'out':
  607. $res="\n".$id." 0 obj\n<< /Type /Encoding\n";
  608. if (!isset($o['info']['encoding'])){
  609. $o['info']['encoding']='WinAnsiEncoding';
  610. }
  611. if ($o['info']['encoding']!='none'){
  612. $res.="/BaseEncoding /".$o['info']['encoding']."\n";
  613. }
  614. $res.="/Differences \n[";
  615. $onum=-100;
  616. foreach($o['info']['differences'] as $num=>$label){
  617. if ($num!=$onum+1){
  618. // we cannot make use of consecutive numbering
  619. $res.= "\n".$num." /".$label;
  620. } else {
  621. $res.= " /".$label;
  622. }
  623. $onum=$num;
  624. }
  625. $res.="\n]\n>>\nendobj";
  626. return $res;
  627. break;
  628. }
  629. }
  630. /**
  631. * the document procset, solves some problems with printing to old PS printers
  632. */
  633. function o_procset($id,$action,$options=''){
  634. if ($action!='new'){
  635. $o =& $this->objects[$id];
  636. }
  637. switch ($action){
  638. case 'new':
  639. $this->objects[$id]=array('t'=>'procset','info'=>array('PDF'=>1,'Text'=>1));
  640. $this->o_pages($this->currentNode,'procset',$id);
  641. $this->procsetObjectId=$id;
  642. break;
  643. case 'add':
  644. // this is to add new items to the procset list, despite the fact that this is considered
  645. // obselete, the items are required for printing to some postscript printers
  646. switch ($options) {
  647. case 'ImageB':
  648. case 'ImageC':
  649. case 'ImageI':
  650. $o['info'][$options]=1;
  651. break;
  652. }
  653. break;
  654. case 'out':
  655. $res="\n".$id." 0 obj\n[";
  656. foreach ($o['info'] as $label=>$val){
  657. $res.='/'.$label.' ';
  658. }
  659. $res.="]\nendobj";
  660. return $res;
  661. break;
  662. }
  663. }
  664. /**
  665. * define the document information
  666. */
  667. function o_info($id,$action,$options=''){
  668. if ($action!='new'){
  669. $o =& $this->objects[$id];
  670. }
  671. switch ($action){
  672. case 'new':
  673. $this->infoObject=$id;
  674. $date='D:'.date('Ymd');
  675. $this->objects[$id]=array('t'=>'info','info'=>array('Creator'=>'R and OS php pdf writer, http://www.ros.co.nz','CreationDate'=>$date));
  676. break;
  677. case 'Title':
  678. case 'Author':
  679. case 'Subject':
  680. case 'Keywords':
  681. case 'Creator':
  682. case 'Producer':
  683. case 'CreationDate':
  684. case 'ModDate':
  685. case 'Trapped':
  686. $o['info'][$action]=$options;
  687. break;
  688. case 'out':
  689. if ($this->encrypted){
  690. $this->encryptInit($id);
  691. }
  692. $res="\n".$id." 0 obj\n<<\n";
  693. foreach ($o['info'] as $k=>$v){
  694. $res.='/'.$k.' (';
  695. if ($this->encrypted){
  696. $res.=$this->filterText($this->ARC4($v));
  697. } else {
  698. $res.=$this->filterText($v);
  699. }
  700. $res.=")\n";
  701. }
  702. $res.=">>\nendobj";
  703. return $res;
  704. break;
  705. }
  706. }
  707. /**
  708. * an action object, used to link to URLS initially
  709. */
  710. function o_action($id,$action,$options=''){
  711. if ($action!='new'){
  712. $o =& $this->objects[$id];
  713. }
  714. switch ($action){
  715. case 'new':
  716. if (is_array($options)){
  717. $this->objects[$id]=array('t'=>'action','info'=>$options,'type'=>$options['type']);
  718. } else {
  719. // then assume a URI action
  720. $this->objects[$id]=array('t'=>'action','info'=>$options,'type'=>'URI');
  721. }
  722. break;
  723. case 'out':
  724. if ($this->encrypted){
  725. $this->encryptInit($id);
  726. }
  727. $res="\n".$id." 0 obj\n<< /Type /Action";
  728. switch($o['type']){
  729. case 'ilink':
  730. // there will be an 'label' setting, this is the name of the destination
  731. $res.="\n/S /GoTo\n/D ".$this->destinations[(string)$o['info']['label']]." 0 R";
  732. break;
  733. case 'URI':
  734. $res.="\n/S /URI\n/URI (";
  735. if ($this->encrypted){
  736. $res.=$this->filterText($this->ARC4($o['info']));
  737. } else {
  738. $res.=$this->filterText($o['info']);
  739. }
  740. $res.=")";
  741. break;
  742. }
  743. $res.="\n>>\nendobj";
  744. return $res;
  745. break;
  746. }
  747. }
  748. /**
  749. * an annotation object, this will add an annotation to the current page.
  750. * initially will support just link annotations
  751. */
  752. function o_annotation($id,$action,$options=''){
  753. if ($action!='new'){
  754. $o =& $this->objects[$id];
  755. }
  756. switch ($action){
  757. case 'new':
  758. // add the annotation to the current page
  759. $pageId = $this->currentPage;
  760. $this->o_page($pageId,'annot',$id);
  761. // and add the action object which is going to be required
  762. switch($options['type']){
  763. case 'link':
  764. $this->objects[$id]=array('t'=>'annotation','info'=>$options);
  765. $this->numObj++;
  766. $this->o_action($this->numObj,'new',$options['url']);
  767. $this->objects[$id]['info']['actionId']=$this->numObj;
  768. break;
  769. case 'ilink':
  770. // this is to a named internal link
  771. $label = $options['label'];
  772. $this->objects[$id]=array('t'=>'annotation','info'=>$options);
  773. $this->numObj++;
  774. $this->o_action($this->numObj,'new',array('type'=>'ilink','label'=>$label));
  775. $this->objects[$id]['info']['actionId']=$this->numObj;
  776. break;
  777. }
  778. break;
  779. case 'out':
  780. $res="\n".$id." 0 obj\n<< /Type /Annot";
  781. switch($o['info']['type']){
  782. case 'link':
  783. case 'ilink':
  784. $res.= "\n/Subtype /Link";
  785. break;
  786. }
  787. $res.="\n/A ".$o['info']['actionId']." 0 R";
  788. $res.="\n/Border [0 0 0]";
  789. $res.="\n/H /I";
  790. $res.="\n/Rect [ ";
  791. foreach($o['info']['rect'] as $v){
  792. $res.= sprintf("%.4f ",$v);
  793. }
  794. $res.="]";
  795. $res.="\n>>\nendobj";
  796. return $res;
  797. break;
  798. }
  799. }
  800. /**
  801. * a page object, it also creates a contents object to hold its contents
  802. */
  803. function o_page($id,$action,$options=''){
  804. if ($action!='new'){
  805. $o =& $this->objects[$id];
  806. }
  807. switch ($action){
  808. case 'new':
  809. $this->numPages++;
  810. $this->objects[$id]=array('t'=>'page','info'=>array('parent'=>$this->currentNode,'pageNum'=>$this->numPages));
  811. if (is_array($options)){
  812. // then this must be a page insertion, array shoudl contain 'rid','pos'=[before|after]
  813. $options['id']=$id;
  814. $this->o_pages($this->currentNode,'page',$options);
  815. } else {
  816. $this->o_pages($this->currentNode,'page',$id);
  817. }
  818. $this->currentPage=$id;
  819. //make a contents object to go with this page
  820. $this->numObj++;
  821. $this->o_contents($this->numObj,'new',$id);
  822. $this->currentContents=$this->numObj;
  823. $this->objects[$id]['info']['contents']=array();
  824. $this->objects[$id]['info']['contents'][]=$this->numObj;
  825. $match = ($this->numPages%2 ? 'odd' : 'even');
  826. foreach($this->addLooseObjects as $oId=>$target){
  827. if ($target=='all' || $match==$target){
  828. $this->objects[$id]['info']['contents'][]=$oId;
  829. }
  830. }
  831. break;
  832. case 'content':
  833. $o['info']['contents'][]=$options;
  834. break;
  835. case 'annot':
  836. // add an annotation to this page
  837. if (!isset($o['info']['annot'])){
  838. $o['info']['annot']=array();
  839. }
  840. // $options should contain the id of the annotation dictionary
  841. $o['info']['annot'][]=$options;
  842. break;
  843. case 'out':
  844. $res="\n".$id." 0 obj\n<< /Type /Page";
  845. $res.="\n/Parent ".$o['info']['parent']." 0 R";
  846. if (isset($o['info']['annot'])){
  847. $res.="\n/Annots [";
  848. foreach($o['info']['annot'] as $aId){
  849. $res.=" ".$aId." 0 R";
  850. }
  851. $res.=" ]";
  852. }
  853. $count = count($o['info']['contents']);
  854. if ($count==1){
  855. $res.="\n/Contents ".$o['info']['contents'][0]." 0 R";
  856. } else if ($count>1){
  857. $res.="\n/Contents [\n";
  858. foreach ($o['info']['contents'] as $cId){
  859. $res.=$cId." 0 R\n";
  860. }
  861. $res.="]";
  862. }
  863. $res.="\n>>\nendobj";
  864. return $res;
  865. break;
  866. }
  867. }
  868. /**
  869. * the contents objects hold all of the content which appears on pages
  870. */
  871. function o_contents($id,$action,$options=''){
  872. if ($action!='new'){
  873. $o =& $this->objects[$id];
  874. }
  875. switch ($action){
  876. case 'new':
  877. $this->objects[$id]=array('t'=>'contents','c'=>'','info'=>array());
  878. if (strlen($options) && intval($options)){
  879. // then this contents is the primary for a page
  880. $this->objects[$id]['onPage']=$options;
  881. } else if ($options=='raw'){
  882. // then this page contains some other type of system object
  883. $this->objects[$id]['raw']=1;
  884. }
  885. break;
  886. case 'add':
  887. // add more options to the decleration
  888. foreach ($options as $k=>$v){
  889. $o['info'][$k]=$v;
  890. }
  891. case 'out':
  892. $tmp=$o['c'];
  893. $res= "\n".$id." 0 obj\n";
  894. if (isset($this->objects[$id]['raw'])){
  895. $res.=$tmp;
  896. } else {
  897. $res.= "<<";
  898. if (function_exists('gzcompress') && $this->options['compression']){
  899. // then implement ZLIB based compression on this content stream
  900. $res.=" /Filter /FlateDecode";
  901. $tmp = gzcompress($tmp);
  902. }
  903. if ($this->encrypted){
  904. $this->encryptInit($id);
  905. $tmp = $this->ARC4($tmp);
  906. }
  907. foreach($o['info'] as $k=>$v){
  908. $res .= "\n/".$k.' '.$v;
  909. }
  910. $res.="\n/Length ".strlen($tmp)." >>\nstream\n".$tmp."\nendstream";
  911. }
  912. $res.="\nendobj\n";
  913. return $res;
  914. break;
  915. }
  916. }
  917. /**
  918. * an image object, will be an XObject in the document, includes description and data
  919. */
  920. function o_image($id,$action,$options=''){
  921. if ($action!='new'){
  922. $o =& $this->objects[$id];
  923. }
  924. switch($action){
  925. case 'new':
  926. // make the new object
  927. $this->objects[$id]=array('t'=>'image','data'=>$options['data'],'info'=>array());
  928. $this->objects[$id]['info']['Type']='/XObject';
  929. $this->objects[$id]['info']['Subtype']='/Image';
  930. $this->objects[$id]['info']['Width']=$options['iw'];
  931. $this->objects[$id]['info']['Height']=$options['ih'];
  932. if (!isset($options['type']) || $options['type']=='jpg'){
  933. if (!isset($options['channels'])){
  934. $options['channels']=3;
  935. }
  936. switch($options['channels']){
  937. case 1:
  938. $this->objects[$id]['info']['ColorSpace']='/DeviceGray';
  939. break;
  940. default:
  941. $this->objects[$id]['info']['ColorSpace']='/DeviceRGB';
  942. break;
  943. }
  944. $this->objects[$id]['info']['Filter']='/DCTDecode';
  945. $this->objects[$id]['info']['BitsPerComponent']=8;
  946. } else if ($options['type']=='png'){
  947. $this->objects[$id]['info']['Filter']='/FlateDecode';
  948. $this->objects[$id]['info']['DecodeParms']='<< /Predictor 15 /Colors '.$options['ncolor'].' /Columns '.$options['iw'].' /BitsPerComponent '.$options['bitsPerComponent'].'>>';
  949. if (strlen($options['pdata'])){
  950. $tmp = ' [ /Indexed /DeviceRGB '.(strlen($options['pdata'])/3-1).' ';
  951. $this->numObj++;
  952. $this->o_contents($this->numObj,'new');
  953. $this->objects[$this->numObj]['c']=$options['pdata'];
  954. $tmp.=$this->numObj.' 0 R';
  955. $tmp .=' ]';
  956. $this->objects[$id]['info']['ColorSpace'] = $tmp;
  957. if (isset($options['transparency'])){
  958. switch($options['transparency']['type']){
  959. case 'indexed':
  960. $tmp=' [ '.$options['transparency']['data'].' '.$options['transparency']['data'].'] ';
  961. $this->objects[$id]['info']['Mask'] = $tmp;
  962. break;
  963. }
  964. }
  965. } else {
  966. $this->objects[$id]['info']['ColorSpace']='/'.$options['color'];
  967. }
  968. $this->objects[$id]['info']['BitsPerComponent']=$options['bitsPerComponent'];
  969. }
  970. // assign it a place in the named resource dictionary as an external object, according to
  971. // the label passed in with it.
  972. $this->o_pages($this->currentNode,'xObject',array('label'=>$options['label'],'objNum'=>$id));
  973. // also make sure that we have the right procset object for it.
  974. $this->o_procset($this->procsetObjectId,'add','ImageC');
  975. break;
  976. case 'out':
  977. $tmp=$o['data'];
  978. $res= "\n".$id." 0 obj\n<<";
  979. foreach($o['info'] as $k=>$v){
  980. $res.="\n/".$k.' '.$v;
  981. }
  982. if ($this->encrypted){
  983. $this->encryptInit($id);
  984. $tmp = $this->ARC4($tmp);
  985. }
  986. $res.="\n/Length ".strlen($tmp)." >>\nstream\n".$tmp."\nendstream\nendobj\n";
  987. return $res;
  988. break;
  989. }
  990. }
  991. /**
  992. * encryption object.
  993. */
  994. function o_encryption($id,$action,$options=''){
  995. if ($action!='new'){
  996. $o =& $this->objects[$id];
  997. }
  998. switch($action){
  999. case 'new':
  1000. // make the new object
  1001. $this->objects[$id]=array('t'=>'encryption','info'=>$options);
  1002. $this->arc4_objnum=$id;
  1003. // figure out the additional paramaters required
  1004. $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);
  1005. $len = strlen($options['owner']);
  1006. if ($len>32){
  1007. $owner = substr($options['owner'],0,32);
  1008. } else if ($len<32){
  1009. $owner = $options['owner'].substr($pad,0,32-$len);
  1010. } else {
  1011. $owner = $options['owner'];
  1012. }
  1013. $len = strlen($options['user']);
  1014. if ($len>32){
  1015. $user = substr($options['user'],0,32);
  1016. } else if ($len<32){
  1017. $user = $options['user'].substr($pad,0,32-$len);
  1018. } else {
  1019. $user = $options['user'];
  1020. }
  1021. $tmp = $this->md5_16($owner);
  1022. $okey = substr($tmp,0,5);
  1023. $this->ARC4_init($okey);
  1024. $ovalue=$this->ARC4($user);
  1025. $this->objects[$id]['info']['O']=$ovalue;
  1026. // now make the u value, phew.
  1027. $tmp = $this->md5_16($user.$ovalue.chr($options['p']).chr(255).chr(255).chr(255).$this->fileIdentifier);
  1028. $ukey = substr($tmp,0,5);
  1029. $this->ARC4_init($ukey);
  1030. $this->encryptionKey = $ukey;
  1031. $this->encrypted=1;
  1032. $uvalue=$this->ARC4($pad);
  1033. $this->objects[$id]['info']['U']=$uvalue;
  1034. $this->encryptionKey=$ukey;
  1035. // initialize the arc4 array
  1036. break;
  1037. case 'out':
  1038. $res= "\n".$id." 0 obj\n<<";
  1039. $res.="\n/Filter /Standard";
  1040. $res.="\n/V 1";
  1041. $res.="\n/R 2";
  1042. $res.="\n/O (".$this->filterText($o['info']['O']).')';
  1043. $res.="\n/U (".$this->filterText($o['info']['U']).')';
  1044. // and the p-value needs to be converted to account for the twos-complement approach
  1045. $o['info']['p'] = (($o['info']['p']^255)+1)*-1;
  1046. $res.="\n/P ".($o['info']['p']);
  1047. $res.="\n>>\nendobj\n";
  1048. return $res;
  1049. break;
  1050. }
  1051. }
  1052. /**
  1053. * ARC4 functions
  1054. * A series of function to implement ARC4 encoding in PHP
  1055. */
  1056. /**
  1057. * calculate the 16 byte version of the 128 bit md5 digest of the string
  1058. */
  1059. function md5_16($string){
  1060. $tmp = md5($string);
  1061. $out='';
  1062. for ($i=0;$i<=30;$i=$i+2){
  1063. $out.=chr(hexdec(substr($tmp,$i,2)));
  1064. }
  1065. return $out;
  1066. }
  1067. /**
  1068. * initialize the encryption for processing a particular object
  1069. */
  1070. function encryptInit($id){
  1071. $tmp = $this->encryptionKey;
  1072. $hex = dechex($id);
  1073. if (strlen($hex)<6){
  1074. $hex = substr('000000',0,6-strlen($hex)).$hex;
  1075. }
  1076. $tmp.= chr(hexdec(substr($hex,4,2))).chr(hexdec(substr($hex,2,2))).chr(hexdec(substr($hex,0,2))).chr(0).chr(0);
  1077. $key = $this->md5_16($tmp);
  1078. $this->ARC4_init(substr($key,0,10));
  1079. }
  1080. /**
  1081. * initialize the ARC4 encryption
  1082. */
  1083. function ARC4_init($key=''){
  1084. $this->arc4 = '';
  1085. // setup the control array
  1086. if (strlen($key)==0){
  1087. return;
  1088. }
  1089. $k = '';
  1090. while(strlen($k)<256){
  1091. $k.=$key;
  1092. }
  1093. $k=substr($k,0,256);
  1094. for ($i=0;$i<256;$i++){
  1095. $this->arc4 .= chr($i);
  1096. }
  1097. $j=0;
  1098. for ($i=0;$i<256;$i++){
  1099. $t = $this->arc4[$i];
  1100. $j = ($j + ord($t) + ord($k[$i]))%256;
  1101. $this->arc4[$i]=$this->arc4[$j];
  1102. $this->arc4[$j]=$t;
  1103. }
  1104. }
  1105. /**
  1106. * ARC4 encrypt a text string
  1107. */
  1108. function ARC4($text){
  1109. $len=strlen($text);
  1110. $a=0;
  1111. $b=0;
  1112. $c = $this->arc4;
  1113. $out='';
  1114. for ($i=0;$i<$len;$i++){
  1115. $a = ($a+1)%256;
  1116. $t= $c[$a];
  1117. $b = ($b+ord($t))%256;
  1118. $c[$a]=$c[$b];
  1119. $c[$b]=$t;
  1120. $k = ord($c[(ord($c[$a])+ord($c[$b]))%256]);
  1121. $out.=chr(ord($text[$i]) ^ $k);
  1122. }
  1123. return $out;
  1124. }
  1125. /**
  1126. * functions which can be called to adjust or add to the document
  1127. */
  1128. /**
  1129. * add a link in the document to an external URL
  1130. */
  1131. function addLink($url,$x0,$y0,$x1,$y1){
  1132. $this->numObj++;
  1133. $info = array('type'=>'link','url'=>$url,'rect'=>array($x0,$y0,$x1,$y1));
  1134. $this->o_annotation($this->numObj,'new',$info);
  1135. }
  1136. /**
  1137. * add a link in the document to an internal destination (ie. within the document)
  1138. */
  1139. function addInternalLink($label,$x0,$y0,$x1,$y1){
  1140. $this->numObj++;
  1141. $info = array('type'=>'ilink','label'=>$label,'rect'=>array($x0,$y0,$x1,$y1));
  1142. $this->o_annotation($this->numObj,'new',$info);
  1143. }
  1144. /**
  1145. * set the encryption of the document
  1146. * can be used to turn it on and/or set the passwords which it will have.
  1147. * also the functions that the user will have are set here, such as print, modify, add
  1148. */
  1149. function setEncryption($userPass='',$ownerPass='',$pc=array()){
  1150. $p=bindec(11000000);
  1151. $options = array(
  1152. 'print'=>4
  1153. ,'modify'=>8
  1154. ,'copy'=>16
  1155. ,'add'=>32
  1156. );
  1157. foreach($pc as $k=>$v){
  1158. if ($v && isset($options[$k])){
  1159. $p+=$options[$k];
  1160. } else if (isset($options[$v])){
  1161. $p+=$options[$v];
  1162. }
  1163. }
  1164. // implement encryption on the document
  1165. if ($this->arc4_objnum == 0){
  1166. // then the block does not exist already, add it.
  1167. $this->numObj++;
  1168. if (strlen($ownerPass)==0){
  1169. $ownerPass=$userPass;
  1170. }
  1171. $this->o_encryption($this->numObj,'new',array('user'=>$userPass,'owner'=>$ownerPass,'p'=>$p));
  1172. }
  1173. }
  1174. /**
  1175. * should be used for internal checks, not implemented as yet
  1176. */
  1177. function checkAllHere(){
  1178. }
  1179. /**
  1180. * return the pdf stream as a string returned from the function
  1181. */
  1182. function output($debug=0){
  1183. if ($debug){
  1184. // turn compression off
  1185. $this->options['compression']=0;
  1186. }
  1187. if ($this->arc4_objnum){
  1188. $this->ARC4_init($this->encryptionKey);
  1189. }
  1190. $this->checkAllHere();
  1191. $xref=array();
  1192. $content="%PDF-1.3\n%��\n";
  1193. // $content="%PDF-1.3\n";
  1194. $pos=strlen($content);
  1195. foreach($this->objects as $k=>$v){
  1196. $tmp='o_'.$v['t'];
  1197. $cont=$this->$tmp($k,'out');
  1198. $content.=$cont;
  1199. $xref[]=$pos;
  1200. $pos+=strlen($cont);
  1201. }
  1202. $content.="\nxref\n0 ".(count($xref)+1)."\n0000000000 65535 f \n";
  1203. foreach($xref as $p){
  1204. $content.=substr('0000000000',0,10-strlen($p)).$p." 00000 n \n";
  1205. }
  1206. $content.="\ntrailer\n << /Size ".(count($xref)+1)."\n /Root 1 0 R\n /Info ".$this->infoObject." 0 R\n";
  1207. // if encryption has been applied to this document then add the marker for this dictionary
  1208. if ($this->arc4_objnum > 0){
  1209. $content .= "/Encrypt ".$this->arc4_objnum." 0 R\n";
  1210. }
  1211. if (strlen($this->fileIdentifier)){
  1212. $content .= "/ID[<".$this->fileIdentifier."><".$this->fileIdentifier.">]\n";
  1213. }
  1214. $content .= " >>\nstartxref\n".$pos."\n%%EOF\n";
  1215. return $content;
  1216. }
  1217. /**
  1218. * intialize a new document
  1219. * if this is called on an existing document results may be unpredictable, but the existing document would be lost at minimum
  1220. * this function is called automatically by the constructor function
  1221. *
  1222. * @access private
  1223. */
  1224. function newDocument($pageSize=array(0,0,612,792)){
  1225. $this->numObj=0;
  1226. $this->objects = array();
  1227. $this->numObj++;
  1228. $this->o_catalog($this->numObj,'new');
  1229. $this->numObj++;
  1230. $this->o_outlines($this->numObj,'new');
  1231. $this->numObj++;
  1232. $this->o_pages($this->numObj,'new');
  1233. $this->o_pages($this->numObj,'mediaBox',$pageSize);
  1234. $this->currentNode = 3;
  1235. $this->numObj++;
  1236. $this->o_procset($this->numObj,'new');
  1237. $this->numObj++;
  1238. $this->o_info($this->numObj,'new');
  1239. $this->numObj++;
  1240. $this->o_page($this->numObj,'new');
  1241. // need to store the first page id as there is no way to get it to the user during
  1242. // startup
  1243. $this->firstPageId = $this->currentContents;
  1244. }
  1245. /**
  1246. * open the font file and return a php structure containing it.
  1247. * first check if this one has been done before and saved in a form more suited to php
  1248. * note that if a php serialized version does not exist it will try and make one, but will
  1249. * require write access to the directory to do it... it is MUCH faster to have these serialized
  1250. * files.
  1251. *
  1252. * @access private
  1253. */
  1254. function openFont($font){
  1255. // assume that $font contains both the path and perhaps the extension to the file, split them
  1256. $pos=strrpos($font,'/');
  1257. if ($pos===false){
  1258. $dir = './';
  1259. $name = $font;
  1260. } else {
  1261. $dir=substr($font,0,$pos+1);
  1262. $name=substr($font,$pos+1);
  1263. }
  1264. if (substr($name,-4)=='.afm'){
  1265. $name=substr($name,0,strlen($name)-4);
  1266. }
  1267. $this->addMessage('openFont: '.$font.' - '.$name);
  1268. if (file_exists($dir.'php_'.$name.'.afm')){
  1269. $this->addMessage('openFont: php file exists '.$dir.'php_'.$name.'.afm');
  1270. $tmp = file($dir.'php_'.$name.'.afm');
  1271. $this->fonts[$font]=unserialize($tmp[0]);
  1272. if (!isset($this->fonts[$font]['_version_']) || $this->fonts[$font]['_version_']<1){
  1273. // if the font file is old, then clear it out and prepare for re-creation
  1274. $this->addMessage('openFont: clear out, make way for new version.');
  1275. unset($this->fonts[$font]);
  1276. }
  1277. }
  1278. if (!isset($this->fonts[$font]) && file_exists($dir.$name.'.afm')){
  1279. // then rebuild the php_<font>.afm file from the <font>.afm file
  1280. $this->addMessage('openFont: build php file from '.$dir.$name.'.afm');
  1281. $data = array();
  1282. $file = file($dir.$name.'.afm');
  1283. foreach ($file as $rowA){
  1284. $row=trim($rowA);
  1285. $pos=strpos($row,' ');
  1286. if ($pos){
  1287. // then there must be some keyword
  1288. $key = substr($row,0,$pos);
  1289. switch ($key){
  1290. case 'FontName':
  1291. case 'FullName':
  1292. case 'FamilyName':
  1293. case 'Weight':
  1294. case 'ItalicAngle':
  1295. case 'IsFixedPitch':
  1296. case 'CharacterSet':
  1297. case 'UnderlinePosition':
  1298. case 'UnderlineThickness':
  1299. case 'Version':
  1300. case 'EncodingScheme':
  1301. case 'CapHeight':
  1302. case 'XHeight':
  1303. case 'Ascender':
  1304. case 'Descender':
  1305. case 'StdHW':
  1306. case 'StdVW':
  1307. case 'StartCharMetrics':
  1308. $data[$key]=trim(substr($row,$pos));
  1309. break;
  1310. case 'FontBBox':
  1311. $data[$key]=explode(' ',trim(substr($row,$pos)));
  1312. break;
  1313. case 'C':
  1314. //C 39 ; WX 222 ; N quoteright ; B 53 463 157 718 ;
  1315. $bits=explode(';',trim($row));
  1316. $dtmp=array();
  1317. foreach($bits as $bit){
  1318. $bits2 = explode(' ',trim($bit));
  1319. if (strlen($bits2[0])){
  1320. if (count($bits2)>2){
  1321. $dtmp[$bits2[0]]=array();
  1322. for ($i=1;$i<count($bits2);$i++){
  1323. $dtmp[$bits2[0]][]=$bits2[$i];
  1324. }
  1325. } else if (count($bits2)==2){
  1326. $dtmp[$bits2[0]]=$bits2[1];
  1327. }
  1328. }
  1329. }
  1330. if ($dtmp['C']>=0){
  1331. $data['C'][$dtmp['C']]=$dtmp;
  1332. $data['C'][$dtmp['N']]=$dtmp;
  1333. } else {
  1334. $data['C'][$dtmp['N']]=$dtmp;
  1335. }
  1336. break;
  1337. case 'KPX':
  1338. //KPX Adieresis yacute -40
  1339. $bits=explode(' ',trim($row));
  1340. $data['KPX'][$bits[1]][$bits[2]]=$bits[3];
  1341. break;
  1342. }
  1343. }
  1344. }
  1345. $data['_version_']=1;
  1346. $this->fonts[$font]=$data;
  1347. touch($dir.'php_'.$name.'.afm'); // php bug
  1348. $fp = fopen($dir.'php_'.$name.'.afm','w');
  1349. fwrite($fp,serialize($data));
  1350. fclose($fp);
  1351. } else if (!isset($this->fonts[$font])){
  1352. $this->addMessage('openFont: no font file found');
  1353. // echo 'Font not Found '.$font;
  1354. }
  1355. }
  1356. /**
  1357. * if the font is not loaded then load it and make the required object
  1358. * else just make it the current font
  1359. * the encoding array can contain 'encoding'=> 'none','WinAnsiEncoding','MacRomanEncoding' or 'MacExpertEncoding'
  1360. * note that encoding='none' will need to be used for symbolic fonts
  1361. * and 'differences' => an array of mappings between numbers 0->255 and character names.
  1362. *
  1363. */
  1364. function selectFont($fontName,$encoding='',$set=1){
  1365. if (!isset($this->fonts[$fontName])){
  1366. // load the file
  1367. $this->openFont($fontName);
  1368. if (isset($this->fonts[$fontName])){
  1369. $this->numObj++;
  1370. $this->numFonts++;
  1371. $pos=strrpos($fontName,'/');
  1372. // $dir=substr($fontName,0,$pos+1);
  1373. $name=substr($fontName,$pos+1);
  1374. if (substr($name,-4)=='.afm'){
  1375. $name=substr($name,0,strlen($name)-4);
  1376. }
  1377. $options=array('name'=>$name);
  1378. if (is_array($encoding)){
  1379. // then encoding and differences might be set
  1380. if (isset($encoding['encoding'])){
  1381. $options['encoding']=$encoding['encoding'];
  1382. }
  1383. if (isset($encoding['differences'])){
  1384. $options['differences']=$encoding['differences'];
  1385. }
  1386. } else if (strlen($encoding)){
  1387. // then perhaps only the encoding has been set
  1388. $options['encoding']=$encoding;
  1389. }
  1390. $fontObj = $this->numObj;
  1391. $this->o_font($this->numObj,'new',$options);
  1392. $this->fonts[$fontName]['fontNum']=$this->numFonts;
  1393. // if this is a '.afm' font, and there is a '.pfa' file to go with it ( as there
  1394. // should be for all non-basic fonts), then load it into an object and put the
  1395. // references into the font object
  1396. $basefile = substr($fontName,0,strlen($fontName)-4);
  1397. if (file_exists($basefile.'.pfb')){
  1398. $fbtype = 'pfb';
  1399. } else if (file_exists($basefile.'.ttf')){
  1400. $fbtype = 'ttf';
  1401. } else {
  1402. $fbtype='';
  1403. }
  1404. $fbfile = $basefile.'.'.$fbtype;
  1405. // $pfbfile = substr($fontName,0,strlen($fontName)-4).'.pfb';
  1406. // $ttffile = substr($fontName,0,strlen($fontName)-4).'.ttf';
  1407. $this->addMessage('selectFont: checking for - '.$fbfile);
  1408. if (substr($fontName,-4)=='.afm' && strlen($fbtype) ){
  1409. $adobeFontName = $this->fonts[$fontName]['FontName'];
  1410. // $fontObj = $this->numObj;
  1411. $this->addMessage('selectFont: adding font file - '.$fbfile.' - '.$adobeFontName);
  1412. // find the array of fond widths, and put that into an object.
  1413. $firstChar = -1;
  1414. $lastChar = 0;
  1415. $widths = array();
  1416. foreach ($this->fonts[$fontName]['C'] as $num=>$d){
  1417. if (intval($num)>0 || $num=='0'){
  1418. if ($lastChar>0 && $num>$lastChar+1){
  1419. for($i=$lastChar+1;$i<$num;$i++){
  1420. $widths[] = 0;
  1421. }
  1422. }
  1423. $widths[] = $d['WX'];
  1424. if ($firstChar==-1){
  1425. $firstChar = $num;
  1426. }
  1427. $lastChar = $num;
  1428. }
  1429. }
  1430. // also need to adjust the widths for the differences array
  1431. if (isset($options['differences'])){
  1432. foreach($options['differences'] as $charNum=>$charName){
  1433. if ($charNum>$lastChar){
  1434. for($i=$lastChar+1;$i<=$charNum;$i++){
  1435. $widths[]=0;
  1436. }
  1437. $lastChar=$charNum;
  1438. }
  1439. if (isset($this->fonts[$fontName]['C'][$charName])){
  1440. $widths[$charNum-$firstChar]=$this->fonts[$fontName]['C'][$charName]['WX'];
  1441. }
  1442. }
  1443. }
  1444. $this->addMessage('selectFont: FirstChar='.$firstChar);
  1445. $this->addMessage('selectFont: LastChar='.$lastChar);
  1446. $this->numObj++;
  1447. $this->o_contents($this->numObj,'new','raw');
  1448. $this->objects[$this->numObj]['c'].='[';
  1449. foreach($widths as $width){
  1450. $this->objects[$this->numObj]['c'].=' '.$width;
  1451. }
  1452. $this->objects[$this->numObj]['c'].=' ]';
  1453. $widthid = $this->numObj;
  1454. // load the pfb file, and put that into an object too.
  1455. // note that pdf supports only binary format type 1 font files, though there is a
  1456. // simple utility to convert them from pfa to pfb.
  1457. $fp = fopen($fbfile,'rb');
  1458. $tmp = get_magic_quotes_runtime();
  1459. set_magic_quotes_runtime(0);
  1460. $data = fread($fp,filesize($fbfile));
  1461. set_magic_quotes_runtime($tmp);
  1462. fclose($fp);
  1463. // create the font descriptor
  1464. $this->numObj++;
  1465. $fontDescriptorId = $this->numObj;
  1466. $this->numObj++;
  1467. $pfbid = $this->numObj;
  1468. // determine flags (more than a little flakey, hopefully will not matter much)
  1469. $flags=0;
  1470. if ($this->fonts[$fontName]['ItalicAngle']!=0){ $flags+=pow(2,6); }
  1471. if ($this->fonts[$fontName]['IsFixedPitch']=='true'){ $flags+=1; }
  1472. $flags+=pow(2,5); // assume non-sybolic
  1473. $list = array('Ascent'=>'Ascender','CapHeight'=>'CapHeight','Descent'=>'Descender','FontBBox'=>'FontBBox','ItalicAngle'=>'ItalicAngle');
  1474. $fdopt = array(
  1475. 'Flags'=>$flags
  1476. ,'FontName'=>$adobeFontName
  1477. ,'StemV'=>100 // don't know what the value for this should be!
  1478. );
  1479. foreach($list as $k=>$v){
  1480. if (isset($this->fonts[$fontName][$v])){
  1481. $fdopt[$k]=$this->fonts[$fontName][$v];
  1482. }
  1483. }
  1484. if ($fbtype=='pfb'){
  1485. $fdopt['FontFile']=$pfbid;
  1486. } else if ($fbtype=='ttf'){
  1487. $fdopt['FontFile2']=$pfbid;
  1488. }
  1489. $this->o_fontDescriptor($fontDescriptorId,'new',$fdopt);
  1490. // embed the font program
  1491. $this->o_contents($this->numObj,'new');
  1492. $this->objects[$pfbid]['c'].=$data;
  1493. // determine the cruicial lengths within this file
  1494. if ($fbtype=='pfb'){
  1495. $l1 = strpos($data,'eexec')+6;
  1496. $l2 = strpos($data,'00000000')-$l1;
  1497. $l3 = strlen($data)-$l2-$l1;
  1498. $this->o_contents($this->numObj,'add',array('Length1'=>$l1,'Length2'=>$l2,'Length3'=>$l3));
  1499. } else if ($fbtype=='ttf'){
  1500. $l1 = strlen($data);
  1501. $this->o_contents($this->numObj,'add',array('Length1'=>$l1));
  1502. }
  1503. // tell the font object about all this new stuff
  1504. $tmp = array('BaseFont'=>$adobeFontName,'Widths'=>$widthid
  1505. ,'FirstChar'=>$firstChar,'LastChar'=>$lastChar
  1506. ,'FontDescriptor'=>$fontDescriptorId);
  1507. if ($fbtype=='ttf'){
  1508. $tmp['SubType']='TrueType';
  1509. }
  1510. $this->addMessage('adding extra info to font.('.$fontObj.')');
  1511. foreach($tmp as $fk=>$fv){
  1512. $this->addMessage($fk." : ".$fv);
  1513. }
  1514. $this->o_font($fontObj,'add',$tmp);
  1515. } else {
  1516. $this->addMessage('selectFont: pfb or ttf file not found, ok if this is one of the 14 standard fonts');
  1517. }
  1518. // also set the differences here, note that this means that these will take effect only the
  1519. //first time that a font is selected, else they are ignored
  1520. if (isset($options['differences'])){
  1521. $this->fonts[$fontName]['differences']=$options['differences'];
  1522. }
  1523. }
  1524. }
  1525. if ($set && isset($this->fonts[$fontName])){
  1526. // so if for some reason the font was not set in the last one then it will not be selected
  1527. $this->currentBaseFont=$fontName;
  1528. // the next line means that if a new font is selected, then the current text state will be
  1529. // applied to it as well.
  1530. $this->setCurrentFont();
  1531. }
  1532. return $this->currentFontNum;
  1533. }
  1534. /**
  1535. * sets up the current font, based on the font families, and the current text state
  1536. * note that this system is quite flexible, a <b><i> font can be completely different to a
  1537. * <i><b> font, and even <b><b> will have to be defined within the family to have meaning
  1538. * This function is to be called whenever the currentTextState is changed, it will update
  1539. * the currentFont setting to whatever the appropriatte family one is.
  1540. * If the user calls selectFont themselves then that will reset the currentBaseFont, and the currentFont
  1541. * This function will change the currentFont to whatever it should be, but will not change the
  1542. * currentBaseFont.
  1543. *
  1544. * @access private
  1545. */
  1546. function setCurrentFont(){
  1547. if (strlen($this->currentBaseFont)==0){
  1548. // then assume an initial font
  1549. $this->selectFont('./fonts/Helvetica.afm');
  1550. }
  1551. $cf = substr($this->currentBaseFont,strrpos($this->currentBaseFont,'/')+1);
  1552. if (strlen($this->currentTextState)
  1553. && isset($this->fontFamilies[$cf])
  1554. && isset($this->fontFamilies[$cf][$this->currentTextState])){
  1555. // then we are in some state or another
  1556. // and this font has a family, and the current setting exists within it
  1557. // select the font, then return it
  1558. $nf = substr($this->currentBaseFont,0,strrpos($this->currentBaseFont,'/')+1).$this->fontFamilies[$cf][$this->currentTextState];
  1559. $this->selectFont($nf,'',0);
  1560. $this->currentFont = $nf;
  1561. $this->currentFontNum = $this->fonts[$nf]['fontNum'];
  1562. } else {
  1563. // the this font must not have the right family member for the current state
  1564. // simply assume the base font
  1565. $this->currentFont = $this->currentBaseFont;
  1566. $this->currentFontNum = $this->fonts[$this->currentFont]['fontNum'];
  1567. }
  1568. }
  1569. /**
  1570. * function for the user to find out what the ID is of the first page that was created during
  1571. * startup - useful if they wish to add something to it later.
  1572. */
  1573. function getFirstPageId(){
  1574. return $this->firstPageId;
  1575. }
  1576. /**
  1577. * add content to the currently active object
  1578. *
  1579. * @access private
  1580. */
  1581. function addContent($content){
  1582. $this->objects[$this->currentContents]['c'].=$content;
  1583. }
  1584. /**
  1585. * sets the colour for fill operations
  1586. */
  1587. function setColor($r,$g,$b,$force=0){
  1588. if ($r>=0 && ($force || $r!=$this->currentColour['r'] || $g!=$this->currentColour['g'] || $b!=$this->currentColour['b'])){
  1589. $this->objects[$this->currentContents]['c'].="\n".sprintf('%.3f',$r).' '.sprintf('%.3f',$g).' '.sprintf('%.3f',$b).' rg';
  1590. $this->currentColour=array('r'=>$r,'g'=>$g,'b'=>$b);
  1591. }
  1592. }
  1593. /**
  1594. * sets the colour for stroke operations
  1595. */
  1596. function setStrokeColor($r,$g,$b,$force=0){
  1597. if ($r>=0 && ($force || $r!=$this->currentStrokeColour['r'] || $g!=$this->currentStrokeColour['g'] || $b!=$this->currentStrokeColour['b'])){
  1598. $this->objects[$this->currentContents]['c'].="\n".sprintf('%.3f',$r).' '.sprintf('%.3f',$g).' '.sprintf('%.3f',$b).' RG';
  1599. $this->currentStrokeColour=array('r'=>$r,'g'=>$g,'b'=>$b);
  1600. }
  1601. }
  1602. /**
  1603. * draw a line from one set of coordinates to another
  1604. */
  1605. function line($x1,$y1,$x2,$y2){
  1606. $this->objects[$this->currentContents]['c'].="\n".sprintf('%.3f',$x1).' '.sprintf('%.3f',$y1).' m '.sprintf('%.3f',$x2).' '.sprintf('%.3f',$y2).' l S';
  1607. }
  1608. /**
  1609. * draw a bezier curve based on 4 control points
  1610. */
  1611. function curve($x0,$y0,$x1,$y1,$x2,$y2,$x3,$y3){
  1612. // in the current line style, draw a bezier curve from (x0,y0) to (x3,y3) using the other two points
  1613. // as the control points for the curve.
  1614. $this->objects[$this->currentContents]['c'].="\n".sprintf('%.3f',$x0).' '.sprintf('%.3f',$y0).' m '.sprintf('%.3f',$x1).' '.sprintf('%.3f',$y1);
  1615. $this->objects[$this->currentContents]['c'].= ' '.sprintf('%.3f',$x2).' '.sprintf('%.3f',$y2).' '.sprintf('%.3f',$x3).' '.sprintf('%.3f',$y3).' c S';
  1616. }
  1617. /**
  1618. * draw a part of an ellipse
  1619. */
  1620. function partEllipse($x0,$y0,$astart,$afinish,$r1,$r2=0,$angle=0,$nSeg=8){
  1621. $this->ellipse($x0,$y0,$r1,$r2,$angle,$nSeg,$astart,$afinish,0);
  1622. }
  1623. /**
  1624. * draw a filled ellipse
  1625. */
  1626. function filledEllipse($x0,$y0,$r1,$r2=0,$angle=0,$nSeg=8,$astart=0,$afinish=360){
  1627. return $this->ellipse($x0,$y0,$r1,$r2=0,$angle,$nSeg,$astart,$afinish,1,1);
  1628. }
  1629. /**
  1630. * draw an ellipse
  1631. * note that the part and filled ellipse are just special cases of this function
  1632. *
  1633. * draws an ellipse in the current line style
  1634. * centered at $x0,$y0, radii $r1,$r2
  1635. * if $r2 is not set, then a circle is drawn
  1636. * nSeg is not allowed to be less than 2, as this will simply draw a line (and will even draw a
  1637. * pretty crappy shape at 2, as we are approximating with bezier curves.
  1638. */
  1639. function ellipse($x0,$y0,$r1,$r2=0,$angle=0,$nSeg=8,$astart=0,$afinish=360,$close=1,$fill=0){
  1640. if ($r1==0){
  1641. return;
  1642. }
  1643. if ($r2==0){
  1644. $r2=$r1;
  1645. }
  1646. if ($nSeg<2){
  1647. $nSeg=2;
  1648. }
  1649. $astart = deg2rad((float)$astart);
  1650. $afinish = deg2rad((float)$afinish);
  1651. $totalAngle =$afinish-$astart;
  1652. $dt = $totalAngle/$nSeg;
  1653. $dtm = $dt/3;
  1654. if ($angle != 0){
  1655. $a = -1*deg2rad((float)$angle);
  1656. $tmp = "\n q ";
  1657. $tmp .= sprintf('%.3f',cos($a)).' '.sprintf('%.3f',(-1.0*sin($a))).' '.sprintf('%.3f',sin($a)).' '.sprintf('%.3f',cos($a)).' ';
  1658. $tmp .= sprintf('%.3f',$x0).' '.sprintf('%.3f',$y0).' cm';
  1659. $this->objects[$this->currentContents]['c'].= $tmp;
  1660. $x0=0;
  1661. $y0=0;
  1662. }
  1663. $t1 = $astart;
  1664. $a0 = $x0+$r1*cos($t1);
  1665. $b0 = $y0+$r2*sin($t1);
  1666. $c0 = -$r1*sin($t1);
  1667. $d0 = $r2*cos($t1);
  1668. $this->objects[$this->currentContents]['c'].="\n".sprintf('%.3f',$a0).' '.sprintf('%.3f',$b0).' m ';
  1669. for ($i=1;$i<=$nSeg;$i++){
  1670. // draw this bit of the total curve
  1671. $t1 = $i*$dt+$astart;
  1672. $a1 = $x0+$r1*cos($t1);
  1673. $b1 = $y0+$r2*sin($t1);
  1674. $c1 = -$r1*sin($t1);
  1675. $d1 = $r2*cos($t1);
  1676. $this->objects[$this->currentContents]['c'].="\n".sprintf('%.3f',($a0+$c0*$dtm)).' '.sprintf('%.3f',($b0+$d0*$dtm));
  1677. $this->objects[$this->currentContents]['c'].= ' '.sprintf('%.3f',($a1-$c1*$dtm)).' '.sprintf('%.3f',($b1-$d1*$dtm)).' '.sprintf('%.3f',$a1).' '.sprintf('%.3f',$b1).' c';
  1678. $a0=$a1;
  1679. $b0=$b1;
  1680. $c0=$c1;
  1681. $d0=$d1;
  1682. }
  1683. if ($fill){
  1684. $this->objects[$this->currentContents]['c'].=' f';
  1685. } else {
  1686. if ($close){
  1687. $this->objects[$this->currentContents]['c'].=' s'; // small 's' signifies closing the path as well
  1688. } else {
  1689. $this->objects[$this->currentContents]['c'].=' S';
  1690. }
  1691. }
  1692. if ($angle !=0){
  1693. $this->objects[$this->currentContents]['c'].=' Q';
  1694. }
  1695. }
  1696. /**
  1697. * this sets the line drawing style.
  1698. * width, is the thickness of the line in user units
  1699. * cap is the type of cap to put on the line, values can be 'butt','round','square'
  1700. * where the diffference between 'square' and 'butt' is that 'square' projects a flat end past the
  1701. * end of the line.
  1702. * join can be 'miter', 'round', 'bevel'
  1703. * dash is an array which sets the dash pattern, is a series of length values, which are the lengths of the
  1704. * on and off dashes.
  1705. * (2) represents 2 on, 2 off, 2 on , 2 off ...
  1706. * (2,1) is 2 on, 1 off, 2 on, 1 off.. etc
  1707. * phase is a modifier on the dash pattern which is used to shift the point at which the pattern starts.
  1708. */
  1709. function setLineStyle($width=1,$cap='',$join='',$dash='',$phase=0){
  1710. // this is quite inefficient in that it sets all the parameters whenever 1 is changed, but will fix another day
  1711. $string = '';
  1712. if ($width>0){
  1713. $string.= $width.' w';
  1714. }
  1715. $ca = array('butt'=>0,'round'=>1,'square'=>2);
  1716. if (isset($ca[$cap])){
  1717. $string.= ' '.$ca[$cap].' J';
  1718. }
  1719. $ja = array('miter'=>0,'round'=>1,'bevel'=>2);
  1720. if (isset($ja[$join])){
  1721. $string.= ' '.$ja[$join].' j';
  1722. }
  1723. if (is_array($dash)){
  1724. $string.= ' [';
  1725. foreach ($dash as $len){
  1726. $string.=' '.$len;
  1727. }
  1728. $string.= ' ] '.$phase.' d';
  1729. }
  1730. $this->currentLineStyle = $string;
  1731. $this->objects[$this->currentContents]['c'].="\n".$string;
  1732. }
  1733. /**
  1734. * draw a polygon, the syntax for this is similar to the GD polygon command
  1735. */
  1736. function polygon($p,$np,$f=0){
  1737. $this->objects[$this->currentContents]['c'].="\n";
  1738. $this->objects[$this->currentContents]['c'].=sprintf('%.3f',$p[0]).' '.sprintf('%.3f',$p[1]).' m ';
  1739. for ($i=2;$i<$np*2;$i=$i+2){
  1740. $this->objects[$this->currentContents]['c'].= sprintf('%.3f',$p[$i]).' '.sprintf('%.3f',$p[$i+1]).' l ';
  1741. }
  1742. if ($f==1){
  1743. $this->objects[$this->currentContents]['c'].=' f';
  1744. } else {
  1745. $this->objects[$this->currentContents]['c'].=' S';
  1746. }
  1747. }
  1748. /**
  1749. * a filled rectangle, note that it is the width and height of the rectangle which are the secondary paramaters, not
  1750. * the coordinates of the upper-right corner
  1751. */
  1752. function filledRectangle($x1,$y1,$width,$height){
  1753. $this->objects[$this->currentContents]['c'].="\n".sprintf('%.3f',$x1).' '.sprintf('%.3f',$y1).' '.sprintf('%.3f',$width).' '.sprintf('%.3f',$height).' re f';
  1754. }
  1755. /**
  1756. * draw a rectangle, note that it is the width and height of the rectangle which are the secondary paramaters, not
  1757. * the coordinates of the upper-right corner
  1758. */
  1759. function rectangle($x1,$y1,$width,$height){
  1760. $this->objects[$this->currentContents]['c'].="\n".sprintf('%.3f',$x1).' '.sprintf('%.3f',$y1).' '.sprintf('%.3f',$width).' '.sprintf('%.3f',$height).' re S';
  1761. }
  1762. /**
  1763. * add a new page to the document
  1764. * this also makes the new page the current active object
  1765. */
  1766. function newPage($insert=0,$id=0,$pos='after'){
  1767. // if there is a state saved, then go up the stack closing them
  1768. // then on the new page, re-open them with the right setings
  1769. if ($this->nStateStack){
  1770. for ($i=$this->nStateStack;$i>=1;$i--){
  1771. $this->restoreState($i);
  1772. }
  1773. }
  1774. $this->numObj++;
  1775. if ($insert){
  1776. // the id from the ezPdf class is the od of the contents of the page, not the page object itself
  1777. // query that object to find the parent
  1778. $rid = $this->objects[$id]['onPage'];
  1779. $opt= array('rid'=>$rid,'pos'=>$pos);
  1780. $this->o_page($this->numObj,'new',$opt);
  1781. } else {
  1782. $this->o_page($this->numObj,'new');
  1783. }
  1784. // if there is a stack saved, then put that onto the page
  1785. if ($this->nStateStack){
  1786. for ($i=1;$i<=$this->nStateStack;$i++){
  1787. $this->saveState($i);
  1788. }
  1789. }
  1790. // and if there has been a stroke or fill colour set, then transfer them
  1791. if ($this->currentColour['r']>=0){
  1792. $this->setColor($this->currentColour['r'],$this->currentColour['g'],$this->currentColour['b'],1);
  1793. }
  1794. if ($this->currentStrokeColour['r']>=0){
  1795. $this->setStrokeColor($this->currentStrokeColour['r'],$this->currentStrokeColour['g'],$this->currentStrokeColour['b'],1);
  1796. }
  1797. // if there is a line style set, then put this in too
  1798. if (strlen($this->currentLineStyle)){
  1799. $this->objects[$this->currentContents]['c'].="\n".$this->currentLineStyle;
  1800. }
  1801. // the call to the o_page object set currentContents to the present page, so this can be returned as the page id
  1802. return $this->currentContents;
  1803. }
  1804. /**
  1805. * output the pdf code, streaming it to the browser
  1806. * the relevant headers are set so that hopefully the browser will recognise it
  1807. */
  1808. function stream($options=''){
  1809. // setting the options allows the adjustment of the headers
  1810. // values at the moment are:
  1811. // 'Content-Disposition'=>'filename' - sets the filename, though not too sure how well this will
  1812. // work as in my trial the browser seems to use the filename of the php file with .pdf on the end
  1813. // 'Accept-Ranges'=>1 or 0 - if this is not set to 1, then this header is not included, off by default
  1814. // this header seems to have caused some problems despite tha fact that it is supposed to solve
  1815. // them, so I am leaving it off by default.
  1816. // 'compress'=> 1 or 0 - apply content stream compression, this is on (1) by default
  1817. if (!is_array($options)){
  1818. $options=array();
  1819. }
  1820. if ( isset($options['compress']) && $options['compress']==0){
  1821. $tmp = $this->output(1);
  1822. } else {
  1823. $tmp = $this->output();
  1824. }
  1825. // Rod's mods below are based on this tip:
  1826. // http://sourceforge.net/forum/forum.php?thread_id=1420028&forum_id=147987
  1827. header('Cache-Control:'); // added by Rod
  1828. header('Pragma:'); // added by Rod
  1829. header("Content-type: application/pdf");
  1830. header("Content-Length: ".strlen(ltrim($tmp)));
  1831. $fileName = (isset($options['Content-Disposition'])?$options['Content-Disposition']:'file.pdf');
  1832. // header("Content-Disposition: inline; filename=".$fileName);
  1833. // This supports a new "inline" keyword in the options array. If true, the
  1834. // Content-Disposition header is set to indicate an inline document:
  1835. header("Content-Disposition: " .
  1836. (empty($options['inline']) ? "attachment" : "inline") .
  1837. "; filename=" . $fileName);
  1838. // End of Rod's mods.
  1839. if (isset($options['Accept-Ranges']) && $options['Accept-Ranges']==1){
  1840. header("Accept-Ranges: ".strlen(ltrim($tmp)));
  1841. }
  1842. echo ltrim($tmp);
  1843. }
  1844. /**
  1845. * return the height in units of the current font in the given size
  1846. */
  1847. function getFontHeight($size){
  1848. if (!$this->numFonts){
  1849. $this->selectFont('./fonts/Helvetica');
  1850. }
  1851. // for the current font, and the given size, what is the height of the font in user units
  1852. $h = $this->fonts[$this->currentFont]['FontBBox'][3]-$this->fonts[$this->currentFont]['FontBBox'][1];
  1853. return $size*$h/1000;
  1854. }
  1855. /**
  1856. * return the font decender, this will normally return a negative number
  1857. * if you add this number to the baseline, you get the level of the bottom of the font
  1858. * it is in the pdf user units
  1859. */
  1860. function getFontDecender($size){
  1861. // note that this will most likely return a negative value
  1862. if (!$this->numFonts){
  1863. $this->selectFont('./fonts/Helvetica');
  1864. }
  1865. $h = $this->fonts[$this->currentFont]['FontBBox'][1];
  1866. return $size*$h/1000;
  1867. }
  1868. /**
  1869. * filter the text, this is applied to all text just before being inserted into the pdf document
  1870. * it escapes the various things that need to be escaped, and so on
  1871. *
  1872. * @access private
  1873. */
  1874. function filterText($text){
  1875. $text = str_replace('\\','\\\\',$text);
  1876. $text = str_replace('(','\(',$text);
  1877. $text = str_replace(')','\)',$text);
  1878. $text = str_replace('&lt;','<',$text);
  1879. $text = str_replace('&gt;','>',$text);
  1880. $text = str_replace('&#039;','\'',$text);
  1881. $text = str_replace('&quot;','"',$text);
  1882. $text = str_replace('&amp;','&',$text);
  1883. return $text;
  1884. }
  1885. /**
  1886. * given a start position and information about how text is to be laid out, calculate where
  1887. * on the page the text will end
  1888. *
  1889. * @access private
  1890. */
  1891. function PRVTgetTextPosition($x,$y,$angle,$size,$wa,$text){
  1892. // given this information return an array containing x and y for the end position as elements 0 and 1
  1893. $w = $this->getTextWidth($size,$text);
  1894. // need to adjust for the number of spaces in this text
  1895. $words = explode(' ',$text);
  1896. $nspaces=count($words)-1;
  1897. $w += $wa*$nspaces;
  1898. $a = deg2rad((float)$angle);
  1899. return array(cos($a)*$w+$x,-sin($a)*$w+$y);
  1900. }
  1901. /**
  1902. * wrapper function for PRVTcheckTextDirective1
  1903. *
  1904. * @access private
  1905. */
  1906. function PRVTcheckTextDirective(&$text,$i,&$f){
  1907. $x=0;
  1908. $y=0;
  1909. return $this->PRVTcheckTextDirective1($text,$i,$f,0,$x,$y);
  1910. }
  1911. /**
  1912. * checks if the text stream contains a control directive
  1913. * if so then makes some changes and returns the number of characters involved in the directive
  1914. * this has been re-worked to include everything neccesary to fins the current writing point, so that
  1915. * the location can be sent to the callback function if required
  1916. * if the directive does not require a font change, then $f should be set to 0
  1917. *
  1918. * @access private
  1919. */
  1920. function PRVTcheckTextDirective1(&$text,$i,&$f,$final,&$x,&$y,$size=0,$angle=0,$wordSpaceAdjust=0){
  1921. $directive = 0;
  1922. $j=$i;
  1923. if ($text[$j]=='<'){
  1924. $j++;
  1925. switch($text[$j]){
  1926. case '/':
  1927. $j++;
  1928. if (strlen($text) <= $j){
  1929. return $directive;
  1930. }
  1931. switch($text[$j]){
  1932. case 'b':
  1933. case 'i':
  1934. $j++;
  1935. if ($text[$j]=='>'){
  1936. $p = strrpos($this->currentTextState,$text[$j-1]);
  1937. if ($p !== false){
  1938. // then there is one to remove
  1939. $this->currentTextState = substr($this->currentTextState,0,$p).substr($this->currentTextState,$p+1);
  1940. }
  1941. $directive=$j-$i+1;
  1942. }
  1943. break;
  1944. case 'c':
  1945. // this this might be a callback function
  1946. $j++;
  1947. $k = strpos($text,'>',$j);
  1948. if ($k!==false && $text[$j]==':'){
  1949. // then this will be treated as a callback directive
  1950. $directive = $k-$i+1;
  1951. $f=0;
  1952. // split the remainder on colons to get the function name and the paramater
  1953. $tmp = substr($text,$j+1,$k-$j-1);
  1954. $b1 = strpos($tmp,':');
  1955. if ($b1!==false){
  1956. $func = substr($tmp,0,$b1);
  1957. $parm = substr($tmp,$b1+1);
  1958. } else {
  1959. $func=$tmp;
  1960. $parm='';
  1961. }
  1962. if (!isset($func) || !strlen(trim($func))){
  1963. $directive=0;
  1964. } else {
  1965. // only call the function if this is the final call
  1966. if ($final){
  1967. // need to assess the text position, calculate the text width to this point
  1968. // can use getTextWidth to find the text width I think
  1969. $tmp = $this->PRVTgetTextPosition($x,$y,$angle,$size,$wordSpaceAdjust,substr($text,0,$i));
  1970. $info = array('x'=>$tmp[0],'y'=>$tmp[1],'angle'=>$angle,'status'=>'end','p'=>$parm,'nCallback'=>$this->nCallback);
  1971. $x=$tmp[0];
  1972. $y=$tmp[1];
  1973. $ret = $this->$func($info);
  1974. if (is_array($ret)){
  1975. // then the return from the callback function could set the position, to start with, later will do font colour, and font
  1976. foreach($ret as $rk=>$rv){
  1977. switch($rk){
  1978. case 'x':
  1979. case 'y':
  1980. $$rk=$rv;
  1981. break;
  1982. }
  1983. }
  1984. }
  1985. // also remove from to the stack
  1986. // for simplicity, just take from the end, fix this another day
  1987. $this->nCallback--;
  1988. if ($this->nCallback<0){
  1989. $this->nCallBack=0;
  1990. }
  1991. }
  1992. }
  1993. }
  1994. break;
  1995. }
  1996. break;
  1997. case 'b':
  1998. case 'i':
  1999. $j++;
  2000. if ($text[$j]=='>'){
  2001. $this->currentTextState.=$text[$j-1];
  2002. $directive=$j-$i+1;
  2003. }
  2004. break;
  2005. case 'C':
  2006. $noClose=1;
  2007. case 'c':
  2008. // this this might be a callback function
  2009. $j++;
  2010. $k = strpos($text,'>',$j);
  2011. if ($k!==false && $text[$j]==':'){
  2012. // then this will be treated as a callback directive
  2013. $directive = $k-$i+1;
  2014. $f=0;
  2015. // split the remainder on colons to get the function name and the paramater
  2016. // $bits = explode(':',substr($text,$j+1,$k-$j-1));
  2017. $tmp = substr($text,$j+1,$k-$j-1);
  2018. $b1 = strpos($tmp,':');
  2019. if ($b1!==false){
  2020. $func = substr($tmp,0,$b1);
  2021. $parm = substr($tmp,$b1+1);
  2022. } else {
  2023. $func=$tmp;
  2024. $parm='';
  2025. }
  2026. if (!isset($func) || !strlen(trim($func))){
  2027. $directive=0;
  2028. } else {
  2029. // only call the function if this is the final call, ie, the one actually doing printing, not measurement
  2030. if ($final){
  2031. // need to assess the text position, calculate the text width to this point
  2032. // can use getTextWidth to find the text width I think
  2033. // also add the text height and decender
  2034. $tmp = $this->PRVTgetTextPosition($x,$y,$angle,$size,$wordSpaceAdjust,substr($text,0,$i));
  2035. $info = array('x'=>$tmp[0],'y'=>$tmp[1],'angle'=>$angle,'status'=>'start','p'=>$parm,'f'=>$func,'height'=>$this->getFontHeight($size),'decender'=>$this->getFontDecender($size));
  2036. $x=$tmp[0];
  2037. $y=$tmp[1];
  2038. if (!isset($noClose) || !$noClose){
  2039. // only add to the stack if this is a small 'c', therefore is a start-stop pair
  2040. $this->nCallback++;
  2041. $info['nCallback']=$this->nCallback;
  2042. $this->callback[$this->nCallback]=$info;
  2043. }
  2044. $ret = $this->$func($info);
  2045. if (is_array($ret)){
  2046. // then the return from the callback function could set the position, to start with, later will do font colour, and font
  2047. foreach($ret as $rk=>$rv){
  2048. switch($rk){
  2049. case 'x':
  2050. case 'y':
  2051. $$rk=$rv;
  2052. break;
  2053. }
  2054. }
  2055. }
  2056. }
  2057. }
  2058. }
  2059. break;
  2060. }
  2061. }
  2062. return $directive;
  2063. }
  2064. /**
  2065. * add text to the document, at a specified location, size and angle on the page
  2066. */
  2067. function addText($x,$y,$size,$text,$angle=0,$wordSpaceAdjust=0){
  2068. if (!$this->numFonts){$this->selectFont('./fonts/Helvetica');}
  2069. // if there are any open callbacks, then they should be called, to show the start of the line
  2070. if ($this->nCallback>0){
  2071. for ($i=$this->nCallback;$i>0;$i--){
  2072. // call each function
  2073. $info = array('x'=>$x,'y'=>$y,'angle'=>$angle,'status'=>'sol','p'=>$this->callback[$i]['p'],'nCallback'=>$this->callback[$i]['nCallback'],'height'=>$this->callback[$i]['height'],'decender'=>$this->callback[$i]['decender']);
  2074. $func = $this->callback[$i]['f'];
  2075. $this->$func($info);
  2076. }
  2077. }
  2078. if ($angle==0){
  2079. $this->objects[$this->currentContents]['c'].="\n".'BT '.sprintf('%.3f',$x).' '.sprintf('%.3f',$y).' Td';
  2080. } else {
  2081. $a = deg2rad((float)$angle);
  2082. $tmp = "\n".'BT ';
  2083. $tmp .= sprintf('%.3f',cos($a)).' '.sprintf('%.3f',(-1.0*sin($a))).' '.sprintf('%.3f',sin($a)).' '.sprintf('%.3f',cos($a)).' ';
  2084. $tmp .= sprintf('%.3f',$x).' '.sprintf('%.3f',$y).' Tm';
  2085. $this->objects[$this->currentContents]['c'] .= $tmp;
  2086. }
  2087. if ($wordSpaceAdjust!=0 || $wordSpaceAdjust != $this->wordSpaceAdjust){
  2088. $this->wordSpaceAdjust=$wordSpaceAdjust;
  2089. $this->objects[$this->currentContents]['c'].=' '.sprintf('%.3f',$wordSpaceAdjust).' Tw';
  2090. }
  2091. $len=strlen($text);
  2092. $start=0;
  2093. for ($i=0;$i<$len;$i++){
  2094. $f=1;
  2095. $directive = $this->PRVTcheckTextDirective($text,$i,$f);
  2096. if ($directive){
  2097. // then we should write what we need to
  2098. if ($i>$start){
  2099. $part = substr($text,$start,$i-$start);
  2100. $this->objects[$this->currentContents]['c'].=' /F'.$this->currentFontNum.' '.sprintf('%.1f',$size).' Tf ';
  2101. $this->objects[$this->currentContents]['c'].=' ('.$this->filterText($part).') Tj';
  2102. }
  2103. if ($f){
  2104. // then there was nothing drastic done here, restore the contents
  2105. $this->setCurrentFont();
  2106. } else {
  2107. $this->objects[$this->currentContents]['c'] .= ' ET';
  2108. $f=1;
  2109. $xp=$x;
  2110. $yp=$y;
  2111. $directive = $this->PRVTcheckTextDirective1($text,$i,$f,1,$xp,$yp,$size,$angle,$wordSpaceAdjust);
  2112. // restart the text object
  2113. if ($angle==0){
  2114. $this->objects[$this->currentContents]['c'].="\n".'BT '.sprintf('%.3f',$xp).' '.sprintf('%.3f',$yp).' Td';
  2115. } else {
  2116. $a = deg2rad((float)$angle);
  2117. $tmp = "\n".'BT ';
  2118. $tmp .= sprintf('%.3f',cos($a)).' '.sprintf('%.3f',(-1.0*sin($a))).' '.sprintf('%.3f',sin($a)).' '.sprintf('%.3f',cos($a)).' ';
  2119. $tmp .= sprintf('%.3f',$xp).' '.sprintf('%.3f',$yp).' Tm';
  2120. $this->objects[$this->currentContents]['c'] .= $tmp;
  2121. }
  2122. if ($wordSpaceAdjust!=0 || $wordSpaceAdjust != $this->wordSpaceAdjust){
  2123. $this->wordSpaceAdjust=$wordSpaceAdjust;
  2124. $this->objects[$this->currentContents]['c'].=' '.sprintf('%.3f',$wordSpaceAdjust).' Tw';
  2125. }
  2126. }
  2127. // and move the writing point to the next piece of text
  2128. $i=$i+$directive-1;
  2129. $start=$i+1;
  2130. }
  2131. }
  2132. if ($start<$len){
  2133. $part = substr($text,$start);
  2134. $this->objects[$this->currentContents]['c'].=' /F'.$this->currentFontNum.' '.sprintf('%.1f',$size).' Tf ';
  2135. $this->objects[$this->currentContents]['c'].=' ('.$this->filterText($part).') Tj';
  2136. }
  2137. $this->objects[$this->currentContents]['c'].=' ET';
  2138. // if there are any open callbacks, then they should be called, to show the end of the line
  2139. if ($this->nCallback>0){
  2140. for ($i=$this->nCallback;$i>0;$i--){
  2141. // call each function
  2142. $tmp = $this->PRVTgetTextPosition($x,$y,$angle,$size,$wordSpaceAdjust,$text);
  2143. $info = array('x'=>$tmp[0],'y'=>$tmp[1],'angle'=>$angle,'status'=>'eol','p'=>$this->callback[$i]['p'],'nCallback'=>$this->callback[$i]['nCallback'],'height'=>$this->callback[$i]['height'],'decender'=>$this->callback[$i]['decender']);
  2144. $func = $this->callback[$i]['f'];
  2145. $this->$func($info);
  2146. }
  2147. }
  2148. }
  2149. /**
  2150. * calculate how wide a given text string will be on a page, at a given size.
  2151. * this can be called externally, but is alse used by the other class functions
  2152. */
  2153. function getTextWidth($size,$text){
  2154. // this function should not change any of the settings, though it will need to
  2155. // track any directives which change during calculation, so copy them at the start
  2156. // and put them back at the end.
  2157. $store_currentTextState = $this->currentTextState;
  2158. if (!$this->numFonts){
  2159. $this->selectFont('./fonts/Helvetica');
  2160. }
  2161. // converts a number or a float to a string so it can get the width
  2162. $text = "$text";
  2163. // hmm, this is where it all starts to get tricky - use the font information to
  2164. // calculate the width of each character, add them up and convert to user units
  2165. $w=0;
  2166. $len=strlen($text);
  2167. $cf = $this->currentFont;
  2168. for ($i=0;$i<$len;$i++){
  2169. $f=1;
  2170. $directive = $this->PRVTcheckTextDirective($text,$i,$f);
  2171. if ($directive){
  2172. if ($f){
  2173. $this->setCurrentFont();
  2174. $cf = $this->currentFont;
  2175. }
  2176. $i=$i+$directive-1;
  2177. } else {
  2178. $char=ord($text[$i]);
  2179. if (isset($this->fonts[$cf]['differences'][$char])){
  2180. // then this character is being replaced by another
  2181. $name = $this->fonts[$cf]['differences'][$char];
  2182. if (isset($this->fonts[$cf]['C'][$name]['WX'])){
  2183. $w+=$this->fonts[$cf]['C'][$name]['WX'];
  2184. }
  2185. } else if (isset($this->fonts[$cf]['C'][$char]['WX'])){
  2186. $w+=$this->fonts[$cf]['C'][$char]['WX'];
  2187. }
  2188. }
  2189. }
  2190. $this->currentTextState = $store_currentTextState;
  2191. $this->setCurrentFont();
  2192. return $w*$size/1000;
  2193. }
  2194. /**
  2195. * do a part of the calculation for sorting out the justification of the text
  2196. *
  2197. * @access private
  2198. */
  2199. function PRVTadjustWrapText($text,$actual,$width,&$x,&$adjust,$justification){
  2200. switch ($justification){
  2201. case 'left':
  2202. return;
  2203. break;
  2204. case 'right':
  2205. $x+=$width-$actual;
  2206. break;
  2207. case 'center':
  2208. case 'centre':
  2209. $x+=($width-$actual)/2;
  2210. break;
  2211. case 'full':
  2212. // count the number of words
  2213. $words = explode(' ',$text);
  2214. $nspaces=count($words)-1;
  2215. if ($nspaces>0){
  2216. $adjust = ($width-$actual)/$nspaces;
  2217. } else {
  2218. $adjust=0;
  2219. }
  2220. break;
  2221. }
  2222. }
  2223. /**
  2224. * add text to the page, but ensure that it fits within a certain width
  2225. * if it does not fit then put in as much as possible, splitting at word boundaries
  2226. * and return the remainder.
  2227. * justification and angle can also be specified for the text
  2228. */
  2229. function addTextWrap($x,$y,$width,$size,$text,$justification='left',$angle=0,$test=0){
  2230. // this will display the text, and if it goes beyond the width $width, will backtrack to the
  2231. // previous space or hyphen, and return the remainder of the text.
  2232. // $justification can be set to 'left','right','center','centre','full'
  2233. // need to store the initial text state, as this will change during the width calculation
  2234. // but will need to be re-set before printing, so that the chars work out right
  2235. $store_currentTextState = $this->currentTextState;
  2236. if (!$this->numFonts){$this->selectFont('./fonts/Helvetica');}
  2237. if ($width<=0){
  2238. // error, pretend it printed ok, otherwise risking a loop
  2239. return '';
  2240. }
  2241. $w=0;
  2242. $break=0;
  2243. $breakWidth=0;
  2244. $len=strlen($text);
  2245. $cf = $this->currentFont;
  2246. $tw = $width/$size*1000;
  2247. for ($i=0;$i<$len;$i++){
  2248. $f=1;
  2249. $directive = $this->PRVTcheckTextDirective($text,$i,$f);
  2250. if ($directive){
  2251. if ($f){
  2252. $this->setCurrentFont();
  2253. $cf = $this->currentFont;
  2254. }
  2255. $i=$i+$directive-1;
  2256. } else {
  2257. $cOrd = ord($text[$i]);
  2258. if (isset($this->fonts[$cf]['differences'][$cOrd])){
  2259. // then this character is being replaced by another
  2260. $cOrd2 = $this->fonts[$cf]['differences'][$cOrd];
  2261. } else {
  2262. $cOrd2 = $cOrd;
  2263. }
  2264. if (isset($this->fonts[$cf]['C'][$cOrd2]['WX'])){
  2265. $w+=$this->fonts[$cf]['C'][$cOrd2]['WX'];
  2266. }
  2267. if ($w>$tw){
  2268. // then we need to truncate this line
  2269. if ($break>0){
  2270. // then we have somewhere that we can split :)
  2271. if ($text[$break]==' '){
  2272. $tmp = substr($text,0,$break);
  2273. } else {
  2274. $tmp = substr($text,0,$break+1);
  2275. }
  2276. $adjust=0;
  2277. $this->PRVTadjustWrapText($tmp,$breakWidth,$width,$x,$adjust,$justification);
  2278. // reset the text state
  2279. $this->currentTextState = $store_currentTextState;
  2280. $this->setCurrentFont();
  2281. if (!$test){
  2282. $this->addText($x,$y,$size,$tmp,$angle,$adjust);
  2283. }
  2284. return substr($text,$break+1);
  2285. } else {
  2286. // just split before the current character
  2287. $tmp = substr($text,0,$i);
  2288. $adjust=0;
  2289. $ctmp=ord($text[$i]);
  2290. if (isset($this->fonts[$cf]['differences'][$ctmp])){
  2291. $ctmp=$this->fonts[$cf]['differences'][$ctmp];
  2292. }
  2293. $tmpw=($w-$this->fonts[$cf]['C'][$ctmp]['WX'])*$size/1000;
  2294. $this->PRVTadjustWrapText($tmp,$tmpw,$width,$x,$adjust,$justification);
  2295. // reset the text state
  2296. $this->currentTextState = $store_currentTextState;
  2297. $this->setCurrentFont();
  2298. if (!$test){
  2299. $this->addText($x,$y,$size,$tmp,$angle,$adjust);
  2300. }
  2301. return substr($text,$i);
  2302. }
  2303. }
  2304. if ($text[$i]=='-'){
  2305. $break=$i;
  2306. $breakWidth = $w*$size/1000;
  2307. }
  2308. if ($text[$i]==' '){
  2309. $break=$i;
  2310. $ctmp=ord($text[$i]);
  2311. if (isset($this->fonts[$cf]['differences'][$ctmp])){
  2312. $ctmp=$this->fonts[$cf]['differences'][$ctmp];
  2313. }
  2314. $breakWidth = ($w-$this->fonts[$cf]['C'][$ctmp]['WX'])*$size/1000;
  2315. }
  2316. }
  2317. }
  2318. // then there was no need to break this line
  2319. if ($justification=='full'){
  2320. $justification='left';
  2321. }
  2322. $adjust=0;
  2323. $tmpw=$w*$size/1000;
  2324. $this->PRVTadjustWrapText($text,$tmpw,$width,$x,$adjust,$justification);
  2325. // reset the text state
  2326. $this->currentTextState = $store_currentTextState;
  2327. $this->setCurrentFont();
  2328. if (!$test){
  2329. $this->addText($x,$y,$size,$text,$angle,$adjust,$angle);
  2330. }
  2331. return '';
  2332. }
  2333. /**
  2334. * this will be called at a new page to return the state to what it was on the
  2335. * end of the previous page, before the stack was closed down
  2336. * This is to get around not being able to have open 'q' across pages
  2337. *
  2338. */
  2339. function saveState($pageEnd=0){
  2340. if ($pageEnd){
  2341. // this will be called at a new page to return the state to what it was on the
  2342. // end of the previous page, before the stack was closed down
  2343. // This is to get around not being able to have open 'q' across pages
  2344. $opt = $this->stateStack[$pageEnd]; // ok to use this as stack starts numbering at 1
  2345. $this->setColor($opt['col']['r'],$opt['col']['g'],$opt['col']['b'],1);
  2346. $this->setStrokeColor($opt['str']['r'],$opt['str']['g'],$opt['str']['b'],1);
  2347. $this->objects[$this->currentContents]['c'].="\n".$opt['lin'];
  2348. // $this->currentLineStyle = $opt['lin'];
  2349. } else {
  2350. $this->nStateStack++;
  2351. $this->stateStack[$this->nStateStack]=array(
  2352. 'col'=>$this->currentColour
  2353. ,'str'=>$this->currentStrokeColour
  2354. ,'lin'=>$this->currentLineStyle
  2355. );
  2356. }
  2357. $this->objects[$this->currentContents]['c'].="\nq";
  2358. }
  2359. /**
  2360. * restore a previously saved state
  2361. */
  2362. function restoreState($pageEnd=0){
  2363. if (!$pageEnd){
  2364. $n = $this->nStateStack;
  2365. $this->currentColour = $this->stateStack[$n]['col'];
  2366. $this->currentStrokeColour = $this->stateStack[$n]['str'];
  2367. $this->objects[$this->currentContents]['c'].="\n".$this->stateStack[$n]['lin'];
  2368. $this->currentLineStyle = $this->stateStack[$n]['lin'];
  2369. unset($this->stateStack[$n]);
  2370. $this->nStateStack--;
  2371. }
  2372. $this->objects[$this->currentContents]['c'].="\nQ";
  2373. }
  2374. /**
  2375. * make a loose object, the output will go into this object, until it is closed, then will revert to
  2376. * the current one.
  2377. * this object will not appear until it is included within a page.
  2378. * the function will return the object number
  2379. */
  2380. function openObject(){
  2381. $this->nStack++;
  2382. $this->stack[$this->nStack]=array('c'=>$this->currentContents,'p'=>$this->currentPage);
  2383. // add a new object of the content type, to hold the data flow
  2384. $this->numObj++;
  2385. $this->o_contents($this->numObj,'new');
  2386. $this->currentContents=$this->numObj;
  2387. $this->looseObjects[$this->numObj]=1;
  2388. return $this->numObj;
  2389. }
  2390. /**
  2391. * open an existing object for editing
  2392. */
  2393. function reopenObject($id){
  2394. $this->nStack++;
  2395. $this->stack[$this->nStack]=array('c'=>$this->currentContents,'p'=>$this->currentPage);
  2396. $this->currentContents=$id;
  2397. // also if this object is the primary contents for a page, then set the current page to its parent
  2398. if (isset($this->objects[$id]['onPage'])){
  2399. $this->currentPage = $this->objects[$id]['onPage'];
  2400. }
  2401. }
  2402. /**
  2403. * close an object
  2404. */
  2405. function closeObject(){
  2406. // close the object, as long as there was one open in the first place, which will be indicated by
  2407. // an objectId on the stack.
  2408. if ($this->nStack>0){
  2409. $this->currentContents=$this->stack[$this->nStack]['c'];
  2410. $this->currentPage=$this->stack[$this->nStack]['p'];
  2411. $this->nStack--;
  2412. // easier to probably not worry about removing the old entries, they will be overwritten
  2413. // if there are new ones.
  2414. }
  2415. }
  2416. /**
  2417. * stop an object from appearing on pages from this point on
  2418. */
  2419. function stopObject($id){
  2420. // if an object has been appearing on pages up to now, then stop it, this page will
  2421. // be the last one that could contian it.
  2422. if (isset($this->addLooseObjects[$id])){
  2423. $this->addLooseObjects[$id]='';
  2424. }
  2425. }
  2426. /**
  2427. * after an object has been created, it wil only show if it has been added, using this function.
  2428. */
  2429. function addObject($id,$options='add'){
  2430. // add the specified object to the page
  2431. if (isset($this->looseObjects[$id]) && $this->currentContents!=$id){
  2432. // then it is a valid object, and it is not being added to itself
  2433. switch($options){
  2434. case 'all':
  2435. // then this object is to be added to this page (done in the next block) and
  2436. // all future new pages.
  2437. $this->addLooseObjects[$id]='all';
  2438. case 'add':
  2439. if (isset($this->objects[$this->currentContents]['onPage'])){
  2440. // then the destination contents is the primary for the page
  2441. // (though this object is actually added to that page)
  2442. $this->o_page($this->objects[$this->currentContents]['onPage'],'content',$id);
  2443. }
  2444. break;
  2445. case 'even':
  2446. $this->addLooseObjects[$id]='even';
  2447. $pageObjectId=$this->objects[$this->currentContents]['onPage'];
  2448. if ($this->objects[$pageObjectId]['info']['pageNum']%2==0){
  2449. $this->addObject($id); // hacky huh :)
  2450. }
  2451. break;
  2452. case 'odd':
  2453. $this->addLooseObjects[$id]='odd';
  2454. $pageObjectId=$this->objects[$this->currentContents]['onPage'];
  2455. if ($this->objects[$pageObjectId]['info']['pageNum']%2==1){
  2456. $this->addObject($id); // hacky huh :)
  2457. }
  2458. break;
  2459. case 'next':
  2460. $this->addLooseObjects[$id]='all';
  2461. break;
  2462. case 'nexteven':
  2463. $this->addLooseObjects[$id]='even';
  2464. break;
  2465. case 'nextodd':
  2466. $this->addLooseObjects[$id]='odd';
  2467. break;
  2468. }
  2469. }
  2470. }
  2471. /**
  2472. * add content to the documents info object
  2473. */
  2474. function addInfo($label,$value=0){
  2475. // this will only work if the label is one of the valid ones.
  2476. // modify this so that arrays can be passed as well.
  2477. // if $label is an array then assume that it is key=>value pairs
  2478. // else assume that they are both scalar, anything else will probably error
  2479. if (is_array($label)){
  2480. foreach ($label as $l=>$v){
  2481. $this->o_info($this->infoObject,$l,$v);
  2482. }
  2483. } else {
  2484. $this->o_info($this->infoObject,$label,$value);
  2485. }
  2486. }
  2487. /**
  2488. * set the viewer preferences of the document, it is up to the browser to obey these.
  2489. */
  2490. function setPreferences($label,$value=0){
  2491. // this will only work if the label is one of the valid ones.
  2492. if (is_array($label)){
  2493. foreach ($label as $l=>$v){
  2494. $this->o_catalog($this->catalogId,'viewerPreferences',array($l=>$v));
  2495. }
  2496. } else {
  2497. $this->o_catalog($this->catalogId,'viewerPreferences',array($label=>$value));
  2498. }
  2499. }
  2500. /**
  2501. * extract an integer from a position in a byte stream
  2502. *
  2503. * @access private
  2504. */
  2505. function PRVT_getBytes(&$data,$pos,$num){
  2506. // return the integer represented by $num bytes from $pos within $data
  2507. $ret=0;
  2508. for ($i=0;$i<$num;$i++){
  2509. $ret=$ret*256;
  2510. $ret+=ord($data[$pos+$i]);
  2511. }
  2512. return $ret;
  2513. }
  2514. /**
  2515. * add a PNG image into the document, from a file
  2516. * this should work with remote files
  2517. */
  2518. function addPngFromFile($file,$x,$y,$w=0,$h=0){
  2519. // read in a png file, interpret it, then add to the system
  2520. $error=0;
  2521. $tmp = get_magic_quotes_runtime();
  2522. set_magic_quotes_runtime(0);
  2523. $fp = @fopen($file,'rb');
  2524. if ($fp){
  2525. $data='';
  2526. while(!feof($fp)){
  2527. $data .= fread($fp,1024);
  2528. }
  2529. fclose($fp);
  2530. } else {
  2531. $error = 1;
  2532. $errormsg = 'trouble opening file: '.$file;
  2533. }
  2534. set_magic_quotes_runtime($tmp);
  2535. if (!$error){
  2536. $header = chr(137).chr(80).chr(78).chr(71).chr(13).chr(10).chr(26).chr(10);
  2537. if (substr($data,0,8)!=$header){
  2538. $error=1;
  2539. $errormsg = 'this file does not have a valid header';
  2540. }
  2541. }
  2542. if (!$error){
  2543. // set pointer
  2544. $p = 8;
  2545. $len = strlen($data);
  2546. // cycle through the file, identifying chunks
  2547. $haveHeader=0;
  2548. $info=array();
  2549. $idata='';
  2550. $pdata='';
  2551. while ($p<$len){
  2552. $chunkLen = $this->PRVT_getBytes($data,$p,4);
  2553. $chunkType = substr($data,$p+4,4);
  2554. // echo $chunkType.' - '.$chunkLen.'<br>';
  2555. switch($chunkType){
  2556. case 'IHDR':
  2557. // this is where all the file information comes from
  2558. $info['width']=$this->PRVT_getBytes($data,$p+8,4);
  2559. $info['height']=$this->PRVT_getBytes($data,$p+12,4);
  2560. $info['bitDepth']=ord($data[$p+16]);
  2561. $info['colorType']=ord($data[$p+17]);
  2562. $info['compressionMethod']=ord($data[$p+18]);
  2563. $info['filterMethod']=ord($data[$p+19]);
  2564. $info['interlaceMethod']=ord($data[$p+20]);
  2565. //print_r($info);
  2566. $haveHeader=1;
  2567. if ($info['compressionMethod']!=0){
  2568. $error=1;
  2569. $errormsg = 'unsupported compression method';
  2570. }
  2571. if ($info['filterMethod']!=0){
  2572. $error=1;
  2573. $errormsg = 'unsupported filter method';
  2574. }
  2575. break;
  2576. case 'PLTE':
  2577. $pdata.=substr($data,$p+8,$chunkLen);
  2578. break;
  2579. case 'IDAT':
  2580. $idata.=substr($data,$p+8,$chunkLen);
  2581. break;
  2582. case 'tRNS':
  2583. //this chunk can only occur once and it must occur after the PLTE chunk and before IDAT chunk
  2584. //print "tRNS found, color type = ".$info['colorType']."<BR>";
  2585. $transparency = array();
  2586. if ($info['colorType'] == 3) { // indexed color, rbg
  2587. /* corresponding to entries in the plte chunk
  2588. Alpha for palette index 0: 1 byte
  2589. Alpha for palette index 1: 1 byte
  2590. ...etc...
  2591. */
  2592. // there will be one entry for each palette entry. up until the last non-opaque entry.
  2593. // set up an array, stretching over all palette entries which will be o (opaque) or 1 (transparent)
  2594. $transparency['type']='indexed';
  2595. $numPalette = strlen($pdata)/3;
  2596. $trans=0;
  2597. for ($i=$chunkLen;$i>=0;$i--){
  2598. if (ord($data[$p+8+$i])==0){
  2599. $trans=$i;
  2600. }
  2601. }
  2602. $transparency['data'] = $trans;
  2603. } elseif($info['colorType'] == 0) { // grayscale
  2604. /* corresponding to entries in the plte chunk
  2605. Gray: 2 bytes, range 0 .. (2^bitdepth)-1
  2606. */
  2607. // $transparency['grayscale']=$this->PRVT_getBytes($data,$p+8,2); // g = grayscale
  2608. $transparency['type']='indexed';
  2609. $transparency['data'] = ord($data[$p+8+1]);
  2610. } elseif($info['colorType'] == 2) { // truecolor
  2611. /* corresponding to entries in the plte chunk
  2612. Red: 2 bytes, range 0 .. (2^bitdepth)-1
  2613. Green: 2 bytes, range 0 .. (2^bitdepth)-1
  2614. Blue: 2 bytes, range 0 .. (2^bitdepth)-1
  2615. */
  2616. $transparency['r']=$this->PRVT_getBytes($data,$p+8,2); // r from truecolor
  2617. $transparency['g']=$this->PRVT_getBytes($data,$p+10,2); // g from truecolor
  2618. $transparency['b']=$this->PRVT_getBytes($data,$p+12,2); // b from truecolor
  2619. } else {
  2620. //unsupported transparency type
  2621. }
  2622. // KS End new code
  2623. break;
  2624. default:
  2625. break;
  2626. }
  2627. $p += $chunkLen+12;
  2628. }
  2629. if(!$haveHeader){
  2630. $error = 1;
  2631. $errormsg = 'information header is missing';
  2632. }
  2633. if (isset($info['interlaceMethod']) && $info['interlaceMethod']){
  2634. $error = 1;
  2635. $errormsg = 'There appears to be no support for interlaced images in pdf.';
  2636. }
  2637. }
  2638. if (!$error && $info['bitDepth'] > 8){
  2639. $error = 1;
  2640. $errormsg = 'only bit depth of 8 or less is supported';
  2641. }
  2642. if (!$error){
  2643. if ($info['colorType']!=2 && $info['colorType']!=0 && $info['colorType']!=3){
  2644. $error = 1;
  2645. $errormsg = 'transparancey alpha channel not supported, transparency only supported for palette images.';
  2646. } else {
  2647. switch ($info['colorType']){
  2648. case 3:
  2649. $color = 'DeviceRGB';
  2650. $ncolor=1;
  2651. break;
  2652. case 2:
  2653. $color = 'DeviceRGB';
  2654. $ncolor=3;
  2655. break;
  2656. case 0:
  2657. $color = 'DeviceGray';
  2658. $ncolor=1;
  2659. break;
  2660. }
  2661. }
  2662. }
  2663. if ($error){
  2664. $this->addMessage('PNG error - ('.$file.') '.$errormsg);
  2665. return;
  2666. }
  2667. if ($w==0){
  2668. $w=$h/$info['height']*$info['width'];
  2669. }
  2670. if ($h==0){
  2671. $h=$w*$info['height']/$info['width'];
  2672. }
  2673. //print_r($info);
  2674. // so this image is ok... add it in.
  2675. $this->numImages++;
  2676. $im=$this->numImages;
  2677. $label='I'.$im;
  2678. $this->numObj++;
  2679. // $this->o_image($this->numObj,'new',array('label'=>$label,'data'=>$idata,'iw'=>$w,'ih'=>$h,'type'=>'png','ic'=>$info['width']));
  2680. $options = array('label'=>$label,'data'=>$idata,'bitsPerComponent'=>$info['bitDepth'],'pdata'=>$pdata
  2681. ,'iw'=>$info['width'],'ih'=>$info['height'],'type'=>'png','color'=>$color,'ncolor'=>$ncolor);
  2682. if (isset($transparency)){
  2683. $options['transparency']=$transparency;
  2684. }
  2685. $this->o_image($this->numObj,'new',$options);
  2686. $this->objects[$this->currentContents]['c'].="\nq";
  2687. $this->objects[$this->currentContents]['c'].="\n".sprintf('%.3f',$w)." 0 0 ".sprintf('%.3f',$h)." ".sprintf('%.3f',$x)." ".sprintf('%.3f',$y)." cm";
  2688. $this->objects[$this->currentContents]['c'].="\n/".$label.' Do';
  2689. $this->objects[$this->currentContents]['c'].="\nQ";
  2690. }
  2691. /**
  2692. * add a JPEG image into the document, from a file
  2693. */
  2694. function addJpegFromFile($img,$x,$y,$w=0,$h=0){
  2695. // attempt to add a jpeg image straight from a file, using no GD commands
  2696. // note that this function is unable to operate on a remote file.
  2697. if (!file_exists($img)){
  2698. return;
  2699. }
  2700. $tmp=getimagesize($img);
  2701. $imageWidth=$tmp[0];
  2702. $imageHeight=$tmp[1];
  2703. if (isset($tmp['channels'])){
  2704. $channels = $tmp['channels'];
  2705. } else {
  2706. $channels = 3;
  2707. }
  2708. if ($w<=0 && $h<=0){
  2709. $w=$imageWidth;
  2710. }
  2711. if ($w==0){
  2712. $w=$h/$imageHeight*$imageWidth;
  2713. }
  2714. if ($h==0){
  2715. $h=$w*$imageHeight/$imageWidth;
  2716. }
  2717. $fp=fopen($img,'rb');
  2718. $tmp = get_magic_quotes_runtime();
  2719. set_magic_quotes_runtime(0);
  2720. $data = fread($fp,filesize($img));
  2721. set_magic_quotes_runtime($tmp);
  2722. fclose($fp);
  2723. $this->addJpegImage_common($data,$x,$y,$w,$h,$imageWidth,$imageHeight,$channels);
  2724. }
  2725. /**
  2726. * add an image into the document, from a GD object
  2727. * this function is not all that reliable, and I would probably encourage people to use
  2728. * the file based functions
  2729. */
  2730. function addImage(&$img,$x,$y,$w=0,$h=0,$quality=75){
  2731. // add a new image into the current location, as an external object
  2732. // add the image at $x,$y, and with width and height as defined by $w & $h
  2733. // note that this will only work with full colour images and makes them jpg images for display
  2734. // later versions could present lossless image formats if there is interest.
  2735. // there seems to be some problem here in that images that have quality set above 75 do not appear
  2736. // not too sure why this is, but in the meantime I have restricted this to 75.
  2737. if ($quality>75){
  2738. $quality=75;
  2739. }
  2740. // if the width or height are set to zero, then set the other one based on keeping the image
  2741. // height/width ratio the same, if they are both zero, then give up :)
  2742. $imageWidth=imagesx($img);
  2743. $imageHeight=imagesy($img);
  2744. if ($w<=0 && $h<=0){
  2745. return;
  2746. }
  2747. if ($w==0){
  2748. $w=$h/$imageHeight*$imageWidth;
  2749. }
  2750. if ($h==0){
  2751. $h=$w*$imageHeight/$imageWidth;
  2752. }
  2753. // gotta get the data out of the img..
  2754. // so I write to a temp file, and then read it back.. soo ugly, my apologies.
  2755. $tmpDir='/tmp';
  2756. $tmpName=tempnam($tmpDir,'img');
  2757. imagejpeg($img,$tmpName,$quality);
  2758. $fp=fopen($tmpName,'rb');
  2759. $tmp = get_magic_quotes_runtime();
  2760. set_magic_quotes_runtime(0);
  2761. $fp = @fopen($tmpName,'rb');
  2762. if ($fp){
  2763. $data='';
  2764. while(!feof($fp)){
  2765. $data .= fread($fp,1024);
  2766. }
  2767. fclose($fp);
  2768. } else {
  2769. $error = 1;
  2770. $errormsg = 'trouble opening file';
  2771. }
  2772. // $data = fread($fp,filesize($tmpName));
  2773. set_magic_quotes_runtime($tmp);
  2774. // fclose($fp);
  2775. unlink($tmpName);
  2776. $this->addJpegImage_common($data,$x,$y,$w,$h,$imageWidth,$imageHeight);
  2777. }
  2778. /**
  2779. * common code used by the two JPEG adding functions
  2780. *
  2781. * @access private
  2782. */
  2783. function addJpegImage_common(&$data,$x,$y,$w=0,$h=0,$imageWidth,$imageHeight,$channels=3){
  2784. // note that this function is not to be called externally
  2785. // it is just the common code between the GD and the file options
  2786. $this->numImages++;
  2787. $im=$this->numImages;
  2788. $label='I'.$im;
  2789. $this->numObj++;
  2790. $this->o_image($this->numObj,'new',array('label'=>$label,'data'=>$data,'iw'=>$imageWidth,'ih'=>$imageHeight,'channels'=>$channels));
  2791. $this->objects[$this->currentContents]['c'].="\nq";
  2792. $this->objects[$this->currentContents]['c'].="\n".sprintf('%.3f',$w)." 0 0 ".sprintf('%.3f',$h)." ".sprintf('%.3f',$x)." ".sprintf('%.3f',$y)." cm";
  2793. $this->objects[$this->currentContents]['c'].="\n/".$label.' Do';
  2794. $this->objects[$this->currentContents]['c'].="\nQ";
  2795. }
  2796. /**
  2797. * specify where the document should open when it first starts
  2798. */
  2799. function openHere($style,$a=0,$b=0,$c=0){
  2800. // this function will open the document at a specified page, in a specified style
  2801. // the values for style, and the required paramters are:
  2802. // 'XYZ' left, top, zoom
  2803. // 'Fit'
  2804. // 'FitH' top
  2805. // 'FitV' left
  2806. // 'FitR' left,bottom,right
  2807. // 'FitB'
  2808. // 'FitBH' top
  2809. // 'FitBV' left
  2810. $this->numObj++;
  2811. $this->o_destination($this->numObj,'new',array('page'=>$this->currentPage,'type'=>$style,'p1'=>$a,'p2'=>$b,'p3'=>$c));
  2812. $id = $this->catalogId;
  2813. $this->o_catalog($id,'openHere',$this->numObj);
  2814. }
  2815. /**
  2816. * create a labelled destination within the document
  2817. */
  2818. function addDestination($label,$style,$a=0,$b=0,$c=0){
  2819. // associates the given label with the destination, it is done this way so that a destination can be specified after
  2820. // it has been linked to
  2821. // styles are the same as the 'openHere' function
  2822. $this->numObj++;
  2823. $this->o_destination($this->numObj,'new',array('page'=>$this->currentPage,'type'=>$style,'p1'=>$a,'p2'=>$b,'p3'=>$c));
  2824. $id = $this->numObj;
  2825. // store the label->idf relationship, note that this means that labels can be used only once
  2826. $this->destinations["$label"]=$id;
  2827. }
  2828. /**
  2829. * define font families, this is used to initialize the font families for the default fonts
  2830. * and for the user to add new ones for their fonts. The default bahavious can be overridden should
  2831. * that be desired.
  2832. */
  2833. function setFontFamily($family,$options=''){
  2834. if (!is_array($options)){
  2835. if ($family=='init'){
  2836. // set the known family groups
  2837. // these font families will be used to enable bold and italic markers to be included
  2838. // within text streams. html forms will be used... <b></b> <i></i>
  2839. $this->fontFamilies['Helvetica.afm']=array(
  2840. 'b'=>'Helvetica-Bold.afm'
  2841. ,'i'=>'Helvetica-Oblique.afm'
  2842. ,'bi'=>'Helvetica-BoldOblique.afm'
  2843. ,'ib'=>'Helvetica-BoldOblique.afm'
  2844. );
  2845. $this->fontFamilies['Courier.afm']=array(
  2846. 'b'=>'Courier-Bold.afm'
  2847. ,'i'=>'Courier-Oblique.afm'
  2848. ,'bi'=>'Courier-BoldOblique.afm'
  2849. ,'ib'=>'Courier-BoldOblique.afm'
  2850. );
  2851. $this->fontFamilies['Times-Roman.afm']=array(
  2852. 'b'=>'Times-Bold.afm'
  2853. ,'i'=>'Times-Italic.afm'
  2854. ,'bi'=>'Times-BoldItalic.afm'
  2855. ,'ib'=>'Times-BoldItalic.afm'
  2856. );
  2857. }
  2858. } else {
  2859. // the user is trying to set a font family
  2860. // note that this can also be used to set the base ones to something else
  2861. if (strlen($family)){
  2862. $this->fontFamilies[$family] = $options;
  2863. }
  2864. }
  2865. }
  2866. /**
  2867. * used to add messages for use in debugging
  2868. */
  2869. function addMessage($message){
  2870. $this->messages.=$message."\n";
  2871. }
  2872. /**
  2873. * a few functions which should allow the document to be treated transactionally.
  2874. */
  2875. function transaction($action){
  2876. switch ($action){
  2877. case 'start':
  2878. // store all the data away into the checkpoint variable
  2879. $data = get_object_vars($this);
  2880. $this->checkpoint = $data;
  2881. unset($data);
  2882. break;
  2883. case 'commit':
  2884. if (is_array($this->checkpoint) && isset($this->checkpoint['checkpoint'])){
  2885. $tmp = $this->checkpoint['checkpoint'];
  2886. $this->checkpoint = $tmp;
  2887. unset($tmp);
  2888. } else {
  2889. $this->checkpoint='';
  2890. }
  2891. break;
  2892. case 'rewind':
  2893. // do not destroy the current checkpoint, but move us back to the state then, so that we can try again
  2894. if (is_array($this->checkpoint)){
  2895. // can only abort if were inside a checkpoint
  2896. $tmp = $this->checkpoint;
  2897. foreach ($tmp as $k=>$v){
  2898. if ($k != 'checkpoint'){
  2899. $this->$k=$v;
  2900. }
  2901. }
  2902. unset($tmp);
  2903. }
  2904. break;
  2905. case 'abort':
  2906. if (is_array($this->checkpoint)){
  2907. // can only abort if were inside a checkpoint
  2908. $tmp = $this->checkpoint;
  2909. foreach ($tmp as $k=>$v){
  2910. $this->$k=$v;
  2911. }
  2912. unset($tmp);
  2913. }
  2914. break;
  2915. }
  2916. }
  2917. } // end of class
  2918. ?>