class.ezpdf.php 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568
  1. <?php
  2. include_once('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. {
  1143. // for each line of text to print
  1144. $start=1;
  1145. while (strlen($line) || $start)
  1146. {
  1147. //while there is still some text to print or if this is the first line
  1148. $start=0;
  1149. $this->y=$this->y-$height; //make Y position depend on font-size
  1150. if ($this->y < $this->ez['bottomMargin'])
  1151. {
  1152. //if this vertical position (Y) is not out of the page
  1153. if ($test)
  1154. {
  1155. //we are on a new page, nothing to do
  1156. $newPage=true;
  1157. }
  1158. else
  1159. {
  1160. //else start new page
  1161. $this->ezNewPage();
  1162. // and then re-calc the left and right, in case they have changed due to columns
  1163. }
  1164. }
  1165. if (is_array($options) && isset($options['aleft'])){
  1166. //there are options and they include left position
  1167. $left=$options['aleft'];
  1168. } else {
  1169. //there are no options, or they don't include left position
  1170. $left = $this->ez['leftMargin'] + ((is_array($options) && isset($options['left']))?$options['left']:0);
  1171. }
  1172. if (is_array($options) && isset($options['aright'])){
  1173. $right=$options['aright'];
  1174. } else {
  1175. $right = $this->ez['pageWidth'] - $this->ez['rightMargin'] - ((is_array($options) && isset($options['right']))?$options['right']:0);
  1176. }
  1177. $line=$this->addTextWrap($left,$this->y,$right-$left,$size,$line,$just,0,$test);
  1178. }
  1179. }
  1180. if ($test){
  1181. $this->y=$store_y;
  1182. return $newPage;
  1183. } else {
  1184. return $this->y;
  1185. }
  1186. }
  1187. // ------------------------------------------------------------------------------
  1188. function ezImage($image,$pad = 5,$width = 0,$resize = 'full',$just = 'center',$border = ''){
  1189. //beta ezimage function
  1190. if (stristr($image,'://'))//copy to temp file
  1191. {
  1192. $fp = @fopen($image,"rb");
  1193. while(!feof($fp))
  1194. {
  1195. $cont.= fread($fp,1024);
  1196. }
  1197. fclose($fp);
  1198. $image = tempnam ("/tmp", "php-pdf");
  1199. $fp2 = @fopen($image,"w");
  1200. fwrite($fp2,$cont);
  1201. fclose($fp2);
  1202. $temp = true;
  1203. }
  1204. if (!(file_exists($image))) return false; //return immediately if image file does not exist
  1205. $imageInfo = getimagesize($image);
  1206. switch ($imageInfo[2]){
  1207. case 2:
  1208. $type = "jpeg";
  1209. break;
  1210. case 3:
  1211. $type = "png";
  1212. break;
  1213. default:
  1214. return false; //return if file is not jpg or png
  1215. }
  1216. if ($width == 0) $width = $imageInfo[0]; //set width
  1217. $ratio = $imageInfo[0]/$imageInfo[1];
  1218. //get maximum width of image
  1219. if (isset($this->ez['columns']) && $this->ez['columns']['on'] == 1)
  1220. {
  1221. $bigwidth = $this->ez['columns']['width'] - ($pad * 2);
  1222. }
  1223. else
  1224. {
  1225. $bigwidth = $this->ez['pageWidth'] - ($pad * 2);
  1226. }
  1227. //fix width if larger than maximum or if $resize=full
  1228. if ($resize == 'full' || $resize == 'width' || $width > $bigwidth)
  1229. {
  1230. $width = $bigwidth;
  1231. }
  1232. $height = ($width/$ratio); //set height
  1233. //fix size if runs off page
  1234. if ($height > ($this->y - $this->ez['bottomMargin'] - ($pad * 2)))
  1235. {
  1236. if ($resize != 'full')
  1237. {
  1238. $this->ezNewPage();
  1239. }
  1240. else
  1241. {
  1242. $height = ($this->y - $this->ez['bottomMargin'] - ($pad * 2)); //shrink height
  1243. $width = ($height*$ratio); //fix width
  1244. }
  1245. }
  1246. //fix x-offset if image smaller than bigwidth
  1247. if ($width < $bigwidth)
  1248. {
  1249. //center if justification=center
  1250. if ($just == 'center')
  1251. {
  1252. $offset = ($bigwidth - $width) / 2;
  1253. }
  1254. //move to right if justification=right
  1255. if ($just == 'right')
  1256. {
  1257. $offset = ($bigwidth - $width);
  1258. }
  1259. //leave at left if justification=left
  1260. if ($just == 'left')
  1261. {
  1262. $offset = 0;
  1263. }
  1264. }
  1265. //call appropriate function
  1266. if ($type == "jpeg"){
  1267. $this->addJpegFromFile($image,$this->ez['leftMargin'] + $pad + $offset, $this->y + $this->getFontHeight($this->ez['fontSize']) - $pad - $height,$width);
  1268. }
  1269. if ($type == "png"){
  1270. $this->addPngFromFile($image,$this->ez['leftMargin'] + $pad + $offset, $this->y + $this->getFontHeight($this->ez['fontSize']) - $pad - $height,$width);
  1271. }
  1272. //draw border
  1273. if ($border != '')
  1274. {
  1275. if (!(isset($border['color'])))
  1276. {
  1277. $border['color']['red'] = .5;
  1278. $border['color']['blue'] = .5;
  1279. $border['color']['green'] = .5;
  1280. }
  1281. if (!(isset($border['width']))) $border['width'] = 1;
  1282. if (!(isset($border['cap']))) $border['cap'] = 'round';
  1283. if (!(isset($border['join']))) $border['join'] = 'round';
  1284. $this->setStrokeColor($border['color']['red'],$border['color']['green'],$border['color']['blue']);
  1285. $this->setLineStyle($border['width'],$border['cap'],$border['join']);
  1286. $this->rectangle($this->ez['leftMargin'] + $pad + $offset, $this->y + $this->getFontHeight($this->ez['fontSize']) - $pad - $height,$width,$height);
  1287. }
  1288. // move y below image
  1289. $this->y = $this->y - $pad - $height;
  1290. //remove tempfile for remote images
  1291. if ($temp == true) unlink($image);
  1292. }
  1293. // ------------------------------------------------------------------------------
  1294. // note that templating code is still considered developmental - have not really figured
  1295. // out a good way of doing this yet.
  1296. function loadTemplate($templateFile){
  1297. // this function will load the requested template ($file includes full or relative pathname)
  1298. // the code for the template will be modified to make it name safe, and then stored in
  1299. // an array for later use
  1300. // The id of the template will be returned for the user to operate on it later
  1301. if (!file_exists($templateFile)){
  1302. return -1;
  1303. }
  1304. $code = implode('',file($templateFile));
  1305. if (!strlen($code)){
  1306. return;
  1307. }
  1308. $code = trim($code);
  1309. if (substr($code,0,5)=='<?php'){
  1310. $code = substr($code,5);
  1311. }
  1312. if (substr($code,-2)=='?>'){
  1313. $code = substr($code,0,strlen($code)-2);
  1314. }
  1315. if (isset($this->ez['numTemplates'])){
  1316. $newNum = $this->ez['numTemplates'];
  1317. $this->ez['numTemplates']++;
  1318. } else {
  1319. $newNum=0;
  1320. $this->ez['numTemplates']=1;
  1321. $this->ez['templates']=array();
  1322. }
  1323. $this->ez['templates'][$newNum]['code']=$code;
  1324. return $newNum;
  1325. }
  1326. // ------------------------------------------------------------------------------
  1327. function execTemplate($id,$data=array(),$options=array()){
  1328. // execute the given template on the current document.
  1329. if (!isset($this->ez['templates'][$id])){
  1330. return;
  1331. }
  1332. eval($this->ez['templates'][$id]['code']);
  1333. }
  1334. // ------------------------------------------------------------------------------
  1335. function ilink($info){
  1336. $this->alink($info,1);
  1337. }
  1338. function alink($info,$internal=0){
  1339. // a callback function to support the formation of clickable links within the document
  1340. $lineFactor=0.05; // the thickness of the line as a proportion of the height. also the drop of the line.
  1341. switch($info['status']){
  1342. case 'start':
  1343. case 'sol':
  1344. // the beginning of the link
  1345. // this should contain the URl for the link as the 'p' entry, and will also contain the value of 'nCallback'
  1346. if (!isset($this->ez['links'])){
  1347. $this->ez['links']=array();
  1348. }
  1349. $i = $info['nCallback'];
  1350. $this->ez['links'][$i] = array('x'=>$info['x'],'y'=>$info['y'],'angle'=>$info['angle'],'decender'=>$info['decender'],'height'=>$info['height'],'url'=>$info['p']);
  1351. if ($internal==0){
  1352. $this->saveState();
  1353. $this->setColor(0,0,1);
  1354. $this->setStrokeColor(0,0,1);
  1355. $thick = $info['height']*$lineFactor;
  1356. $this->setLineStyle($thick);
  1357. }
  1358. break;
  1359. case 'end':
  1360. case 'eol':
  1361. // the end of the link
  1362. // assume that it is the most recent opening which has closed
  1363. $i = $info['nCallback'];
  1364. $start = $this->ez['links'][$i];
  1365. // add underlining
  1366. if ($internal){
  1367. $this->addInternalLink($start['url'],$start['x'],$start['y']+$start['decender'],$info['x'],$start['y']+$start['decender']+$start['height']);
  1368. } else {
  1369. $a = deg2rad((float)$start['angle']-90.0);
  1370. $drop = $start['height']*$lineFactor*1.5;
  1371. $dropx = cos($a)*$drop;
  1372. $dropy = -sin($a)*$drop;
  1373. $this->line($start['x']-$dropx,$start['y']-$dropy,$info['x']-$dropx,$info['y']-$dropy);
  1374. $this->addLink($start['url'],$start['x'],$start['y']+$start['decender'],$info['x'],$start['y']+$start['decender']+$start['height']);
  1375. $this->restoreState();
  1376. }
  1377. break;
  1378. }
  1379. }
  1380. // ------------------------------------------------------------------------------
  1381. function uline($info){
  1382. // a callback function to support underlining
  1383. $lineFactor=0.05; // the thickness of the line as a proportion of the height. also the drop of the line.
  1384. switch($info['status']){
  1385. case 'start':
  1386. case 'sol':
  1387. // the beginning of the underline zone
  1388. if (!isset($this->ez['links'])){
  1389. $this->ez['links']=array();
  1390. }
  1391. $i = $info['nCallback'];
  1392. $this->ez['links'][$i] = array('x'=>$info['x'],'y'=>$info['y'],'angle'=>$info['angle'],'decender'=>$info['decender'],'height'=>$info['height']);
  1393. $this->saveState();
  1394. $thick = $info['height']*$lineFactor;
  1395. $this->setLineStyle($thick);
  1396. break;
  1397. case 'end':
  1398. case 'eol':
  1399. // the end of the link
  1400. // assume that it is the most recent opening which has closed
  1401. $i = $info['nCallback'];
  1402. $start = $this->ez['links'][$i];
  1403. // add underlining
  1404. $a = deg2rad((float)$start['angle']-90.0);
  1405. $drop = $start['height']*$lineFactor*1.5;
  1406. $dropx = cos($a)*$drop;
  1407. $dropy = -sin($a)*$drop;
  1408. $this->line($start['x']-$dropx,$start['y']-$dropy,$info['x']-$dropx,$info['y']-$dropy);
  1409. $this->restoreState();
  1410. break;
  1411. }
  1412. }
  1413. // ------------------------------------------------------------------------------
  1414. }
  1415. ?>