PageRenderTime 60ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/includes/class.ezpdf.php

https://bitbucket.org/dgough/annamaria-daneswood-25102012
PHP | 1555 lines | 1125 code | 153 blank | 277 comment | 284 complexity | ff5377c902545fbde188dbd49947eb9c MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1

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

  1. <?php
  2. include_once('includes/class.pdf.php');
  3. class Cezpdf extends Cpdf {
  4. //==============================================================================
  5. // this class will take the basic interaction facilities of the Cpdf class
  6. // and make more useful functions so that the user does not have to
  7. // know all the ins and outs of pdf presentation to produce something pretty.
  8. //
  9. // IMPORTANT NOTE
  10. // there is no warranty, implied or otherwise with this software.
  11. //
  12. // version 009 (versioning is linked to class.pdf.php)
  13. //
  14. // released under a public domain licence.
  15. //
  16. // Wayne Munro, R&OS Ltd, http://www.ros.co.nz/pdf
  17. //==============================================================================
  18. var $ez=array('fontSize'=>10); // used for storing most of the page configuration parameters
  19. var $y; // this is the current vertical positon on the page of the writing point, very important
  20. var $ezPages=array(); // keep an array of the ids of the pages, making it easy to go back and add page numbers etc.
  21. var $ezPageCount=0;
  22. // ------------------------------------------------------------------------------
  23. function Cezpdf($paper='a4',$orientation='portrait'){
  24. // Assuming that people don't want to specify the paper size using the absolute coordinates
  25. // allow a couple of options:
  26. // orientation can be 'portrait' or 'landscape'
  27. // or, to actually set the coordinates, then pass an array in as the first parameter.
  28. // the defaults are as shown.
  29. //
  30. // -------------------------
  31. // 2002-07-24 - Nicola Asuni (info@tecnick.com):
  32. // Added new page formats (45 standard ISO paper formats and 4 american common formats)
  33. // paper cordinates are calculated in this way: (inches * 72) where 1 inch = 2.54 cm
  34. //
  35. // Now you may also pass a 2 values array containing the page width and height in centimeters
  36. // -------------------------
  37. if (!is_array($paper)){
  38. switch (strtoupper($paper)){
  39. case '4A0': {$size = array(0,0,4767.87,6740.79); break;}
  40. case '2A0': {$size = array(0,0,3370.39,4767.87); break;}
  41. case 'A0': {$size = array(0,0,2383.94,3370.39); break;}
  42. case 'A1': {$size = array(0,0,1683.78,2383.94); break;}
  43. case 'A2': {$size = array(0,0,1190.55,1683.78); break;}
  44. case 'A3': {$size = array(0,0,841.89,1190.55); break;}
  45. case 'A4': default: {$size = array(0,0,595.28,841.89); break;}
  46. case 'A5': {$size = array(0,0,419.53,595.28); break;}
  47. case 'A6': {$size = array(0,0,297.64,419.53); break;}
  48. case 'A7': {$size = array(0,0,209.76,297.64); break;}
  49. case 'A8': {$size = array(0,0,147.40,209.76); break;}
  50. case 'A9': {$size = array(0,0,104.88,147.40); break;}
  51. case 'A10': {$size = array(0,0,73.70,104.88); break;}
  52. case 'B0': {$size = array(0,0,2834.65,4008.19); break;}
  53. case 'B1': {$size = array(0,0,2004.09,2834.65); break;}
  54. case 'B2': {$size = array(0,0,1417.32,2004.09); break;}
  55. case 'B3': {$size = array(0,0,1000.63,1417.32); break;}
  56. case 'B4': {$size = array(0,0,708.66,1000.63); break;}
  57. case 'B5': {$size = array(0,0,498.90,708.66); break;}
  58. case 'B6': {$size = array(0,0,354.33,498.90); break;}
  59. case 'B7': {$size = array(0,0,249.45,354.33); break;}
  60. case 'B8': {$size = array(0,0,175.75,249.45); break;}
  61. case 'B9': {$size = array(0,0,124.72,175.75); break;}
  62. case 'B10': {$size = array(0,0,87.87,124.72); break;}
  63. case 'C0': {$size = array(0,0,2599.37,3676.54); break;}
  64. case 'C1': {$size = array(0,0,1836.85,2599.37); break;}
  65. case 'C2': {$size = array(0,0,1298.27,1836.85); break;}
  66. case 'C3': {$size = array(0,0,918.43,1298.27); break;}
  67. case 'C4': {$size = array(0,0,649.13,918.43); break;}
  68. case 'C5': {$size = array(0,0,459.21,649.13); break;}
  69. case 'C6': {$size = array(0,0,323.15,459.21); break;}
  70. case 'C7': {$size = array(0,0,229.61,323.15); break;}
  71. case 'C8': {$size = array(0,0,161.57,229.61); break;}
  72. case 'C9': {$size = array(0,0,113.39,161.57); break;}
  73. case 'C10': {$size = array(0,0,79.37,113.39); break;}
  74. case 'RA0': {$size = array(0,0,2437.80,3458.27); break;}
  75. case 'RA1': {$size = array(0,0,1729.13,2437.80); break;}
  76. case 'RA2': {$size = array(0,0,1218.90,1729.13); break;}
  77. case 'RA3': {$size = array(0,0,864.57,1218.90); break;}
  78. case 'RA4': {$size = array(0,0,609.45,864.57); break;}
  79. case 'SRA0': {$size = array(0,0,2551.18,3628.35); break;}
  80. case 'SRA1': {$size = array(0,0,1814.17,2551.18); break;}
  81. case 'SRA2': {$size = array(0,0,1275.59,1814.17); break;}
  82. case 'SRA3': {$size = array(0,0,907.09,1275.59); break;}
  83. case 'SRA4': {$size = array(0,0,637.80,907.09); break;}
  84. case 'LETTER': {$size = array(0,0,612.00,792.00); break;}
  85. case 'LEGAL': {$size = array(0,0,612.00,1008.00); break;}
  86. case 'EXECUTIVE': {$size = array(0,0,521.86,756.00); break;}
  87. case 'FOLIO': {$size = array(0,0,612.00,936.00); break;}
  88. }
  89. switch (strtolower($orientation)){
  90. case 'landscape':
  91. $a=$size[3];
  92. $size[3]=$size[2];
  93. $size[2]=$a;
  94. break;
  95. }
  96. } else {
  97. if (count($paper)>2) {
  98. // then an array was sent it to set the size
  99. $size = $paper;
  100. }
  101. else { //size in centimeters has been passed
  102. $size[0] = 0;
  103. $size[1] = 0;
  104. $size[2] = ( $paper[0] / 2.54 ) * 72;
  105. $size[3] = ( $paper[1] / 2.54 ) * 72;
  106. }
  107. }
  108. $this->Cpdf($size);
  109. $this->ez['pageWidth']=$size[2];
  110. $this->ez['pageHeight']=$size[3];
  111. // also set the margins to some reasonable defaults
  112. $this->ez['topMargin']=30;
  113. $this->ez['bottomMargin']=30;
  114. $this->ez['leftMargin']=30;
  115. $this->ez['rightMargin']=30;
  116. // set the current writing position to the top of the first page
  117. $this->y = $this->ez['pageHeight']-$this->ez['topMargin'];
  118. // and get the ID of the page that was created during the instancing process.
  119. $this->ezPages[1]=$this->getFirstPageId();
  120. $this->ezPageCount=1;
  121. }
  122. // ------------------------------------------------------------------------------
  123. // 2002-07-24: Nicola Asuni (info@tecnick.com)
  124. // Set Margins in centimeters
  125. function ezSetCmMargins($top,$bottom,$left,$right){
  126. $top = ( $top / 2.54 ) * 72;
  127. $bottom = ( $bottom / 2.54 ) * 72;
  128. $left = ( $left / 2.54 ) * 72;
  129. $right = ( $right / 2.54 ) * 72;
  130. $this->ezSetMargins($top,$bottom,$left,$right);
  131. }
  132. // ------------------------------------------------------------------------------
  133. function ezColumnsStart($options=array()){
  134. // start from the current y-position, make the set number of columne
  135. if (isset($this->ez['columns']) && $this->ez['columns']==1){
  136. // if we are already in a column mode then just return.
  137. return;
  138. }
  139. $def=array('gap'=>10,'num'=>2);
  140. foreach($def as $k=>$v){
  141. if (!isset($options[$k])){
  142. $options[$k]=$v;
  143. }
  144. }
  145. // setup the columns
  146. $this->ez['columns']=array('on'=>1,'colNum'=>1);
  147. // store the current margins
  148. $this->ez['columns']['margins']=array(
  149. $this->ez['leftMargin']
  150. ,$this->ez['rightMargin']
  151. ,$this->ez['topMargin']
  152. ,$this->ez['bottomMargin']
  153. );
  154. // and store the settings for the columns
  155. $this->ez['columns']['options']=$options;
  156. // then reset the margins to suit the new columns
  157. // safe enough to assume the first column here, but start from the current y-position
  158. $this->ez['topMargin']=$this->ez['pageHeight']-$this->y;
  159. $width=($this->ez['pageWidth']-$this->ez['leftMargin']-$this->ez['rightMargin']-($options['num']-1)*$options['gap'])/$options['num'];
  160. $this->ez['columns']['width']=$width;
  161. $this->ez['rightMargin']=$this->ez['pageWidth']-$this->ez['leftMargin']-$width;
  162. }
  163. // ------------------------------------------------------------------------------
  164. function ezColumnsStop(){
  165. if (isset($this->ez['columns']) && $this->ez['columns']['on']==1){
  166. $this->ez['columns']['on']=0;
  167. $this->ez['leftMargin']=$this->ez['columns']['margins'][0];
  168. $this->ez['rightMargin']=$this->ez['columns']['margins'][1];
  169. $this->ez['topMargin']=$this->ez['columns']['margins'][2];
  170. $this->ez['bottomMargin']=$this->ez['columns']['margins'][3];
  171. }
  172. }
  173. // ------------------------------------------------------------------------------
  174. function ezInsertMode($status=1,$pageNum=1,$pos='before'){
  175. // puts the document into insert mode. new pages are inserted until this is re-called with status=0
  176. // by default pages wil be inserted at the start of the document
  177. switch($status){
  178. case '1':
  179. if (isset($this->ezPages[$pageNum])){
  180. $this->ez['insertMode']=1;
  181. $this->ez['insertOptions']=array('id'=>$this->ezPages[$pageNum],'pos'=>$pos);
  182. }
  183. break;
  184. case '0':
  185. $this->ez['insertMode']=0;
  186. break;
  187. }
  188. }
  189. // ------------------------------------------------------------------------------
  190. function ezNewPage(){
  191. $pageRequired=1;
  192. if (isset($this->ez['columns']) && $this->ez['columns']['on']==1){
  193. // check if this is just going to a new column
  194. // increment the column number
  195. //echo 'HERE<br>';
  196. $this->ez['columns']['colNum']++;
  197. //echo $this->ez['columns']['colNum'].'<br>';
  198. if ($this->ez['columns']['colNum'] <= $this->ez['columns']['options']['num']){
  199. // then just reset to the top of the next column
  200. $pageRequired=0;
  201. } else {
  202. $this->ez['columns']['colNum']=1;
  203. $this->ez['topMargin']=$this->ez['columns']['margins'][2];
  204. }
  205. $width = $this->ez['columns']['width'];
  206. $this->ez['leftMargin']=$this->ez['columns']['margins'][0]+($this->ez['columns']['colNum']-1)*($this->ez['columns']['options']['gap']+$width);
  207. $this->ez['rightMargin']=$this->ez['pageWidth']-$this->ez['leftMargin']-$width;
  208. }
  209. //echo 'left='.$this->ez['leftMargin'].' right='.$this->ez['rightMargin'].'<br>';
  210. if ($pageRequired){
  211. // make a new page, setting the writing point back to the top
  212. $this->y = $this->ez['pageHeight']-$this->ez['topMargin'];
  213. // make the new page with a call to the basic class.
  214. $this->ezPageCount++;
  215. if (isset($this->ez['insertMode']) && $this->ez['insertMode']==1){
  216. $id = $this->ezPages[$this->ezPageCount] = $this->newPage(1,$this->ez['insertOptions']['id'],$this->ez['insertOptions']['pos']);
  217. // then manipulate the insert options so that inserted pages follow each other
  218. $this->ez['insertOptions']['id']=$id;
  219. $this->ez['insertOptions']['pos']='after';
  220. } else {
  221. $this->ezPages[$this->ezPageCount] = $this->newPage();
  222. }
  223. } else {
  224. $this->y = $this->ez['pageHeight']-$this->ez['topMargin'];
  225. }
  226. }
  227. // ------------------------------------------------------------------------------
  228. function ezSetMargins($top,$bottom,$left,$right){
  229. // sets the margins to new values
  230. $this->ez['topMargin']=$top;
  231. $this->ez['bottomMargin']=$bottom;
  232. $this->ez['leftMargin']=$left;
  233. $this->ez['rightMargin']=$right;
  234. // check to see if this means that the current writing position is outside the
  235. // writable area
  236. if ($this->y > $this->ez['pageHeight']-$top){
  237. // then move y down
  238. $this->y = $this->ez['pageHeight']-$top;
  239. }
  240. if ( $this->y < $bottom){
  241. // then make a new page
  242. $this->ezNewPage();
  243. }
  244. }
  245. // ------------------------------------------------------------------------------
  246. function ezGetCurrentPageNumber(){
  247. // return the strict numbering (1,2,3,4..) number of the current page
  248. return $this->ezPageCount;
  249. }
  250. // ------------------------------------------------------------------------------
  251. function ezStartPageNumbers($x,$y,$size,$pos='left',$pattern='{PAGENUM} of {TOTALPAGENUM}',$num=''){
  252. // put page numbers on the pages from here.
  253. // place then on the 'pos' side of the coordinates (x,y).
  254. // pos can be 'left' or 'right'
  255. // use the given 'pattern' for display, where (PAGENUM} and {TOTALPAGENUM} are replaced
  256. // as required.
  257. // if $num is set, then make the first page this number, the number of total pages will
  258. // be adjusted to account for this.
  259. // Adjust this function so that each time you 'start' page numbers then you effectively start a different batch
  260. // return the number of the batch, so that they can be stopped in a different order if required.
  261. if (!$pos || !strlen($pos)){
  262. $pos='left';
  263. }
  264. if (!$pattern || !strlen($pattern)){
  265. $pattern='{PAGENUM} of {TOTALPAGENUM}';
  266. }
  267. if (!isset($this->ez['pageNumbering'])){
  268. $this->ez['pageNumbering']=array();
  269. }
  270. $i = count($this->ez['pageNumbering']);
  271. $this->ez['pageNumbering'][$i][$this->ezPageCount]=array('x'=>$x,'y'=>$y,'pos'=>$pos,'pattern'=>$pattern,'num'=>$num,'size'=>$size);
  272. return $i;
  273. }
  274. // ------------------------------------------------------------------------------
  275. function ezWhatPageNumber($pageNum,$i=0){
  276. // given a particular generic page number (ie, document numbered sequentially from beginning),
  277. // return the page number under a particular page numbering scheme ($i)
  278. $num=0;
  279. $start=1;
  280. $startNum=1;
  281. if (!isset($this->ez['pageNumbering']))
  282. {
  283. $this->addMessage('WARNING: page numbering called for and wasn\'t started with ezStartPageNumbers');
  284. return 0;
  285. }
  286. foreach($this->ez['pageNumbering'][$i] as $k=>$v){
  287. if ($k<=$pageNum){
  288. if (is_array($v)){
  289. // start block
  290. if (strlen($v['num'])){
  291. // a start was specified
  292. $start=$v['num'];
  293. $startNum=$k;
  294. $num=$pageNum-$startNum+$start;
  295. }
  296. } else {
  297. // stop block
  298. $num=0;
  299. }
  300. }
  301. }
  302. return $num;
  303. }
  304. // ------------------------------------------------------------------------------
  305. function ezStopPageNumbers($stopTotal=0,$next=0,$i=0){
  306. // if stopTotal=1 then the totalling of pages for this number will stop too
  307. // if $next=1, then do this page, but not the next, else do not do this page either
  308. // if $i is set, then stop that particular pagenumbering sequence.
  309. if (!isset($this->ez['pageNumbering'])){
  310. $this->ez['pageNumbering']=array();
  311. }
  312. if ($next && isset($this->ez['pageNumbering'][$i][$this->ezPageCount]) && is_array($this->ez['pageNumbering'][$i][$this->ezPageCount])){
  313. // then this has only just been started, this will over-write the start, and nothing will appear
  314. // add a special command to the start block, telling it to stop as well
  315. if ($stopTotal){
  316. $this->ez['pageNumbering'][$i][$this->ezPageCount]['stoptn']=1;
  317. } else {
  318. $this->ez['pageNumbering'][$i][$this->ezPageCount]['stopn']=1;
  319. }
  320. } else {
  321. if ($stopTotal){
  322. $this->ez['pageNumbering'][$i][$this->ezPageCount]='stopt';
  323. } else {
  324. $this->ez['pageNumbering'][$i][$this->ezPageCount]='stop';
  325. }
  326. if ($next){
  327. $this->ez['pageNumbering'][$i][$this->ezPageCount].='n';
  328. }
  329. }
  330. }
  331. // ------------------------------------------------------------------------------
  332. function ezPRVTpageNumberSearch($lbl,&$tmp){
  333. foreach($tmp as $i=>$v){
  334. if (is_array($v)){
  335. if (isset($v[$lbl])){
  336. return $i;
  337. }
  338. } else {
  339. if ($v==$lbl){
  340. return $i;
  341. }
  342. }
  343. }
  344. return 0;
  345. }
  346. // ------------------------------------------------------------------------------
  347. function ezPRVTaddPageNumbers(){
  348. // this will go through the pageNumbering array and add the page numbers are required
  349. if (isset($this->ez['pageNumbering'])){
  350. $totalPages1 = $this->ezPageCount;
  351. $tmp1=$this->ez['pageNumbering'];
  352. $status=0;
  353. foreach($tmp1 as $i=>$tmp){
  354. // do each of the page numbering systems
  355. // firstly, find the total pages for this one
  356. $k = $this->ezPRVTpageNumberSearch('stopt',$tmp);
  357. if ($k && $k>0){
  358. $totalPages = $k-1;
  359. } else {
  360. $l = $this->ezPRVTpageNumberSearch('stoptn',$tmp);
  361. if ($l && $l>0){
  362. $totalPages = $l;
  363. } else {
  364. $totalPages = $totalPages1;
  365. }
  366. }
  367. foreach ($this->ezPages as $pageNum=>$id){
  368. if (isset($tmp[$pageNum])){
  369. if (is_array($tmp[$pageNum])){
  370. // then this must be starting page numbers
  371. $status=1;
  372. $info = $tmp[$pageNum];
  373. $info['dnum']=$info['num']-$pageNum;
  374. // also check for the special case of the numbering stopping and starting on the same page
  375. if (isset($info['stopn']) || isset($info['stoptn']) ){
  376. $status=2;
  377. }
  378. } else if ($tmp[$pageNum]=='stop' || $tmp[$pageNum]=='stopt'){
  379. // then we are stopping page numbers
  380. $status=0;
  381. } else if ($status==1 && ($tmp[$pageNum]=='stoptn' || $tmp[$pageNum]=='stopn')){
  382. // then we are stopping page numbers
  383. $status=2;
  384. }
  385. }
  386. if ($status){
  387. // then add the page numbering to this page
  388. if (strlen($info['num'])){
  389. $num=$pageNum+$info['dnum'];
  390. } else {
  391. $num=$pageNum;
  392. }
  393. $total = $totalPages+$num-$pageNum;
  394. $pat = str_replace('{PAGENUM}',$num,$info['pattern']);
  395. $pat = str_replace('{TOTALPAGENUM}',$total,$pat);
  396. $this->reopenObject($id);
  397. switch($info['pos']){
  398. case 'right':
  399. $this->addText($info['x'],$info['y'],$info['size'],$pat);
  400. break;
  401. default:
  402. $w=$this->getTextWidth($info['size'],$pat);
  403. $this->addText($info['x']-$w,$info['y'],$info['size'],$pat);
  404. break;
  405. }
  406. $this->closeObject();
  407. }
  408. if ($status==2){
  409. $status=0;
  410. }
  411. }
  412. }
  413. }
  414. }
  415. // ------------------------------------------------------------------------------
  416. function ezPRVTcleanUp(){
  417. $this->ezPRVTaddPageNumbers();
  418. }
  419. // ------------------------------------------------------------------------------
  420. function ezStream($options=''){
  421. $this->ezPRVTcleanUp();
  422. $this->stream($options);
  423. }
  424. // ------------------------------------------------------------------------------
  425. function ezOutput($options=0){
  426. $this->ezPRVTcleanUp();
  427. return $this->output($options);
  428. }
  429. // ------------------------------------------------------------------------------
  430. function ezSetY($y){
  431. // used to change the vertical position of the writing point.
  432. $this->y = $y;
  433. if ( $this->y < $this->ez['bottomMargin']){
  434. // then make a new page
  435. $this->ezNewPage();
  436. }
  437. }
  438. // ------------------------------------------------------------------------------
  439. function ezSetDy($dy,$mod=''){
  440. // used to change the vertical position of the writing point.
  441. // changes up by a positive increment, so enter a negative number to go
  442. // down the page
  443. // if $mod is set to 'makeSpace' and a new page is forced, then the pointed will be moved
  444. // down on the new page, this will allow space to be reserved for graphics etc.
  445. $this->y += $dy;
  446. if ( $this->y < $this->ez['bottomMargin']){
  447. // then make a new page
  448. $this->ezNewPage();
  449. if ($mod=='makeSpace'){
  450. $this->y += $dy;
  451. }
  452. }
  453. }
  454. // ------------------------------------------------------------------------------
  455. function ezPrvtTableDrawLines($pos,$gap,$x0,$x1,$y0,$y1,$y2,$col,$inner,$outer,$opt=1){
  456. $x0=1000;
  457. $x1=0;
  458. $this->setStrokeColor($col[0],$col[1],$col[2]);
  459. $cnt=0;
  460. $n = count($pos);
  461. foreach($pos as $x){
  462. $cnt++;
  463. if ($cnt==1 || $cnt==$n){
  464. $this->setLineStyle($outer);
  465. } else {
  466. $this->setLineStyle($inner);
  467. }
  468. $this->line($x-$gap/2,$y0,$x-$gap/2,$y2);
  469. if ($x>$x1){ $x1=$x; };
  470. if ($x<$x0){ $x0=$x; };
  471. }
  472. $this->setLineStyle($outer);
  473. $this->line($x0-$gap/2-$outer/2,$y0,$x1-$gap/2+$outer/2,$y0);
  474. // only do the second line if it is different to the first, AND each row does not have
  475. // a line on it.
  476. if ($y0!=$y1 && $opt<2){
  477. $this->line($x0-$gap/2,$y1,$x1-$gap/2,$y1);
  478. }
  479. $this->line($x0-$gap/2-$outer/2,$y2,$x1-$gap/2+$outer/2,$y2);
  480. }
  481. // ------------------------------------------------------------------------------
  482. function ezPrvtTableColumnHeadings($cols,$pos,$maxWidth,$height,$decender,$gap,$size,&$y,$optionsAll=array()){
  483. // uses ezText to add the text, and returns the height taken by the largest heading
  484. // this page will move the headings to a new page if they will not fit completely on this one
  485. // transaction support will be used to implement this
  486. if (isset($optionsAll['cols'])){
  487. $options = $optionsAll['cols'];
  488. } else {
  489. $options = array();
  490. }
  491. $mx=0;
  492. $startPage = $this->ezPageCount;
  493. $secondGo=0;
  494. // $y is the position at which the top of the table should start, so the base
  495. // of the first text, is $y-$height-$gap-$decender, but ezText starts by dropping $height
  496. // the return from this function is the total cell height, including gaps, and $y is adjusted
  497. // to be the postion of the bottom line
  498. // begin the transaction
  499. $this->transaction('start');
  500. $ok=0;
  501. // $y-=$gap-$decender;
  502. $y-=$gap;
  503. while ($ok==0){
  504. foreach($cols as $colName=>$colHeading){
  505. $this->ezSetY($y);
  506. if (isset($options[$colName]) && isset($options[$colName]['justification'])){
  507. $justification = $options[$colName]['justification'];
  508. } else {
  509. $justification = 'left';
  510. }
  511. $this->ezText($colHeading,$size,array('aleft'=> $pos[$colName],'aright'=>($maxWidth[$colName]+$pos[$colName]),'justification'=>$justification));
  512. $dy = $y-$this->y;
  513. if ($dy>$mx){
  514. $mx=$dy;
  515. }
  516. }
  517. $y = $y - $mx - $gap + $decender;
  518. // $y -= $mx-$gap+$decender;
  519. // now, if this has moved to a new page, then abort the transaction, move to a new page, and put it there
  520. // do not check on the second time around, to avoid an infinite loop
  521. if ($this->ezPageCount != $startPage && $secondGo==0){
  522. $this->transaction('rewind');
  523. $this->ezNewPage();
  524. $y = $this->y - $gap-$decender;
  525. $ok=0;
  526. $secondGo=1;
  527. // $y = $store_y;
  528. $mx=0;
  529. } else {
  530. $this->transaction('commit');
  531. $ok=1;
  532. }
  533. }
  534. return $mx+$gap*2-$decender;
  535. }
  536. // ------------------------------------------------------------------------------
  537. function ezPrvtGetTextWidth($size,$text){
  538. // will calculate the maximum width, taking into account that the text may be broken
  539. // by line breaks.
  540. $mx=0;
  541. $lines = explode("\n",$text);
  542. foreach ($lines as $line){
  543. $w = $this->getTextWidth($size,$line);
  544. if ($w>$mx){
  545. $mx=$w;
  546. }
  547. }
  548. return $mx;
  549. }
  550. // ------------------------------------------------------------------------------
  551. function ezTable(&$data,$cols='',$title='',$options=''){
  552. // add a table of information to the pdf document
  553. // $data is a two dimensional array
  554. // $cols (optional) is an associative array, the keys are the names of the columns from $data
  555. // to be presented (and in that order), the values are the titles to be given to the columns
  556. // $title (optional) is the title to be put on the top of the table
  557. //
  558. // $options is an associative array which can contain:
  559. // 'showLines'=> 0,1,2, default is 1 (show outside and top lines only), 2=> lines on each row
  560. // 'showHeadings' => 0 or 1
  561. // 'shaded'=> 0,1,2,3 default is 1 (1->alternate lines are shaded, 0->no shading, 2-> both shaded, second uses shadeCol2)
  562. // 'shadeCol' => (r,g,b) array, defining the colour of the shading, default is (0.8,0.8,0.8)
  563. // 'shadeCol2' => (r,g,b) array, defining the colour of the shading of the other blocks, default is (0.7,0.7,0.7)
  564. // 'fontSize' => 10
  565. // 'textCol' => (r,g,b) array, text colour
  566. // 'titleFontSize' => 12
  567. // 'rowGap' => 2 , the space added at the top and bottom of each row, between the text and the lines
  568. // 'colGap' => 5 , the space on the left and right sides of each cell
  569. // 'lineCol' => (r,g,b) array, defining the colour of the lines, default, black.
  570. // 'xPos' => 'left','right','center','centre',or coordinate, reference coordinate in the x-direction
  571. // 'xOrientation' => 'left','right','center','centre', position of the table w.r.t 'xPos'
  572. // 'width'=> <number> which will specify the width of the table, if it turns out to not be this
  573. // wide, then it will stretch the table to fit, if it is wider then each cell will be made
  574. // proportionalty smaller, and the content may have to wrap.
  575. // 'maxWidth'=> <number> similar to 'width', but will only make table smaller than it wants to be
  576. // 'options' => array(<colname>=>array('justification'=>'left','width'=>100,'link'=>linkDataName),<colname>=>....)
  577. // allow the setting of other paramaters for the individual columns
  578. // 'minRowSpace'=> the minimum space between the bottom of each row and the bottom margin, in which a new row will be started
  579. // if it is less, then a new page would be started, default=-100
  580. // 'innerLineThickness'=>1
  581. // 'outerLineThickness'=>1
  582. // 'splitRows'=>0, 0 or 1, whether or not to allow the rows to be split across page boundaries
  583. // 'protectRows'=>number, the number of rows to hold with the heading on page, ie, if there less than this number of
  584. // rows on the page, then move the whole lot onto the next page, default=1
  585. //
  586. // note that the user will have had to make a font selection already or this will not
  587. // produce a valid pdf file.
  588. if (!is_array($data)){
  589. return;
  590. }
  591. if (!is_array($cols)){
  592. // take the columns from the first row of the data set
  593. reset($data);
  594. list($k,$v)=each($data);
  595. if (!is_array($v)){
  596. return;
  597. }
  598. $cols=array();
  599. foreach($v as $k1=>$v1){
  600. $cols[$k1]=$k1;
  601. }
  602. }
  603. if (!is_array($options)){
  604. $options=array();
  605. }
  606. $defaults = array(
  607. 'shaded'=>1,'showLines'=>1,'shadeCol'=>array(0.8,0.8,0.8),'shadeCol2'=>array(0.7,0.7,0.7),'fontSize'=>10,'titleFontSize'=>12
  608. ,'titleGap'=>5,'lineCol'=>array(0,0,0),'gap'=>5,'xPos'=>'centre','xOrientation'=>'centre'
  609. ,'showHeadings'=>1,'textCol'=>array(0,0,0),'width'=>0,'maxWidth'=>0,'cols'=>array(),'minRowSpace'=>-100,'rowGap'=>2,'colGap'=>5
  610. ,'innerLineThickness'=>1,'outerLineThickness'=>1,'splitRows'=>0,'protectRows'=>1
  611. );
  612. foreach($defaults as $key=>$value){
  613. if (is_array($value)){
  614. if (!isset($options[$key]) || !is_array($options[$key])){
  615. $options[$key]=$value;
  616. }
  617. } else {
  618. if (!isset($options[$key])){
  619. $options[$key]=$value;
  620. }
  621. }
  622. }
  623. $options['gap']=2*$options['colGap'];
  624. $middle = ($this->ez['pageWidth']-$this->ez['rightMargin'])/2+($this->ez['leftMargin'])/2;
  625. // figure out the maximum widths of the text within each column
  626. $maxWidth=array();
  627. foreach($cols as $colName=>$colHeading){
  628. $maxWidth[$colName]=0;
  629. }
  630. // find the maximum cell widths based on the data
  631. foreach($data as $row){
  632. foreach($cols as $colName=>$colHeading){
  633. $w = $this->ezPrvtGetTextWidth($options['fontSize'],(string)$row[$colName])*1.01;
  634. if ($w > $maxWidth[$colName]){
  635. $maxWidth[$colName]=$w;
  636. }
  637. }
  638. }
  639. // and the maximum widths to fit in the headings
  640. foreach($cols as $colName=>$colTitle){
  641. $w = $this->ezPrvtGetTextWidth($options['fontSize'],(string)$colTitle)*1.01;
  642. if ($w > $maxWidth[$colName]){
  643. $maxWidth[$colName]=$w;
  644. }
  645. }
  646. // calculate the start positions of each of the columns
  647. $pos=array();
  648. $x=0;
  649. $t=$x;
  650. $adjustmentWidth=0;
  651. $setWidth=0;
  652. foreach($maxWidth as $colName => $w){
  653. $pos[$colName]=$t;
  654. // if the column width has been specified then set that here, also total the
  655. // width avaliable for adjustment
  656. if (isset($options['cols'][$colName]) && isset($options['cols'][$colName]['width']) && $options['cols'][$colName]['width']>0){
  657. $t=$t+$options['cols'][$colName]['width'];
  658. $maxWidth[$colName] = $options['cols'][$colName]['width']-$options['gap'];
  659. $setWidth += $options['cols'][$colName]['width'];
  660. } else {
  661. $t=$t+$w+$options['gap'];
  662. $adjustmentWidth += $w;
  663. $setWidth += $options['gap'];
  664. }
  665. }
  666. $pos['_end_']=$t;
  667. // if maxWidth is specified, and the table is too wide, and the width has not been set,
  668. // then set the width.
  669. if ($options['width']==0 && $options['maxWidth'] && ($t-$x)>$options['maxWidth']){
  670. // then need to make this one smaller
  671. $options['width']=$options['maxWidth'];
  672. }
  673. if ($options['width'] && $adjustmentWidth>0 && $setWidth<$options['width']){
  674. // first find the current widths of the columns involved in this mystery
  675. $cols0 = array();
  676. $cols1 = array();
  677. $xq=0;
  678. $presentWidth=0;
  679. $last='';
  680. foreach($pos as $colName=>$p){
  681. if (!isset($options['cols'][$last]) || !isset($options['cols'][$last]['width']) || $options['cols'][$last]['width']<=0){
  682. if (strlen($last)){
  683. $cols0[$last]=$p-$xq -$options['gap'];
  684. $presentWidth += ($p-$xq - $options['gap']);
  685. }
  686. } else {
  687. $cols1[$last]=$p-$xq;
  688. }
  689. $last=$colName;
  690. $xq=$p;
  691. }
  692. // $cols0 contains the widths of all the columns which are not set
  693. $neededWidth = $options['width']-$setWidth;
  694. // if needed width is negative then add it equally to each column, else get more tricky
  695. if ($presentWidth<$neededWidth){
  696. foreach($cols0 as $colName=>$w){
  697. $cols0[$colName]+= ($neededWidth-$presentWidth)/count($cols0);
  698. }
  699. } else {
  700. $cnt=0;
  701. while ($presentWidth>$neededWidth && $cnt<100){
  702. $cnt++; // insurance policy
  703. // find the widest columns, and the next to widest width
  704. $aWidest = array();
  705. $nWidest=0;
  706. $widest=0;
  707. foreach($cols0 as $colName=>$w){
  708. if ($w>$widest){
  709. $aWidest=array($colName);
  710. $nWidest = $widest;
  711. $widest=$w;
  712. } else if ($w==$widest){
  713. $aWidest[]=$colName;
  714. }
  715. }
  716. // then figure out what the width of the widest columns would have to be to take up all the slack
  717. $newWidestWidth = $widest - ($presentWidth-$neededWidth)/count($aWidest);
  718. if ($newWidestWidth > $nWidest){
  719. // then there is space to set them to this
  720. foreach($aWidest as $colName){
  721. $cols0[$colName] = $newWidestWidth;
  722. }
  723. $presentWidth=$neededWidth;
  724. } else {
  725. // there is not space, reduce the size of the widest ones down to the next size down, and we
  726. // will go round again
  727. foreach($aWidest as $colName){
  728. $cols0[$colName] = $nWidest;
  729. }
  730. $presentWidth=$presentWidth-($widest-$nWidest)*count($aWidest);
  731. }
  732. }
  733. }
  734. // $cols0 now contains the new widths of the constrained columns.
  735. // now need to update the $pos and $maxWidth arrays
  736. $xq=0;
  737. foreach($pos as $colName=>$p){
  738. $pos[$colName]=$xq;
  739. if (!isset($options['cols'][$colName]) || !isset($options['cols'][$colName]['width']) || $options['cols'][$colName]['width']<=0){
  740. if (isset($cols0[$colName])){
  741. $xq += $cols0[$colName] + $options['gap'];
  742. $maxWidth[$colName]=$cols0[$colName];
  743. }
  744. } else {
  745. if (isset($cols1[$colName])){
  746. $xq += $cols1[$colName];
  747. }
  748. }
  749. }
  750. $t=$x+$options['width'];
  751. $pos['_end_']=$t;
  752. }
  753. // now adjust the table to the correct location across the page
  754. switch ($options['xPos']){
  755. case 'left':
  756. $xref = $this->ez['leftMargin'];
  757. break;
  758. case 'right':
  759. $xref = $this->ez['pageWidth'] - $this->ez['rightMargin'];
  760. break;
  761. case 'centre':
  762. case 'center':
  763. $xref = $middle;
  764. break;
  765. default:
  766. $xref = $options['xPos'];
  767. break;
  768. }
  769. switch ($options['xOrientation']){
  770. case 'left':
  771. $dx = $xref-$t;
  772. break;
  773. case 'right':
  774. $dx = $xref;
  775. break;
  776. case 'centre':
  777. case 'center':
  778. $dx = $xref-$t/2;
  779. break;
  780. }
  781. foreach($pos as $k=>$v){
  782. $pos[$k]=$v+$dx;
  783. }
  784. $x0=$x+$dx;
  785. $x1=$t+$dx;
  786. $baseLeftMargin = $this->ez['leftMargin'];
  787. $basePos = $pos;
  788. $baseX0 = $x0;
  789. $baseX1 = $x1;
  790. // ok, just about ready to make me a table
  791. $this->setColor($options['textCol'][0],$options['textCol'][1],$options['textCol'][2]);
  792. $this->setStrokeColor($options['shadeCol'][0],$options['shadeCol'][1],$options['shadeCol'][2]);
  793. $middle = ($x1+$x0)/2;
  794. // start a transaction which will be used to regress the table, if there are not enough rows protected
  795. if ($options['protectRows']>0){
  796. $this->transaction('start');
  797. $movedOnce=0;
  798. }
  799. $abortTable = 1;
  800. while ($abortTable){
  801. $abortTable=0;
  802. $dm = $this->ez['leftMargin']-$baseLeftMargin;
  803. foreach($basePos as $k=>$v){
  804. $pos[$k]=$v+$dm;
  805. }
  806. $x0=$baseX0+$dm;
  807. $x1=$baseX1+$dm;
  808. $middle = ($x1+$x0)/2;
  809. // if the title is set, then do that
  810. if (strlen($title)){
  811. $w = $this->getTextWidth($options['titleFontSize'],$title);
  812. $this->y -= $this->getFontHeight($options['titleFontSize']);
  813. if ($this->y < $this->ez['bottomMargin']){
  814. $this->ezNewPage();
  815. // margins may have changed on the newpage
  816. $dm = $this->ez['leftMargin']-$baseLeftMargin;
  817. foreach($basePos as $k=>$v){
  818. $pos[$k]=$v+$dm;
  819. }
  820. $x0=$baseX0+$dm;
  821. $x1=$baseX1+$dm;
  822. $middle = ($x1+$x0)/2;
  823. $this->y -= $this->getFontHeight($options['titleFontSize']);
  824. }
  825. $this->addText($middle-$w/2,$this->y,$options['titleFontSize'],$title);
  826. $this->y -= $options['titleGap'];
  827. }
  828. // margins may have changed on the newpage
  829. $dm = $this->ez['leftMargin']-$baseLeftMargin;
  830. foreach($basePos as $k=>$v){
  831. $pos[$k]=$v+$dm;
  832. }
  833. $x0=$baseX0+$dm;
  834. $x1=$baseX1+$dm;
  835. $y=$this->y; // to simplify the code a bit
  836. // make the table
  837. $height = $this->getFontHeight($options['fontSize']);
  838. $decender = $this->getFontDecender($options['fontSize']);
  839. $y0=$y+$decender;
  840. $dy=0;
  841. if ($options['showHeadings']){
  842. // this function will move the start of the table to a new page if it does not fit on this one
  843. $headingHeight = $this->ezPrvtTableColumnHeadings($cols,$pos,$maxWidth,$height,$decender,$options['rowGap'],$options['fontSize'],$y,$options);
  844. $y0 = $y+$headingHeight;
  845. $y1 = $y;
  846. $dm = $this->ez['leftMargin']-$baseLeftMargin;
  847. foreach($basePos as $k=>$v){
  848. $pos[$k]=$v+$dm;
  849. }
  850. $x0=$baseX0+$dm;
  851. $x1=$baseX1+$dm;
  852. } else {
  853. $y1 = $y0;
  854. }
  855. $firstLine=1;
  856. // open an object here so that the text can be put in over the shading
  857. if ($options['shaded']){
  858. $this->saveState();
  859. $textObjectId = $this->openObject();
  860. $this->closeObject();
  861. $this->addObject($textObjectId);
  862. $this->reopenObject($textObjectId);
  863. }
  864. $cnt=0;
  865. $newPage=0;
  866. foreach($data as $row){
  867. $cnt++;
  868. // the transaction support will be used to prevent rows being split
  869. if ($options['splitRows']==0){
  870. $pageStart = $this->ezPageCount;
  871. if (isset($this->ez['columns']) && $this->ez['columns']['on']==1){
  872. $columnStart = $this->ez['columns']['colNum'];
  873. }
  874. $this->transaction('start');
  875. $row_orig = $row;
  876. $y_orig = $y;
  877. $y0_orig = $y0;
  878. $y1_orig = $y1;
  879. }
  880. $ok=0;
  881. $secondTurn=0;
  882. while(!$abortTable && $ok == 0){
  883. $mx=0;
  884. $newRow=1;
  885. while(!$abortTable && ($newPage || $newRow)){
  886. $y-=$height;
  887. if ($newPage || $y<$this->ez['bottomMargin'] || (isset($options['minRowSpace']) && $y<($this->ez['bottomMargin']+$options['minRowSpace'])) ){
  888. // check that enough rows are with the heading
  889. if ($options['protectRows']>0 && $movedOnce==0 && $cnt<=$options['protectRows']){
  890. // then we need to move the whole table onto the next page
  891. $movedOnce = 1;
  892. $abortTable = 1;
  893. }
  894. $y2=$y-$mx+2*$height+$decender-$newRow*$height;
  895. if ($options['showLines']){
  896. if (!$options['showHeadings']){
  897. $y0=$y1;
  898. }
  899. $this->ezPrvtTableDrawLines($pos,$options['gap'],$x0,$x1,$y0,$y1,$y2,$options['lineCol'],$options['innerLineThickness'],$options['outerLineThickness'],$options['showLines']);
  900. }
  901. if ($options['shaded']){
  902. $this->closeObject();
  903. $this->restoreState();
  904. }
  905. $this->ezNewPage();
  906. // and the margins may have changed, this is due to the possibility of the columns being turned on
  907. // as the columns are managed by manipulating the margins
  908. $dm = $this->ez['leftMargin']-$baseLeftMargin;
  909. foreach($basePos as $k=>$v){
  910. $pos[$k]=$v+$dm;
  911. }
  912. // $x0=$x0+$dm;
  913. // $x1=$x1+$dm;
  914. $x0=$baseX0+$dm;
  915. $x1=$baseX1+$dm;
  916. if ($options['shaded']){
  917. $this->saveState();
  918. $textObjectId = $this->openObject();
  919. $this->closeObject();
  920. $this->addObject($textObjectId);
  921. $this->reopenObject($textObjectId);
  922. }
  923. $this->setColor($options['textCol'][0],$options['textCol'][1],$options['textCol'][2],1);
  924. $y = $this->ez['pageHeight']-$this->ez['topMargin'];
  925. $y0=$y+$decender;
  926. $mx=0;
  927. if ($options['showHeadings']){
  928. $this->ezPrvtTableColumnHeadings($cols,$pos,$maxWidth,$height,$decender,$options['rowGap'],$options['fontSize'],$y,$options);
  929. $y1=$y;
  930. } else {
  931. $y1=$y0;
  932. }
  933. $firstLine=1;
  934. $y -= $height;
  935. }
  936. $newRow=0;
  937. // write the actual data
  938. // if these cells need to be split over a page, then $newPage will be set, and the remaining
  939. // text will be placed in $leftOvers
  940. $newPage=0;
  941. $leftOvers=array();
  942. foreach($cols as $colName=>$colTitle){
  943. $this->ezSetY($y+$height);
  944. $colNewPage=0;
  945. if (isset($row[$colName])){
  946. if (isset($options['cols'][$colName]) && isset($options['cols'][$colName]['link']) && strlen($options['cols'][$colName]['link'])){
  947. $lines = explode("\n",$row[$colName]);
  948. if (isset($row[$options['cols'][$colName]['link']]) && strlen($row[$options['cols'][$colName]['link']])){
  949. foreach($lines as $k=>$v){
  950. $lines[$k]='<c:alink:'.$row[$options['cols'][$colName]['link']].'>'.$v.'</c:alink>';
  951. }
  952. }
  953. } else {
  954. $lines = explode("\n",$row[$colName]);
  955. }
  956. } else {
  957. $lines = array();
  958. }
  959. $this->y -= $options['rowGap'];
  960. foreach ($lines as $line){
  961. $line = $this->ezProcessText($line);
  962. $start=1;
  963. while (strlen($line) || $start){
  964. $start=0;
  965. if (!$colNewPage){
  966. $this->y=$this->y-$height;
  967. }
  968. if ($this->y < $this->ez['bottomMargin']){
  969. // $this->ezNewPage();
  970. $newPage=1; // whether a new page is required for any of the columns
  971. $colNewPage=1; // whether a new page is required for this column
  972. }
  973. if ($colNewPage){
  974. if (isset($leftOvers[$colName])){
  975. $leftOvers[$colName].="\n".$line;
  976. } else {
  977. $leftOvers[$colName] = $line;
  978. }
  979. $line='';
  980. } else {
  981. if (isset($options['cols'][$colName]) && isset($options['cols'][$colName]['justification']) ){
  982. $just = $options['cols'][$colName]['justification'];
  983. } else {
  984. $just='left';
  985. }
  986. $line=$this->addTextWrap($pos[$colName],$this->y,$maxWidth[$colName],$options['fontSize'],$line,$just);
  987. }
  988. }
  989. }
  990. $dy=$y+$height-$this->y+$options['rowGap'];
  991. if ($dy-$height*$newPage>$mx){
  992. $mx=$dy-$height*$newPage;
  993. }
  994. }
  995. // set $row to $leftOvers so that they will be processed onto the new page
  996. $row = $leftOvers;
  997. // now add the shading underneath
  998. if ($options['shaded'] && $cnt%2==0){
  999. $this->closeObject();
  1000. $this->setColor($options['shadeCol'][0],$options['shadeCol'][1],$options['shadeCol'][2],1);
  1001. $this->filledRectangle($x0-$options['gap']/2,$y+$decender+$height-$mx,$x1-$x0,$mx);
  1002. $this->reopenObject($textObjectId);
  1003. }
  1004. if ($options['shaded']==2 && $cnt%2==1){
  1005. $this->closeObject();
  1006. $this->setColor($options['shadeCol2'][0],$options['shadeCol2'][1],$options['shadeCol2'][2],1);
  1007. $this->filledRectangle($x0-$options['gap']/2,$y+$decender+$height-$mx,$x1-$x0,$mx);
  1008. $this->reopenObject($textObjectId);
  1009. }
  1010. if ($options['showLines']>1){
  1011. // then draw a line on the top of each block
  1012. // $this->closeObject();
  1013. $this->saveState();
  1014. $this->setStrokeColor($options['lineCol'][0],$options['lineCol'][1],$options['lineCol'][2],1);
  1015. // $this->line($x0-$options['gap']/2,$y+$decender+$height-$mx,$x1-$x0,$mx);
  1016. if ($firstLine){
  1017. $this->setLineStyle($options['outerLineThickness']);
  1018. $firstLine=0;
  1019. } else {
  1020. $this->setLineStyle($options['innerLineThickness']);
  1021. }
  1022. $this->line($x0-$options['gap']/2,$y+$decender+$height,$x1-$options['gap']/2,$y+$decender+$height);
  1023. $this->restoreState();
  1024. // $this->reopenObject($textObjectId);
  1025. }
  1026. } // end of while
  1027. $y=$y-$mx+$height;
  1028. // checking row split over pages
  1029. if ($options['splitRows']==0){
  1030. if ( ( ($this->ezPageCount != $pageStart) || (isset($this->ez['columns']) && $this->ez['columns']['on']==1 && $columnStart != $this->ez['columns']['colNum'] )) && $secondTurn==0){
  1031. // then we need to go back and try that again !
  1032. $newPage=1;
  1033. $secondTurn=1;
  1034. $this->transaction('rewind');
  1035. $row = $row_orig;
  1036. $y = $y_orig;
  1037. $y0 = $y0_orig;
  1038. $y1 = $y1_orig;
  1039. $ok=0;
  1040. $dm = $this->ez['leftMargin']-$baseLeftMargin;
  1041. foreach($basePos as $k=>$v){
  1042. $pos[$k]=$v+$dm;
  1043. }
  1044. $x0=$baseX0+$dm;
  1045. $x1=$baseX1+$dm;
  1046. } else {
  1047. $this->transaction('commit');
  1048. $ok=1;
  1049. }
  1050. } else {
  1051. $ok=1; // don't go round the loop if splitting rows is allowed
  1052. }
  1053. } // end of while to check for row splitting
  1054. if ($abortTable){
  1055. if ($ok==0){
  1056. $this->transaction('abort');
  1057. }
  1058. // only the outer transaction should be operational
  1059. $this->transaction('rewind');
  1060. $this->ezNewPage();
  1061. break;
  1062. }
  1063. } // end of foreach ($data as $row)
  1064. } // end of while ($abortTable)
  1065. // table has been put on the page, the rows guarded as required, commit.
  1066. $this->transaction('commit');
  1067. $y2=$y+$decender;
  1068. if ($options['showLines']){
  1069. if (!$options['showHeadings']){
  1070. $y0=$y1;
  1071. }
  1072. $this->ezPrvtTableDrawLines($pos,$options['gap'],$x0,$x1,$y0,$y1,$y2,$options['lineCol'],$options['innerLineThickness'],$options['outerLineThickness'],$options['showLines']);
  1073. }
  1074. // close the object for drawing the text on top
  1075. if ($options['shaded']){
  1076. $this->closeObject();
  1077. $this->restoreState();
  1078. }
  1079. $this->y=$y;
  1080. return $y;
  1081. }
  1082. // ------------------------------------------------------------------------------
  1083. function ezProcessText($text){
  1084. // this function will intially be used to implement underlining support, but could be used for a range of other
  1085. // purposes
  1086. $search = array('<u>','<U>','</u>','</U>');
  1087. $replace = array('<c:uline>','<c:uline>','</c:uline>','</c:uline>');
  1088. return str_replace($search,$replace,$text);
  1089. }
  1090. // ------------------------------------------------------------------------------
  1091. function ezText($text,$size=0,$options=array(),$test=0){
  1092. // this will add a string of text to the document, starting at the current drawing
  1093. // position.
  1094. // it will wrap to keep within the margins, including optional offsets from the left
  1095. // and the right, if $size is not specified, then it will be the last one used, or
  1096. // the default value (12 I think).
  1097. // the text will go to the start of the next line when a return code "\n" is found.
  1098. // possible options are:
  1099. // 'left'=> number, gap to leave from the left margin
  1100. // 'right'=> number, gap to leave from the right margin
  1101. // 'aleft'=> number, absolute left position (overrides 'left')
  1102. // 'aright'=> number, absolute right position (overrides 'right')
  1103. // 'justification' => 'left','right','center','centre','full'
  1104. // only set one of the next two items (leading overrides spacing)
  1105. // 'leading' => number, defines the total height taken by the line, independent of the font height.
  1106. // 'spacing' => a real number, though usually set to one of 1, 1.5, 2 (line spacing as used in word processing)
  1107. // if $test is set then this should just check if the text is going to flow onto a new page or not, returning true or false
  1108. // apply the filtering which will make the underlining function.
  1109. $text = $this->ezProcessText($text);
  1110. $newPage=false;
  1111. $store_y = $this->y;
  1112. if (is_array($options) && isset($options['aleft'])){
  1113. $left=$options['aleft'];
  1114. } else {
  1115. $left = $this->ez['leftMargin'] + ((is_array($options) && isset($options['left']))?$options['left']:0);
  1116. }
  1117. if (is_array($options) && isset($options['aright'])){
  1118. $right=$options['aright'];
  1119. } else {
  1120. $right = $this->ez['pageWidth'] - $this->ez['rightMargin'] - ((is_array($options) && isset($options['right']))?$options['right']:0);
  1121. }
  1122. if ($size<=0){
  1123. $size = $this->ez['fontSize'];
  1124. } else {
  1125. $this->ez['fontSize']=$size;
  1126. }
  1127. if (is_array($options) && isset($options['justification'])){
  1128. $just = $options['justification'];
  1129. } else {
  1130. $just = 'left';
  1131. }
  1132. // modifications to give leading and spacing based on those given by Craig Heydenburg 1/1/02
  1133. if (is_array($options) && isset($options['leading'])) { ## use leading instead of spacing
  1134. $height = $options['leading'];
  1135. } else if (is_array($options) && isset($options['spacing'])) {
  1136. $height = $this->getFontHeight($size) * $options['spacing'];
  1137. } else {
  1138. $height = $this->getFontHeight($size);
  1139. }
  1140. $lines = explode("\n",$text);
  1141. foreach ($lines as $line){
  1142. $start=1;
  1143. while (strlen($line) || $start){
  1144. $start=0;
  1145. $this->y=$this->y-$height;
  1146. if ($this->y < $this->ez['bottomMargin']){
  1147. if ($test){
  1148. $newPage=true;
  1149. } else {
  1150. $this->ezNewPage();
  1151. // and then re-calc the left and right, in case they have changed due to columns
  1152. }
  1153. }
  1154. if (is_array($options) && isset($options['aleft'])){
  1155. $left=$options['aleft'];
  1156. } else {
  1157. $left = $this->ez['leftMargin'] + ((is_array($options) && isset($options['left']))?$options['left']:0);
  1158. }
  1159. if (is_array($options) && isset($options['aright'])){
  1160. $right=$options['aright'];
  1161. } else {
  1162. $right = $this->ez['pageWidth'] - $this->ez['rightMargin'] - ((is_array($options) && isset($options['right']))?$options['right']:0);
  1163. }
  1164. $line=$this->addTextWrap($left,$this->y,$right-$left,$size,$line,$just,0,$test);
  1165. }
  1166. }
  1167. if ($test){
  1168. $this->y=$store_y;
  1169. return $newPage;
  1170. } else {
  1171. return $this->y;
  1172. }
  1173. }
  1174. // ------------------------------------------------------------------------------
  1175. function ezImage($image,$pad = 5,$width = 0,$resize = 'full',$just = 'center',$border = ''){
  1176. //beta ezimage function
  1177. if (stristr($image,'://'))//copy to temp file
  1178. {
  1179. $fp = @fopen($image,"rb");
  1180. while(!feof($fp))
  1181. {
  1182. $cont.= fread($fp,1024);
  1183. }
  1184. fclose($fp);
  1185. $image = tempnam ("/tmp", "php-pdf");
  1186. $fp2 = @fopen($image,"w");
  1187. fwrite($fp2,$cont);
  1188. fclose($fp2);
  1189. $temp = true;
  1190. }
  1191. if (!(file_exists($image))) return false; //return immediately if image file does not exist
  1192. $imageInfo = getimagesize($image);
  1193. switch ($imageInfo[2]){
  1194. case 2:
  1195. $type = "jpeg";
  1196. break;
  1197. case 3:
  1198. $type = "png";
  1199. break;
  1200. default:
  1201. return false; //return if file is not jpg or png
  1202. }
  1203. if ($width == 0) $width = $imageInfo[0]; //set width
  1204. $ratio = $imageInfo[0]/$imageInfo[1];
  1205. //get maximum width of image
  1206. if (isset($this->ez['columns']) && $this->ez['columns']['on'] == 1)
  1207. {
  1208. $bigwidth = $this->ez['columns']['width'] - ($pad * 2);
  1209. }
  1210. else
  1211. {
  1212. $bigwidth = $this->ez['pageWidth'] - ($pad * 2);
  1213. }
  1214. //fix width if larger than maximum or if $resize=full
  1215. if ($resize == 'full' || $resize == 'width' || $width > $bigwidth)
  1216. {
  1217. $width = $bigwidth;
  1218. }
  1219. $height = ($width/$ratio); //set height
  1220. //fix size if runs off page
  1221. if ($height > ($this->y - $this->ez['bottomMargin'] - ($pad * 2)))
  1222. {
  1223. if ($resize != 'full')
  1224. {
  1225. $this->ezNewPage();
  1226. }
  1227. else
  1228. {
  1229. $height = ($this->y - $this->ez['bottomMargin'] - ($pad * 2)); //shrink height
  1230. $width = ($height*$ratio); //fix width
  1231. }
  1232. }
  1233. //fix x-offset if image smaller than bigwidth
  1234. if ($width < $bigwidth)
  1235. {
  1236. //center if justification=center
  1237. if ($just == 'center')
  1238. {
  1239. $offset = ($bigwidth - $width) / 2;
  1240. }
  1241. //move to right if justification=right
  1242. if ($just == 'right')
  1243. {
  1244. $offset = ($bigwidth - $width);
  1245. }
  1246. //leave at left if justification=left
  1247. if ($just == 'left')
  1248. {
  1249. $offset = 0;
  1250. }
  1251. }
  1252. //call appropriate function
  1253. if ($type == "jpeg"){
  1254. $this->addJpegFromFile($image,$this->ez['leftMargin'] + $pad + $offset, $this->y + $this->getFontHeight($this->ez['fontSize']) - $pad - $height,$width);
  1255. }
  1256. if ($type == "png"){
  1257. $this->addPngFromFile($image,$this->ez['leftMargin'] + $pad + $offset, $this->y + $this->getFontHeight($this->ez['fontSize']) - $pad - $height,$width);
  1258. }
  1259. //draw border
  1260. if ($border != '')
  1261. {
  1262. if (!(isset($border['color'])))
  1263. {
  1264. $border['color']['red'] = .5;
  1265. $border['color']['blue'] = .5;
  1266. $border['color']['green'] = .5;
  1267. }
  1268. if (!(isset($border['width']))) $border['width'] = 1;
  1269. if (!(isset($border['cap']))) $border['cap'] = 'round';
  1270. if (!(isset($border['join']))) $border['join'] = 'round';
  1271. $this->setStrokeColor($border['color']['red'],$border['color']['green'],$border['color']['blue']);
  1272. $this->setLineStyle($border['width'],$border['cap'],$border['join']);
  1273. $this->rectangle($this->ez['leftMargin'] + $pad + $offset, $this->y + $this->getFontHeight($this->ez['fontSize']) - $pad - $height,$width,$height);
  1274. }
  1275. // move y below image
  1276. $this->y = $this->y - $pad - $height;
  1277. //remove tempfile for remote images
  1278. if ($temp == true) unlink($image);
  1279. }
  1280. // ------------------------------------------------------------------------------
  1281. // note that templating code is still considered developmental - have not really figured
  1282. // out a good way of doing this yet.
  1283. function loadTemplate($templateFile){
  1284. // this function will load the requested template ($file includes full or relative pathname)
  1285. // the code for the template will be modified to make it name safe, and then stored in
  1286. // an array for later use
  1287. // The id of the template will be returned for the user to operate on it later
  1288. if (!file_exists($templateFile)){
  1289. return -1;
  1290. }
  1291. $code = implode('',file($templateFile));
  1292. if (!strlen($code)){
  1293. return;
  1294. }
  1295. $code = trim($code);
  1296. if (substr($code,0,5)=='<?php'){
  1297. $code = substr($code,5);
  1298. }
  1299. if (substr($code,-2)=='?>'){
  1300. $code = substr($code,0,strlen($code)-2);
  1301. }
  1302. if (isset($this->ez['numTemplates'])){
  1303. $newNum = $this->ez['numTemplates'];
  1304. $this->ez['numTemplates']++;
  1305. } else {
  1306. $newNum=0;
  1307. $this->ez['numTemplates']=1;
  1308. $this->ez['templates']=array();
  1309. }
  1310. $this->ez['templates'][$newNum]['code']=$code;
  1311. return $newNum;
  1312. }
  1313. // ------------------------------------------------------------------------------
  1314. function execTemplate($id,$data=array(),$options=array()){
  1315. // execute the given template on the current document.
  1316. if (!isset($this->ez['templates'][$id])){
  1317. return;
  1318. }
  1319. eval($this->ez['templates'][$id]['code']);
  1320. }
  1321. // ------------------------------------------------------------------------------
  1322. function ilink($info){
  1323. $this->alink($info,1);
  1324. }
  1325. function alink($info,$internal=0){
  1326. // a callback function to support the formation of clickable links within the document
  1327. $lineFactor=0.05; // the thickness of the line as a proportion of the height. also the drop of the line.
  1328. switch($info['status']){
  1329. case 'start':
  1330. case 'sol':
  1331. // the beginning of the link
  1332. // this should contain the URl for the link as the 'p' entry, and will also contain the value of 'nCallback'
  1333. if (!isset($this->ez['links'])){
  1334. $this->ez['links']=array();
  1335. }
  1336. $i = $info['nCallback'];
  1337. $this->ez['links'][$i] = array('x'=>$info['x'],'y'=>$info['y'],'angle'=>$info['angle'],'decender'=>$info['decender'],'height'=>$info['height'],'url'=>$info['p']);
  1338. if ($internal==0){
  1339. $this->saveState();
  1340. $this->setColor(0,0,1);
  1341. $this->setStrokeColor(0,0,1);
  1342. $thick = $info['height']*$lineFactor;
  1343. $this->setLineStyle($thick);
  1344. }
  1345. break;
  1346. case 'end':
  1347. case 'eol':
  1348. // the end of the link
  1349. // assume that it is the most recent opening which has closed
  1350. $i = $info['nCallback'];
  1351. $start = $this->ez['links'][$i];
  1352. // add underlining
  1353. if ($internal){
  1354. $this->addInternalLink($start['url'],$start['x'],$start['y']+$start['decender'],$info['x'],$start['y']+$start['decender']+$start['height']);
  1355. } else {
  1356. $a = deg2rad((float)$start['angle']-90.0);
  1357. $drop = $start['height']*$lineFactor*1.5;
  1358. $dropx = cos($a)*$drop;
  1359. $dropy = -sin($a)*$drop;
  1360. $this->line($start['x']-$dropx,$start['y']-$dropy,$info['x']-$dropx,$info['y']-$dropy);
  1361. $this->addLink($start['url'],$start['x'],$start['y']+$start['decender'],$info['x'],$start['y']+$start['decender']+$start['height']);

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