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

/application/libraries/pdfest.php

https://github.com/paanblogger/sacl
PHP | 4114 lines | 3009 code | 458 blank | 647 comment | 421 complexity | e0b0f4bd5943e0c8cad6d13fd107faf1 MD5 | raw file

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

  1. <?php
  2. //require_once('/fpdi/fpdi_protection.php');
  3. //require_once("fpdf.php");
  4. //*require_once("fpdf_tpl.php");
  5. //*require_once("pdf_context.php");
  6. //*require_once("wrapper_functions.php");
  7. //*require_once("pdf_parser.php");
  8. //*require_once("fpdi_pdf_parser.php");
  9. ///////////////////////////////////////////////////////
  10. //// FPDF START ////
  11. ///////////////////////////////////////////////////////
  12. /*******************************************************************************
  13. * Software: FPDF *
  14. * Version: 1.53 *
  15. * Date: 2004-12-31 *
  16. * Author: Olivier PLATHEY *
  17. * License: Freeware *
  18. * *
  19. * You may use, modify and redistribute this software as you wish. *
  20. *******************************************************************************/
  21. if(!class_exists('FPDF'))
  22. {
  23. define('FPDF_VERSION','1.53');
  24. class FPDF
  25. {
  26. //Private properties
  27. var $page; //current page number
  28. var $n; //current object number
  29. var $offsets; //array of object offsets
  30. var $buffer; //buffer holding in-memory PDF
  31. var $pages; //array containing pages
  32. var $state; //current document state
  33. var $compress; //compression flag
  34. var $DefOrientation; //default orientation
  35. var $CurOrientation; //current orientation
  36. var $OrientationChanges; //array indicating orientation changes
  37. var $k; //scale factor (number of points in user unit)
  38. var $fwPt,$fhPt; //dimensions of page format in points
  39. var $fw,$fh; //dimensions of page format in user unit
  40. var $wPt,$hPt; //current dimensions of page in points
  41. var $w,$h; //current dimensions of page in user unit
  42. var $lMargin; //left margin
  43. var $tMargin; //top margin
  44. var $rMargin; //right margin
  45. var $bMargin; //page break margin
  46. var $cMargin; //cell margin
  47. var $x,$y; //current position in user unit for cell positioning
  48. var $lasth; //height of last cell printed
  49. var $LineWidth; //line width in user unit
  50. var $CoreFonts; //array of standard font names
  51. var $fonts; //array of used fonts
  52. var $FontFiles; //array of font files
  53. var $diffs; //array of encoding differences
  54. var $images; //array of used images
  55. var $PageLinks; //array of links in pages
  56. var $links; //array of internal links
  57. var $FontFamily; //current font family
  58. var $FontStyle; //current font style
  59. var $underline; //underlining flag
  60. var $CurrentFont; //current font info
  61. var $FontSizePt; //current font size in points
  62. var $FontSize; //current font size in user unit
  63. var $DrawColor; //commands for drawing color
  64. var $FillColor; //commands for filling color
  65. var $TextColor; //commands for text color
  66. var $ColorFlag; //indicates whether fill and text colors are different
  67. var $ws; //word spacing
  68. var $AutoPageBreak; //automatic page breaking
  69. var $PageBreakTrigger; //threshold used to trigger page breaks
  70. var $InFooter; //flag set when processing footer
  71. var $ZoomMode; //zoom display mode
  72. var $LayoutMode; //layout display mode
  73. var $title; //title
  74. var $subject; //subject
  75. var $author; //author
  76. var $keywords; //keywords
  77. var $creator; //creator
  78. var $AliasNbPages; //alias for total number of pages
  79. var $PDFVersion; //PDF version number
  80. /*******************************************************************************
  81. * *
  82. * Public methods *
  83. * *
  84. *******************************************************************************/
  85. function FPDF($orientation='P',$unit='mm',$format='A4')
  86. {
  87. //Some checks
  88. $this->_dochecks();
  89. //Initialization of properties
  90. $this->page=0;
  91. $this->n=2;
  92. $this->buffer='';
  93. $this->pages=array();
  94. $this->OrientationChanges=array();
  95. $this->state=0;
  96. $this->fonts=array();
  97. $this->FontFiles=array();
  98. $this->diffs=array();
  99. $this->images=array();
  100. $this->links=array();
  101. $this->InFooter=false;
  102. $this->lasth=0;
  103. $this->FontFamily='';
  104. $this->FontStyle='';
  105. $this->FontSizePt=12;
  106. $this->underline=false;
  107. $this->DrawColor='0 G';
  108. $this->FillColor='0 g';
  109. $this->TextColor='0 g';
  110. $this->ColorFlag=false;
  111. $this->ws=0;
  112. //Standard fonts
  113. $this->CoreFonts=array('courier'=>'Courier','courierB'=>'Courier-Bold','courierI'=>'Courier-Oblique','courierBI'=>'Courier-BoldOblique',
  114. 'helvetica'=>'Helvetica','helveticaB'=>'Helvetica-Bold','helveticaI'=>'Helvetica-Oblique','helveticaBI'=>'Helvetica-BoldOblique',
  115. 'times'=>'Times-Roman','timesB'=>'Times-Bold','timesI'=>'Times-Italic','timesBI'=>'Times-BoldItalic',
  116. 'symbol'=>'Symbol','zapfdingbats'=>'ZapfDingbats');
  117. //Scale factor
  118. if($unit=='pt')
  119. $this->k=1;
  120. elseif($unit=='mm')
  121. $this->k=72/25.4;
  122. elseif($unit=='cm')
  123. $this->k=72/2.54;
  124. elseif($unit=='in')
  125. $this->k=72;
  126. else
  127. $this->Error('Incorrect unit: '.$unit);
  128. //Page format
  129. if(is_string($format))
  130. {
  131. $format=strtolower($format);
  132. if($format=='a3')
  133. $format=array(841.89,1190.55);
  134. elseif($format=='a4')
  135. $format=array(595.28,841.89);
  136. elseif($format=='a5')
  137. $format=array(420.94,595.28);
  138. elseif($format=='letter')
  139. $format=array(612,792);
  140. elseif($format=='legal')
  141. $format=array(612,1008);
  142. else
  143. $this->Error('Unknown page format: '.$format);
  144. $this->fwPt=$format[0];
  145. $this->fhPt=$format[1];
  146. }
  147. else
  148. {
  149. $this->fwPt=$format[0]*$this->k;
  150. $this->fhPt=$format[1]*$this->k;
  151. }
  152. $this->fw=$this->fwPt/$this->k;
  153. $this->fh=$this->fhPt/$this->k;
  154. //Page orientation
  155. $orientation=strtolower($orientation);
  156. if($orientation=='p' || $orientation=='portrait')
  157. {
  158. $this->DefOrientation='P';
  159. $this->wPt=$this->fwPt;
  160. $this->hPt=$this->fhPt;
  161. }
  162. elseif($orientation=='l' || $orientation=='landscape')
  163. {
  164. $this->DefOrientation='L';
  165. $this->wPt=$this->fhPt;
  166. $this->hPt=$this->fwPt;
  167. }
  168. else
  169. $this->Error('Incorrect orientation: '.$orientation);
  170. $this->CurOrientation=$this->DefOrientation;
  171. $this->w=$this->wPt/$this->k;
  172. $this->h=$this->hPt/$this->k;
  173. //Page margins (1 cm)
  174. $margin=28.35/$this->k;
  175. $this->SetMargins($margin,$margin);
  176. //Interior cell margin (1 mm)
  177. $this->cMargin=$margin/10;
  178. //Line width (0.2 mm)
  179. $this->LineWidth=.567/$this->k;
  180. //Automatic page break
  181. $this->SetAutoPageBreak(true,2*$margin);
  182. //Full width display mode
  183. $this->SetDisplayMode('fullwidth');
  184. //Enable compression
  185. $this->SetCompression(true);
  186. //Set default PDF version number
  187. $this->PDFVersion='1.3';
  188. }
  189. function SetMargins($left,$top,$right=-1)
  190. {
  191. //Set left, top and right margins
  192. $this->lMargin=$left;
  193. $this->tMargin=$top;
  194. if($right==-1)
  195. $right=$left;
  196. $this->rMargin=$right;
  197. }
  198. function SetLeftMargin($margin)
  199. {
  200. //Set left margin
  201. $this->lMargin=$margin;
  202. if($this->page>0 && $this->x<$margin)
  203. $this->x=$margin;
  204. }
  205. function SetTopMargin($margin)
  206. {
  207. //Set top margin
  208. $this->tMargin=$margin;
  209. }
  210. function SetRightMargin($margin)
  211. {
  212. //Set right margin
  213. $this->rMargin=$margin;
  214. }
  215. function SetAutoPageBreak($auto,$margin=0)
  216. {
  217. //Set auto page break mode and triggering margin
  218. $this->AutoPageBreak=$auto;
  219. $this->bMargin=$margin;
  220. $this->PageBreakTrigger=$this->h-$margin;
  221. }
  222. function SetDisplayMode($zoom,$layout='continuous')
  223. {
  224. //Set display mode in viewer
  225. if($zoom=='fullpage' || $zoom=='fullwidth' || $zoom=='real' || $zoom=='default' || !is_string($zoom))
  226. $this->ZoomMode=$zoom;
  227. else
  228. $this->Error('Incorrect zoom display mode: '.$zoom);
  229. if($layout=='single' || $layout=='continuous' || $layout=='two' || $layout=='default')
  230. $this->LayoutMode=$layout;
  231. else
  232. $this->Error('Incorrect layout display mode: '.$layout);
  233. }
  234. function SetCompression($compress)
  235. {
  236. //Set page compression
  237. if(function_exists('gzcompress'))
  238. $this->compress=$compress;
  239. else
  240. $this->compress=false;
  241. }
  242. function SetTitle($title)
  243. {
  244. //Title of document
  245. $this->title=$title;
  246. }
  247. function SetSubject($subject)
  248. {
  249. //Subject of document
  250. $this->subject=$subject;
  251. }
  252. function SetAuthor($author)
  253. {
  254. //Author of document
  255. $this->author=$author;
  256. }
  257. function SetKeywords($keywords)
  258. {
  259. //Keywords of document
  260. $this->keywords=$keywords;
  261. }
  262. function SetCreator($creator)
  263. {
  264. //Creator of document
  265. $this->creator=$creator;
  266. }
  267. function AliasNbPages($alias='{nb}')
  268. {
  269. //Define an alias for total number of pages
  270. $this->AliasNbPages=$alias;
  271. }
  272. function Error($msg)
  273. {
  274. //Fatal error
  275. die('<B>FPDF error: </B>'.$msg);
  276. }
  277. function Open()
  278. {
  279. //Begin document
  280. $this->state=1;
  281. }
  282. function Close()
  283. {
  284. //Terminate document
  285. if($this->state==3)
  286. return;
  287. if($this->page==0)
  288. $this->AddPage();
  289. //Page footer
  290. $this->InFooter=true;
  291. $this->Footer();
  292. $this->InFooter=false;
  293. //Close page
  294. $this->_endpage();
  295. //Close document
  296. $this->_enddoc();
  297. }
  298. function AddPage($orientation='')
  299. {
  300. //Start a new page
  301. if($this->state==0)
  302. $this->Open();
  303. $family=$this->FontFamily;
  304. $style=$this->FontStyle.($this->underline ? 'U' : '');
  305. $size=$this->FontSizePt;
  306. $lw=$this->LineWidth;
  307. $dc=$this->DrawColor;
  308. $fc=$this->FillColor;
  309. $tc=$this->TextColor;
  310. $cf=$this->ColorFlag;
  311. if($this->page>0)
  312. {
  313. //Page footer
  314. $this->InFooter=true;
  315. $this->Footer();
  316. $this->InFooter=false;
  317. //Close page
  318. $this->_endpage();
  319. }
  320. //Start new page
  321. $this->_beginpage($orientation);
  322. //Set line cap style to square
  323. $this->_out('2 J');
  324. //Set line width
  325. $this->LineWidth=$lw;
  326. $this->_out(sprintf('%.2f w',$lw*$this->k));
  327. //Set font
  328. if($family)
  329. $this->SetFont($family,$style,$size);
  330. //Set colors
  331. $this->DrawColor=$dc;
  332. if($dc!='0 G')
  333. $this->_out($dc);
  334. $this->FillColor=$fc;
  335. if($fc!='0 g')
  336. $this->_out($fc);
  337. $this->TextColor=$tc;
  338. $this->ColorFlag=$cf;
  339. //Page header
  340. $this->Header();
  341. //Restore line width
  342. if($this->LineWidth!=$lw)
  343. {
  344. $this->LineWidth=$lw;
  345. $this->_out(sprintf('%.2f w',$lw*$this->k));
  346. }
  347. //Restore font
  348. if($family)
  349. $this->SetFont($family,$style,$size);
  350. //Restore colors
  351. if($this->DrawColor!=$dc)
  352. {
  353. $this->DrawColor=$dc;
  354. $this->_out($dc);
  355. }
  356. if($this->FillColor!=$fc)
  357. {
  358. $this->FillColor=$fc;
  359. $this->_out($fc);
  360. }
  361. $this->TextColor=$tc;
  362. $this->ColorFlag=$cf;
  363. }
  364. function Header()
  365. {
  366. //To be implemented in your own inherited class
  367. }
  368. function Footer()
  369. {
  370. //To be implemented in your own inherited class
  371. }
  372. function PageNo()
  373. {
  374. //Get current page number
  375. return $this->page;
  376. }
  377. function SetDrawColor($r,$g=-1,$b=-1)
  378. {
  379. //Set color for all stroking operations
  380. if(($r==0 && $g==0 && $b==0) || $g==-1)
  381. $this->DrawColor=sprintf('%.3f G',$r/255);
  382. else
  383. $this->DrawColor=sprintf('%.3f %.3f %.3f RG',$r/255,$g/255,$b/255);
  384. if($this->page>0)
  385. $this->_out($this->DrawColor);
  386. }
  387. function SetFillColor($r,$g=-1,$b=-1)
  388. {
  389. //Set color for all filling operations
  390. if(($r==0 && $g==0 && $b==0) || $g==-1)
  391. $this->FillColor=sprintf('%.3f g',$r/255);
  392. else
  393. $this->FillColor=sprintf('%.3f %.3f %.3f rg',$r/255,$g/255,$b/255);
  394. $this->ColorFlag=($this->FillColor!=$this->TextColor);
  395. if($this->page>0)
  396. $this->_out($this->FillColor);
  397. }
  398. function SetTextColor($r,$g=-1,$b=-1)
  399. {
  400. //Set color for text
  401. if(($r==0 && $g==0 && $b==0) || $g==-1)
  402. $this->TextColor=sprintf('%.3f g',$r/255);
  403. else
  404. $this->TextColor=sprintf('%.3f %.3f %.3f rg',$r/255,$g/255,$b/255);
  405. $this->ColorFlag=($this->FillColor!=$this->TextColor);
  406. }
  407. function GetStringWidth($s)
  408. {
  409. //Get width of a string in the current font
  410. $s=(string)$s;
  411. $cw=&$this->CurrentFont['cw'];
  412. $w=0;
  413. $l=strlen($s);
  414. for($i=0;$i<$l;$i++)
  415. $w+=$cw[$s{$i}];
  416. return $w*$this->FontSize/1000;
  417. }
  418. function SetLineWidth($width)
  419. {
  420. //Set line width
  421. $this->LineWidth=$width;
  422. if($this->page>0)
  423. $this->_out(sprintf('%.2f w',$width*$this->k));
  424. }
  425. function Line($x1,$y1,$x2,$y2)
  426. {
  427. //Draw a line
  428. $this->_out(sprintf('%.2f %.2f m %.2f %.2f l S',$x1*$this->k,($this->h-$y1)*$this->k,$x2*$this->k,($this->h-$y2)*$this->k));
  429. }
  430. function Rect($x,$y,$w,$h,$style='')
  431. {
  432. //Draw a rectangle
  433. if($style=='F')
  434. $op='f';
  435. elseif($style=='FD' || $style=='DF')
  436. $op='B';
  437. else
  438. $op='S';
  439. $this->_out(sprintf('%.2f %.2f %.2f %.2f re %s',$x*$this->k,($this->h-$y)*$this->k,$w*$this->k,-$h*$this->k,$op));
  440. }
  441. function AddFont($family,$style='',$file='')
  442. {
  443. //Add a TrueType or Type1 font
  444. $family=strtolower($family);
  445. if($file=='')
  446. $file=str_replace(' ','',$family).strtolower($style).'.php';
  447. if($family=='arial')
  448. $family='helvetica';
  449. $style=strtoupper($style);
  450. if($style=='IB')
  451. $style='BI';
  452. $fontkey=$family.$style;
  453. if(isset($this->fonts[$fontkey]))
  454. $this->Error('Font already added: '.$family.' '.$style);
  455. include($this->_getfontpath().$file);
  456. if(!isset($name))
  457. $this->Error('Could not include font definition file');
  458. $i=count($this->fonts)+1;
  459. $this->fonts[$fontkey]=array('i'=>$i,'type'=>$type,'name'=>$name,'desc'=>$desc,'up'=>$up,'ut'=>$ut,'cw'=>$cw,'enc'=>$enc,'file'=>$file);
  460. if($diff)
  461. {
  462. //Search existing encodings
  463. $d=0;
  464. $nb=count($this->diffs);
  465. for($i=1;$i<=$nb;$i++)
  466. {
  467. if($this->diffs[$i]==$diff)
  468. {
  469. $d=$i;
  470. break;
  471. }
  472. }
  473. if($d==0)
  474. {
  475. $d=$nb+1;
  476. $this->diffs[$d]=$diff;
  477. }
  478. $this->fonts[$fontkey]['diff']=$d;
  479. }
  480. if($file)
  481. {
  482. if($type=='TrueType')
  483. $this->FontFiles[$file]=array('length1'=>$originalsize);
  484. else
  485. $this->FontFiles[$file]=array('length1'=>$size1,'length2'=>$size2);
  486. }
  487. }
  488. function SetFont($family,$style='',$size=0)
  489. {
  490. //Select a font; size given in points
  491. global $fpdf_charwidths;
  492. $family=strtolower($family);
  493. if($family=='')
  494. $family=$this->FontFamily;
  495. if($family=='arial')
  496. $family='helvetica';
  497. elseif($family=='symbol' || $family=='zapfdingbats')
  498. $style='';
  499. $style=strtoupper($style);
  500. if(strpos($style,'U')!==false)
  501. {
  502. $this->underline=true;
  503. $style=str_replace('U','',$style);
  504. }
  505. else
  506. $this->underline=false;
  507. if($style=='IB')
  508. $style='BI';
  509. if($size==0)
  510. $size=$this->FontSizePt;
  511. //Test if font is already selected
  512. if($this->FontFamily==$family && $this->FontStyle==$style && $this->FontSizePt==$size)
  513. return;
  514. //Test if used for the first time
  515. $fontkey=$family.$style;
  516. if(!isset($this->fonts[$fontkey]))
  517. {
  518. //Check if one of the standard fonts
  519. if(isset($this->CoreFonts[$fontkey]))
  520. {
  521. if(!isset($fpdf_charwidths[$fontkey]))
  522. {
  523. //Load metric file
  524. $file=$family;
  525. if($family=='times' || $family=='helvetica')
  526. $file.=strtolower($style);
  527. //include($this->_getfontpath().$file.'.php');
  528. $type = 'Core';
  529. $name = 'Helvetica-Oblique';
  530. $up = -100;
  531. $ut = 50;
  532. $cw = array(
  533. chr(0)=>278,chr(1)=>278,chr(2)=>278,chr(3)=>278,chr(4)=>278,chr(5)=>278,chr(6)=>278,chr(7)=>278,chr(8)=>278,chr(9)=>278,chr(10)=>278,chr(11)=>278,chr(12)=>278,chr(13)=>278,chr(14)=>278,chr(15)=>278,chr(16)=>278,chr(17)=>278,chr(18)=>278,chr(19)=>278,chr(20)=>278,chr(21)=>278,
  534. chr(22)=>278,chr(23)=>278,chr(24)=>278,chr(25)=>278,chr(26)=>278,chr(27)=>278,chr(28)=>278,chr(29)=>278,chr(30)=>278,chr(31)=>278,' '=>278,'!'=>278,'"'=>355,'#'=>556,'$'=>556,'%'=>889,'&'=>667,'\''=>191,'('=>333,')'=>333,'*'=>389,'+'=>584,
  535. ','=>278,'-'=>333,'.'=>278,'/'=>278,'0'=>556,'1'=>556,'2'=>556,'3'=>556,'4'=>556,'5'=>556,'6'=>556,'7'=>556,'8'=>556,'9'=>556,':'=>278,';'=>278,'<'=>584,'='=>584,'>'=>584,'?'=>556,'@'=>1015,'A'=>667,
  536. 'B'=>667,'C'=>722,'D'=>722,'E'=>667,'F'=>611,'G'=>778,'H'=>722,'I'=>278,'J'=>500,'K'=>667,'L'=>556,'M'=>833,'N'=>722,'O'=>778,'P'=>667,'Q'=>778,'R'=>722,'S'=>667,'T'=>611,'U'=>722,'V'=>667,'W'=>944,
  537. 'X'=>667,'Y'=>667,'Z'=>611,'['=>278,'\\'=>278,']'=>278,'^'=>469,'_'=>556,'`'=>333,'a'=>556,'b'=>556,'c'=>500,'d'=>556,'e'=>556,'f'=>278,'g'=>556,'h'=>556,'i'=>222,'j'=>222,'k'=>500,'l'=>222,'m'=>833,
  538. 'n'=>556,'o'=>556,'p'=>556,'q'=>556,'r'=>333,'s'=>500,'t'=>278,'u'=>556,'v'=>500,'w'=>722,'x'=>500,'y'=>500,'z'=>500,'{'=>334,'|'=>260,'}'=>334,'~'=>584,chr(127)=>350,chr(128)=>556,chr(129)=>350,chr(130)=>222,chr(131)=>556,
  539. chr(132)=>333,chr(133)=>1000,chr(134)=>556,chr(135)=>556,chr(136)=>333,chr(137)=>1000,chr(138)=>667,chr(139)=>333,chr(140)=>1000,chr(141)=>350,chr(142)=>611,chr(143)=>350,chr(144)=>350,chr(145)=>222,chr(146)=>222,chr(147)=>333,chr(148)=>333,chr(149)=>350,chr(150)=>556,chr(151)=>1000,chr(152)=>333,chr(153)=>1000,
  540. chr(154)=>500,chr(155)=>333,chr(156)=>944,chr(157)=>350,chr(158)=>500,chr(159)=>667,chr(160)=>278,chr(161)=>333,chr(162)=>556,chr(163)=>556,chr(164)=>556,chr(165)=>556,chr(166)=>260,chr(167)=>556,chr(168)=>333,chr(169)=>737,chr(170)=>370,chr(171)=>556,chr(172)=>584,chr(173)=>333,chr(174)=>737,chr(175)=>333,
  541. chr(176)=>400,chr(177)=>584,chr(178)=>333,chr(179)=>333,chr(180)=>333,chr(181)=>556,chr(182)=>537,chr(183)=>278,chr(184)=>333,chr(185)=>333,chr(186)=>365,chr(187)=>556,chr(188)=>834,chr(189)=>834,chr(190)=>834,chr(191)=>611,chr(192)=>667,chr(193)=>667,chr(194)=>667,chr(195)=>667,chr(196)=>667,chr(197)=>667,
  542. chr(198)=>1000,chr(199)=>722,chr(200)=>667,chr(201)=>667,chr(202)=>667,chr(203)=>667,chr(204)=>278,chr(205)=>278,chr(206)=>278,chr(207)=>278,chr(208)=>722,chr(209)=>722,chr(210)=>778,chr(211)=>778,chr(212)=>778,chr(213)=>778,chr(214)=>778,chr(215)=>584,chr(216)=>778,chr(217)=>722,chr(218)=>722,chr(219)=>722,
  543. chr(220)=>722,chr(221)=>667,chr(222)=>667,chr(223)=>611,chr(224)=>556,chr(225)=>556,chr(226)=>556,chr(227)=>556,chr(228)=>556,chr(229)=>556,chr(230)=>889,chr(231)=>500,chr(232)=>556,chr(233)=>556,chr(234)=>556,chr(235)=>556,chr(236)=>278,chr(237)=>278,chr(238)=>278,chr(239)=>278,chr(240)=>556,chr(241)=>556,
  544. chr(242)=>556,chr(243)=>556,chr(244)=>556,chr(245)=>556,chr(246)=>556,chr(247)=>584,chr(248)=>611,chr(249)=>556,chr(250)=>556,chr(251)=>556,chr(252)=>556,chr(253)=>500,chr(254)=>556,chr(255)=>500);
  545. $fpdf_charwidths[$fontkey] = $cw;
  546. if(!isset($fpdf_charwidths[$fontkey]))
  547. $this->Error('Could not include font metric file');
  548. }
  549. $i=count($this->fonts)+1;
  550. $this->fonts[$fontkey]=array('i'=>$i,'type'=>'core','name'=>$this->CoreFonts[$fontkey],'up'=>-100,'ut'=>50,'cw'=>$fpdf_charwidths[$fontkey]);
  551. }
  552. else
  553. $this->Error('Undefined font: '.$family.' '.$style);
  554. }
  555. //Select it
  556. $this->FontFamily=$family;
  557. $this->FontStyle=$style;
  558. $this->FontSizePt=$size;
  559. $this->FontSize=$size/$this->k;
  560. $this->CurrentFont=&$this->fonts[$fontkey];
  561. if($this->page>0)
  562. $this->_out(sprintf('BT /F%d %.2f Tf ET',$this->CurrentFont['i'],$this->FontSizePt));
  563. }
  564. function SetFontSize($size)
  565. {
  566. //Set font size in points
  567. if($this->FontSizePt==$size)
  568. return;
  569. $this->FontSizePt=$size;
  570. $this->FontSize=$size/$this->k;
  571. if($this->page>0)
  572. $this->_out(sprintf('BT /F%d %.2f Tf ET',$this->CurrentFont['i'],$this->FontSizePt));
  573. }
  574. function AddLink()
  575. {
  576. //Create a new internal link
  577. $n=count($this->links)+1;
  578. $this->links[$n]=array(0,0);
  579. return $n;
  580. }
  581. function SetLink($link,$y=0,$page=-1)
  582. {
  583. //Set destination of internal link
  584. if($y==-1)
  585. $y=$this->y;
  586. if($page==-1)
  587. $page=$this->page;
  588. $this->links[$link]=array($page,$y);
  589. }
  590. function Link($x,$y,$w,$h,$link)
  591. {
  592. //Put a link on the page
  593. $this->PageLinks[$this->page][]=array($x*$this->k,$this->hPt-$y*$this->k,$w*$this->k,$h*$this->k,$link);
  594. }
  595. function Text($x,$y,$txt)
  596. {
  597. //Output a string
  598. $s=sprintf('BT %.2f %.2f Td (%s) Tj ET',$x*$this->k,($this->h-$y)*$this->k,$this->_escape($txt));
  599. if($this->underline && $txt!='')
  600. $s.=' '.$this->_dounderline($x,$y,$txt);
  601. if($this->ColorFlag)
  602. $s='q '.$this->TextColor.' '.$s.' Q';
  603. $this->_out($s);
  604. }
  605. function AcceptPageBreak()
  606. {
  607. //Accept automatic page break or not
  608. return $this->AutoPageBreak;
  609. }
  610. function Cell($w,$h=0,$txt='',$border=0,$ln=0,$align='',$fill=0,$link='')
  611. {
  612. //Output a cell
  613. $k=$this->k;
  614. if($this->y+$h>$this->PageBreakTrigger && !$this->InFooter && $this->AcceptPageBreak())
  615. {
  616. //Automatic page break
  617. $x=$this->x;
  618. $ws=$this->ws;
  619. if($ws>0)
  620. {
  621. $this->ws=0;
  622. $this->_out('0 Tw');
  623. }
  624. $this->AddPage($this->CurOrientation);
  625. $this->x=$x;
  626. if($ws>0)
  627. {
  628. $this->ws=$ws;
  629. $this->_out(sprintf('%.3f Tw',$ws*$k));
  630. }
  631. }
  632. if($w==0)
  633. $w=$this->w-$this->rMargin-$this->x;
  634. $s='';
  635. if($fill==1 || $border==1)
  636. {
  637. if($fill==1)
  638. $op=($border==1) ? 'B' : 'f';
  639. else
  640. $op='S';
  641. $s=sprintf('%.2f %.2f %.2f %.2f re %s ',$this->x*$k,($this->h-$this->y)*$k,$w*$k,-$h*$k,$op);
  642. }
  643. if(is_string($border))
  644. {
  645. $x=$this->x;
  646. $y=$this->y;
  647. if(strpos($border,'L')!==false)
  648. $s.=sprintf('%.2f %.2f m %.2f %.2f l S ',$x*$k,($this->h-$y)*$k,$x*$k,($this->h-($y+$h))*$k);
  649. if(strpos($border,'T')!==false)
  650. $s.=sprintf('%.2f %.2f m %.2f %.2f l S ',$x*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-$y)*$k);
  651. if(strpos($border,'R')!==false)
  652. $s.=sprintf('%.2f %.2f m %.2f %.2f l S ',($x+$w)*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
  653. if(strpos($border,'B')!==false)
  654. $s.=sprintf('%.2f %.2f m %.2f %.2f l S ',$x*$k,($this->h-($y+$h))*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
  655. }
  656. if($txt!=='')
  657. {
  658. if($align=='R')
  659. $dx=$w-$this->cMargin-$this->GetStringWidth($txt);
  660. elseif($align=='C')
  661. $dx=($w-$this->GetStringWidth($txt))/2;
  662. else
  663. $dx=$this->cMargin;
  664. if($this->ColorFlag)
  665. $s.='q '.$this->TextColor.' ';
  666. $txt2=str_replace(')','\\)',str_replace('(','\\(',str_replace('\\','\\\\',$txt)));
  667. $s.=sprintf('BT %.2f %.2f Td (%s) Tj ET',($this->x+$dx)*$k,($this->h-($this->y+.5*$h+.3*$this->FontSize))*$k,$txt2);
  668. if($this->underline)
  669. $s.=' '.$this->_dounderline($this->x+$dx,$this->y+.5*$h+.3*$this->FontSize,$txt);
  670. if($this->ColorFlag)
  671. $s.=' Q';
  672. if($link)
  673. $this->Link($this->x+$dx,$this->y+.5*$h-.5*$this->FontSize,$this->GetStringWidth($txt),$this->FontSize,$link);
  674. }
  675. if($s)
  676. $this->_out($s);
  677. $this->lasth=$h;
  678. if($ln>0)
  679. {
  680. //Go to next line
  681. $this->y+=$h;
  682. if($ln==1)
  683. $this->x=$this->lMargin;
  684. }
  685. else
  686. $this->x+=$w;
  687. }
  688. function MultiCell($w,$h,$txt,$border=0,$align='J',$fill=0)
  689. {
  690. //Output text with automatic or explicit line breaks
  691. $cw=&$this->CurrentFont['cw'];
  692. if($w==0)
  693. $w=$this->w-$this->rMargin-$this->x;
  694. $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
  695. $s=str_replace("\r",'',$txt);
  696. $nb=strlen($s);
  697. if($nb>0 && $s[$nb-1]=="\n")
  698. $nb--;
  699. $b=0;
  700. if($border)
  701. {
  702. if($border==1)
  703. {
  704. $border='LTRB';
  705. $b='LRT';
  706. $b2='LR';
  707. }
  708. else
  709. {
  710. $b2='';
  711. if(strpos($border,'L')!==false)
  712. $b2.='L';
  713. if(strpos($border,'R')!==false)
  714. $b2.='R';
  715. $b=(strpos($border,'T')!==false) ? $b2.'T' : $b2;
  716. }
  717. }
  718. $sep=-1;
  719. $i=0;
  720. $j=0;
  721. $l=0;
  722. $ns=0;
  723. $nl=1;
  724. while($i<$nb)
  725. {
  726. //Get next character
  727. $c=$s{$i};
  728. if($c=="\n")
  729. {
  730. //Explicit line break
  731. if($this->ws>0)
  732. {
  733. $this->ws=0;
  734. $this->_out('0 Tw');
  735. }
  736. $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
  737. $i++;
  738. $sep=-1;
  739. $j=$i;
  740. $l=0;
  741. $ns=0;
  742. $nl++;
  743. if($border && $nl==2)
  744. $b=$b2;
  745. continue;
  746. }
  747. if($c==' ')
  748. {
  749. $sep=$i;
  750. $ls=$l;
  751. $ns++;
  752. }
  753. $l+=$cw[$c];
  754. if($l>$wmax)
  755. {
  756. //Automatic line break
  757. if($sep==-1)
  758. {
  759. if($i==$j)
  760. $i++;
  761. if($this->ws>0)
  762. {
  763. $this->ws=0;
  764. $this->_out('0 Tw');
  765. }
  766. $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
  767. }
  768. else
  769. {
  770. if($align=='J')
  771. {
  772. $this->ws=($ns>1) ? ($wmax-$ls)/1000*$this->FontSize/($ns-1) : 0;
  773. $this->_out(sprintf('%.3f Tw',$this->ws*$this->k));
  774. }
  775. $this->Cell($w,$h,substr($s,$j,$sep-$j),$b,2,$align,$fill);
  776. $i=$sep+1;
  777. }
  778. $sep=-1;
  779. $j=$i;
  780. $l=0;
  781. $ns=0;
  782. $nl++;
  783. if($border && $nl==2)
  784. $b=$b2;
  785. }
  786. else
  787. $i++;
  788. }
  789. //Last chunk
  790. if($this->ws>0)
  791. {
  792. $this->ws=0;
  793. $this->_out('0 Tw');
  794. }
  795. if($border && strpos($border,'B')!==false)
  796. $b.='B';
  797. $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
  798. $this->x=$this->lMargin;
  799. }
  800. function Write($h,$txt,$link='')
  801. {
  802. //Output text in flowing mode
  803. $cw=&$this->CurrentFont['cw'];
  804. $w=$this->w-$this->rMargin-$this->x;
  805. $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
  806. $s=str_replace("\r",'',$txt);
  807. $nb=strlen($s);
  808. $sep=-1;
  809. $i=0;
  810. $j=0;
  811. $l=0;
  812. $nl=1;
  813. while($i<$nb)
  814. {
  815. //Get next character
  816. $c=$s{$i};
  817. if($c=="\n")
  818. {
  819. //Explicit line break
  820. $this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link);
  821. $i++;
  822. $sep=-1;
  823. $j=$i;
  824. $l=0;
  825. if($nl==1)
  826. {
  827. $this->x=$this->lMargin;
  828. $w=$this->w-$this->rMargin-$this->x;
  829. $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
  830. }
  831. $nl++;
  832. continue;
  833. }
  834. if($c==' ')
  835. $sep=$i;
  836. $l+=$cw[$c];
  837. if($l>$wmax)
  838. {
  839. //Automatic line break
  840. if($sep==-1)
  841. {
  842. if($this->x>$this->lMargin)
  843. {
  844. //Move to next line
  845. $this->x=$this->lMargin;
  846. $this->y+=$h;
  847. $w=$this->w-$this->rMargin-$this->x;
  848. $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
  849. $i++;
  850. $nl++;
  851. continue;
  852. }
  853. if($i==$j)
  854. $i++;
  855. $this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link);
  856. }
  857. else
  858. {
  859. $this->Cell($w,$h,substr($s,$j,$sep-$j),0,2,'',0,$link);
  860. $i=$sep+1;
  861. }
  862. $sep=-1;
  863. $j=$i;
  864. $l=0;
  865. if($nl==1)
  866. {
  867. $this->x=$this->lMargin;
  868. $w=$this->w-$this->rMargin-$this->x;
  869. $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
  870. }
  871. $nl++;
  872. }
  873. else
  874. $i++;
  875. }
  876. //Last chunk
  877. if($i!=$j)
  878. $this->Cell($l/1000*$this->FontSize,$h,substr($s,$j),0,0,'',0,$link);
  879. }
  880. function Image($file,$x,$y,$w=0,$h=0,$type='',$link='')
  881. {
  882. //Put an image on the page
  883. if(!isset($this->images[$file]))
  884. {
  885. //First use of image, get info
  886. if($type=='')
  887. {
  888. $pos=strrpos($file,'.');
  889. if(!$pos)
  890. $this->Error('Image file has no extension and no type was specified: '.$file);
  891. $type=substr($file,$pos+1);
  892. }
  893. $type=strtolower($type);
  894. $mqr=get_magic_quotes_runtime();
  895. //set_magic_quotes_runtime(0);
  896. if($type=='jpg' || $type=='jpeg')
  897. $info=$this->_parsejpg($file);
  898. elseif($type=='png')
  899. $info=$this->_parsepng($file);
  900. else
  901. {
  902. //Allow for additional formats
  903. $mtd='_parse'.$type;
  904. if(!method_exists($this,$mtd))
  905. $this->Error('Unsupported image type: '.$type);
  906. $info=$this->$mtd($file);
  907. }
  908. //set_magic_quotes_runtime($mqr);
  909. $info['i']=count($this->images)+1;
  910. $this->images[$file]=$info;
  911. }
  912. else
  913. $info=$this->images[$file];
  914. //Automatic width and height calculation if needed
  915. if($w==0 && $h==0)
  916. {
  917. //Put image at 72 dpi
  918. $w=$info['w']/$this->k;
  919. $h=$info['h']/$this->k;
  920. }
  921. if($w==0)
  922. $w=$h*$info['w']/$info['h'];
  923. if($h==0)
  924. $h=$w*$info['h']/$info['w'];
  925. $this->_out(sprintf('q %.2f 0 0 %.2f %.2f %.2f cm /I%d Do Q',$w*$this->k,$h*$this->k,$x*$this->k,($this->h-($y+$h))*$this->k,$info['i']));
  926. if($link)
  927. $this->Link($x,$y,$w,$h,$link);
  928. }
  929. function Ln($h='')
  930. {
  931. //Line feed; default value is last cell height
  932. $this->x=$this->lMargin;
  933. if(is_string($h))
  934. $this->y+=$this->lasth;
  935. else
  936. $this->y+=$h;
  937. }
  938. function GetX()
  939. {
  940. //Get x position
  941. return $this->x;
  942. }
  943. function SetX($x)
  944. {
  945. //Set x position
  946. if($x>=0)
  947. $this->x=$x;
  948. else
  949. $this->x=$this->w+$x;
  950. }
  951. function GetY()
  952. {
  953. //Get y position
  954. return $this->y;
  955. }
  956. function SetY($y)
  957. {
  958. //Set y position and reset x
  959. $this->x=$this->lMargin;
  960. if($y>=0)
  961. $this->y=$y;
  962. else
  963. $this->y=$this->h+$y;
  964. }
  965. function SetXY($x,$y)
  966. {
  967. //Set x and y positions
  968. $this->SetY($y);
  969. $this->SetX($x);
  970. }
  971. function Output($name='',$dest='')
  972. {
  973. //Output PDF to some destination
  974. //Finish document if necessary
  975. if($this->state<3)
  976. $this->Close();
  977. //Normalize parameters
  978. if(is_bool($dest))
  979. $dest=$dest ? 'D' : 'F';
  980. $dest=strtoupper($dest);
  981. if($dest=='')
  982. {
  983. if($name=='')
  984. {
  985. $name='doc.pdf';
  986. $dest='I';
  987. }
  988. else
  989. $dest='F';
  990. }
  991. switch($dest)
  992. {
  993. case 'I':
  994. //Send to standard output
  995. if(ob_get_contents())
  996. $this->Error('Some data has already been output, can\'t send PDF file');
  997. if(php_sapi_name()!='cli')
  998. {
  999. //We send to a browser
  1000. header('Content-Type: application/pdf');
  1001. if(headers_sent())
  1002. $this->Error('Some data has already been output to browser, can\'t send PDF file');
  1003. header('Content-Length: '.strlen($this->buffer));
  1004. header('Content-disposition: inline; filename="'.$name.'"');
  1005. }
  1006. echo $this->buffer;
  1007. break;
  1008. case 'D':
  1009. //Download file
  1010. if(ob_get_contents())
  1011. $this->Error('Some data has already been output, can\'t send PDF file');
  1012. if(isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'],'MSIE'))
  1013. header('Content-Type: application/force-download');
  1014. else
  1015. header('Content-Type: application/octet-stream');
  1016. if(headers_sent())
  1017. $this->Error('Some data has already been output to browser, can\'t send PDF file');
  1018. header('Content-Length: '.strlen($this->buffer));
  1019. header('Content-disposition: attachment; filename="'.$name.'"');
  1020. echo $this->buffer;
  1021. break;
  1022. case 'F':
  1023. //Save to local file
  1024. $f=fopen($name,'wb');
  1025. if(!$f)
  1026. $this->Error('Unable to create output file: '.$name);
  1027. fwrite($f,$this->buffer,strlen($this->buffer));
  1028. fclose($f);
  1029. break;
  1030. case 'S':
  1031. //Return as a string
  1032. return $this->buffer;
  1033. default:
  1034. $this->Error('Incorrect output destination: '.$dest);
  1035. }
  1036. return '';
  1037. }
  1038. /*******************************************************************************
  1039. * *
  1040. * Protected methods *
  1041. * *
  1042. *******************************************************************************/
  1043. function _dochecks()
  1044. {
  1045. //Check for locale-related bug
  1046. if(1.1==1)
  1047. $this->Error('Don\'t alter the locale before including class file');
  1048. //Check for decimal separator
  1049. if(sprintf('%.1f',1.0)!='1.0')
  1050. setlocale(LC_NUMERIC,'C');
  1051. }
  1052. function _getfontpath()
  1053. {
  1054. if(!defined('FPDF_FONTPATH') && is_dir(dirname(__FILE__).'/font'))
  1055. define('FPDF_FONTPATH',dirname(__FILE__).'/font/');
  1056. return defined('FPDF_FONTPATH') ? FPDF_FONTPATH : '';
  1057. }
  1058. function _putpages()
  1059. {
  1060. $nb=$this->page;
  1061. if(!empty($this->AliasNbPages))
  1062. {
  1063. //Replace number of pages
  1064. for($n=1;$n<=$nb;$n++)
  1065. $this->pages[$n]=str_replace($this->AliasNbPages,$nb,$this->pages[$n]);
  1066. }
  1067. if($this->DefOrientation=='P')
  1068. {
  1069. $wPt=$this->fwPt;
  1070. $hPt=$this->fhPt;
  1071. }
  1072. else
  1073. {
  1074. $wPt=$this->fhPt;
  1075. $hPt=$this->fwPt;
  1076. }
  1077. $filter=($this->compress) ? '/Filter /FlateDecode ' : '';
  1078. for($n=1;$n<=$nb;$n++)
  1079. {
  1080. //Page
  1081. $this->_newobj();
  1082. $this->_out('<</Type /Page');
  1083. $this->_out('/Parent 1 0 R');
  1084. if(isset($this->OrientationChanges[$n]))
  1085. $this->_out(sprintf('/MediaBox [0 0 %.2f %.2f]',$hPt,$wPt));
  1086. $this->_out('/Resources 2 0 R');
  1087. if(isset($this->PageLinks[$n]))
  1088. {
  1089. //Links
  1090. $annots='/Annots [';
  1091. foreach($this->PageLinks[$n] as $pl)
  1092. {
  1093. $rect=sprintf('%.2f %.2f %.2f %.2f',$pl[0],$pl[1],$pl[0]+$pl[2],$pl[1]-$pl[3]);
  1094. $annots.='<</Type /Annot /Subtype /Link /Rect ['.$rect.'] /Border [0 0 0] ';
  1095. if(is_string($pl[4]))
  1096. $annots.='/A <</S /URI /URI '.$this->_textstring($pl[4]).'>>>>';
  1097. else
  1098. {
  1099. $l=$this->links[$pl[4]];
  1100. $h=isset($this->OrientationChanges[$l[0]]) ? $wPt : $hPt;
  1101. $annots.=sprintf('/Dest [%d 0 R /XYZ 0 %.2f null]>>',1+2*$l[0],$h-$l[1]*$this->k);
  1102. }
  1103. }
  1104. $this->_out($annots.']');
  1105. }
  1106. $this->_out('/Contents '.($this->n+1).' 0 R>>');
  1107. $this->_out('endobj');
  1108. //Page content
  1109. $p=($this->compress) ? gzcompress($this->pages[$n]) : $this->pages[$n];
  1110. $this->_newobj();
  1111. $this->_out('<<'.$filter.'/Length '.strlen($p).'>>');
  1112. $this->_putstream($p);
  1113. $this->_out('endobj');
  1114. }
  1115. //Pages root
  1116. $this->offsets[1]=strlen($this->buffer);
  1117. $this->_out('1 0 obj');
  1118. $this->_out('<</Type /Pages');
  1119. $kids='/Kids [';
  1120. for($i=0;$i<$nb;$i++)
  1121. $kids.=(3+2*$i).' 0 R ';
  1122. $this->_out($kids.']');
  1123. $this->_out('/Count '.$nb);
  1124. $this->_out(sprintf('/MediaBox [0 0 %.2f %.2f]',$wPt,$hPt));
  1125. $this->_out('>>');
  1126. $this->_out('endobj');
  1127. }
  1128. function _putfonts()
  1129. {
  1130. $nf=$this->n;
  1131. foreach($this->diffs as $diff)
  1132. {
  1133. //Encodings
  1134. $this->_newobj();
  1135. $this->_out('<</Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences ['.$diff.']>>');
  1136. $this->_out('endobj');
  1137. }
  1138. $mqr=get_magic_quotes_runtime();
  1139. //set_magic_quotes_runtime(0);
  1140. foreach($this->FontFiles as $file=>$info)
  1141. {
  1142. //Font file embedding
  1143. $this->_newobj();
  1144. $this->FontFiles[$file]['n']=$this->n;
  1145. $font='';
  1146. $f=fopen($this->_getfontpath().$file,'rb',1);
  1147. if(!$f)
  1148. $this->Error('Font file not found');
  1149. while(!feof($f))
  1150. $font.=fread($f,8192);
  1151. fclose($f);
  1152. $compressed=(substr($file,-2)=='.z');
  1153. if(!$compressed && isset($info['length2']))
  1154. {
  1155. $header=(ord($font{0})==128);
  1156. if($header)
  1157. {
  1158. //Strip first binary header
  1159. $font=substr($font,6);
  1160. }
  1161. if($header && ord($font{$info['length1']})==128)
  1162. {
  1163. //Strip second binary header
  1164. $font=substr($font,0,$info['length1']).substr($font,$info['length1']+6);
  1165. }
  1166. }
  1167. $this->_out('<</Length '.strlen($font));
  1168. if($compressed)
  1169. $this->_out('/Filter /FlateDecode');
  1170. $this->_out('/Length1 '.$info['length1']);
  1171. if(isset($info['length2']))
  1172. $this->_out('/Length2 '.$info['length2'].' /Length3 0');
  1173. $this->_out('>>');
  1174. $this->_putstream($font);
  1175. $this->_out('endobj');
  1176. }
  1177. //set_magic_quotes_runtime($mqr);
  1178. foreach($this->fonts as $k=>$font)
  1179. {
  1180. //Font objects
  1181. $this->fonts[$k]['n']=$this->n+1;
  1182. $type=$font['type'];
  1183. $name=$font['name'];
  1184. if($type=='core')
  1185. {
  1186. //Standard font
  1187. $this->_newobj();
  1188. $this->_out('<</Type /Font');
  1189. $this->_out('/BaseFont /'.$name);
  1190. $this->_out('/Subtype /Type1');
  1191. if($name!='Symbol' && $name!='ZapfDingbats')
  1192. $this->_out('/Encoding /WinAnsiEncoding');
  1193. $this->_out('>>');
  1194. $this->_out('endobj');
  1195. }
  1196. elseif($type=='Type1' || $type=='TrueType')
  1197. {
  1198. //Additional Type1 or TrueType font
  1199. $this->_newobj();
  1200. $this->_out('<</Type /Font');
  1201. $this->_out('/BaseFont /'.$name);
  1202. $this->_out('/Subtype /'.$type);
  1203. $this->_out('/FirstChar 32 /LastChar 255');
  1204. $this->_out('/Widths '.($this->n+1).' 0 R');
  1205. $this->_out('/FontDescriptor '.($this->n+2).' 0 R');
  1206. if($font['enc'])
  1207. {
  1208. if(isset($font['diff']))
  1209. $this->_out('/Encoding '.($nf+$font['diff']).' 0 R');
  1210. else
  1211. $this->_out('/Encoding /WinAnsiEncoding');
  1212. }
  1213. $this->_out('>>');
  1214. $this->_out('endobj');
  1215. //Widths
  1216. $this->_newobj();
  1217. $cw=&$font['cw'];
  1218. $s='[';
  1219. for($i=32;$i<=255;$i++)
  1220. $s.=$cw[chr($i)].' ';
  1221. $this->_out($s.']');
  1222. $this->_out('endobj');
  1223. //Descriptor
  1224. $this->_newobj();
  1225. $s='<</Type /FontDescriptor /FontName /'.$name;
  1226. foreach($font['desc'] as $k=>$v)
  1227. $s.=' /'.$k.' '.$v;
  1228. $file=$font['file'];
  1229. if($file)
  1230. $s.=' /FontFile'.($type=='Type1' ? '' : '2').' '.$this->FontFiles[$file]['n'].' 0 R';
  1231. $this->_out($s.'>>');
  1232. $this->_out('endobj');
  1233. }
  1234. else
  1235. {
  1236. //Allow for additional types
  1237. $mtd='_put'.strtolower($type);
  1238. if(!method_exists($this,$mtd))
  1239. $this->Error('Unsupported font type: '.$type);
  1240. $this->$mtd($font);
  1241. }
  1242. }
  1243. }
  1244. function _putimages()
  1245. {
  1246. $filter=($this->compress) ? '/Filter /FlateDecode ' : '';
  1247. reset($this->images);
  1248. while(list($file,$info)=each($this->images))
  1249. {
  1250. $this->_newobj();
  1251. $this->images[$file]['n']=$this->n;
  1252. $this->_out('<</Type /XObject');
  1253. $this->_out('/Subtype /Image');
  1254. $this->_out('/Width '.$info['w']);
  1255. $this->_out('/Height '.$info['h']);
  1256. if($info['cs']=='Indexed')
  1257. $this->_out('/ColorSpace [/Indexed /DeviceRGB '.(strlen($info['pal'])/3-1).' '.($this->n+1).' 0 R]');
  1258. else
  1259. {
  1260. $this->_out('/ColorSpace /'.$info['cs']);
  1261. if($info['cs']=='DeviceCMYK')
  1262. $this->_out('/Decode [1 0 1 0 1 0 1 0]');
  1263. }
  1264. $this->_out('/BitsPerComponent '.$info['bpc']);
  1265. if(isset($info['f']))
  1266. $this->_out('/Filter /'.$info['f']);
  1267. if(isset($info['parms']))
  1268. $this->_out($info['parms']);
  1269. if(isset($info['trns']) && is_array($info['trns']))
  1270. {
  1271. $trns='';
  1272. for($i=0;$i<count($info['trns']);$i++)
  1273. $trns.=$info['trns'][$i].' '.$info['trns'][$i].' ';
  1274. $this->_out('/Mask ['.$trns.']');
  1275. }
  1276. $this->_out('/Length '.strlen($info['data']).'>>');
  1277. $this->_putstream($info['data']);
  1278. unset($this->images[$file]['data']);
  1279. $this->_out('endobj');
  1280. //Palette
  1281. if($info['cs']=='Indexed')
  1282. {
  1283. $this->_newobj();
  1284. $pal=($this->compress) ? gzcompress($info['pal']) : $info['pal'];
  1285. $this->_out('<<'.$filter.'/Length '.strlen($pal).'>>');
  1286. $this->_putstream($pal);
  1287. $this->_out('endobj');
  1288. }
  1289. }
  1290. }
  1291. function _putxobjectdict()
  1292. {
  1293. foreach($this->images as $image)
  1294. $this->_out('/I'.$image['i'].' '.$image['n'].' 0 R');
  1295. }
  1296. function _putresourcedict()
  1297. {
  1298. $this->_out('/ProcSet [/PDF /Text /ImageB /ImageC /ImageI]');
  1299. $this->_out('/Font <<');
  1300. foreach($this->fonts as $font)
  1301. $this->_out('/F'.$font['i'].' '.$font['n'].' 0 R');
  1302. $this->_out('>>');
  1303. $this->_out('/XObject <<');
  1304. $this->_putxobjectdict();
  1305. $this->_out('>>');
  1306. }
  1307. function _putresources()
  1308. {
  1309. $this->_putfonts();
  1310. $this->_putimages();
  1311. //Resource dictionary
  1312. $this->offsets[2]=strlen($this->buffer);
  1313. $this->_out('2 0 obj');
  1314. $this->_out('<<');
  1315. $this->_putresourcedict();
  1316. $this->_out('>>');
  1317. $this->_out('endobj');
  1318. }
  1319. function _putinfo()
  1320. {
  1321. $this->_out('/Producer '.$this->_textstring('FPDF '.FPDF_VERSION));
  1322. if(!empty($this->title))
  1323. $this->_out('/Title '.$this->_textstring($this->title));
  1324. if(!empty($this->subject))
  1325. $this->_out('/Subject '.$this->_textstring($this->subject));
  1326. if(!empty($this->author))
  1327. $this->_out('/Author '.$this->_textstring($this->author));
  1328. if(!empty($this->keywords))
  1329. $this->_out('/Keywords '.$this->_textstring($this->keywords));
  1330. if(!empty($this->creator))
  1331. $this->_out('/Creator '.$this->_textstring($this->creator));
  1332. $this->_out('/CreationDate '.$this->_textstring('D:'.date('YmdHis')));
  1333. }
  1334. function _putcatalog()
  1335. {
  1336. $this->_out('/Type /Catalog');
  1337. $this->_out('/Pages 1 0 R');
  1338. if($this->ZoomMode=='fullpage')
  1339. $this->_out('/OpenAction [3 0 R /Fit]');
  1340. elseif($this->ZoomMode=='fullwidth')
  1341. $this->_out('/OpenAction [3 0 R /FitH null]');
  1342. elseif($this->ZoomMode=='real')
  1343. $this->_out('/OpenAction [3 0 R /XYZ null null 1]');
  1344. elseif(!is_string($this->ZoomMode))
  1345. $this->_out('/OpenAction [3 0 R /XYZ null null '.($this->ZoomMode/100).']');
  1346. if($this->LayoutMode=='single')
  1347. $this->_out('/PageLayout /SinglePage');
  1348. elseif($this->LayoutMode=='continuous')
  1349. $this->_out('/PageLayout /OneColumn');
  1350. elseif($this->LayoutMode=='two')
  1351. $this->_out('/PageLayout /TwoColumnLeft');
  1352. }
  1353. function _putheader()
  1354. {
  1355. $this->_out('%PDF-'.$this->PDFVersion);
  1356. }
  1357. function _puttrailer()
  1358. {
  1359. $this->_out('/Size '.($this->n+1));
  1360. $this->_out('/Root '.$this->n.' 0 R');
  1361. $this->_out('/Info '.($this->n-1).' 0 R');
  1362. }
  1363. function _enddoc()
  1364. {
  1365. $this->_putheader();
  1366. $this->_putpages();
  1367. $this->_putresources();
  1368. //Info
  1369. $this->_newobj();
  1370. $this->_out('<<');
  1371. $this->_putinfo();
  1372. $this->_out('>>');
  1373. $this->_out('endobj');
  1374. //Catalog
  1375. $this->_newobj();
  1376. $this->_out('<<');
  1377. $this->_putcatalog();
  1378. $this->_out('>>');
  1379. $this->_out('endobj');
  1380. //Cross-ref
  1381. $o=strlen($this->buffer);
  1382. $this->_out('xref');
  1383. $this->_out('0 '.($this->n+1));
  1384. $this->_out('0000000000 65535 f ');
  1385. for($i=1;$i<=$this->n;$i++)
  1386. $this->_out(sprintf('%010d 00000 n ',$this->offsets[$i]));
  1387. //Trailer
  1388. $this->_out('trailer');
  1389. $this->_out('<<');
  1390. $this->_puttrailer();
  1391. $this->_out('>>');
  1392. $this->_out('startxref');
  1393. $this->_out($o);
  1394. $this->_out('%%EOF');
  1395. $this->state=3;
  1396. }
  1397. function _beginpage($orientation)
  1398. {
  1399. $this->page++;
  1400. $this->pages[$this->page]='';
  1401. $this->state=2;
  1402. $this->x=$this->lMargin;
  1403. $this->y=$this->tMargin;
  1404. $this->FontFamily='';
  1405. //Page orientation
  1406. if(!$orientation)
  1407. $orientation=$this->DefOrientation;
  1408. else
  1409. {
  1410. $orientation=strtoupper($orientation{0});
  1411. if($orientation!=$this->DefOrientation)
  1412. $this->OrientationChanges[$this->page]=true;
  1413. }
  1414. if($orientation!=$this->CurOrientation)
  1415. {
  1416. //Change orientation
  1417. if($orientation=='P')
  1418. {
  1419. $this->wPt=$this->fwPt;
  1420. $this->hPt=$this->fhPt;
  1421. $this->w=$this->fw;
  1422. $this->h=$this->fh;
  1423. }
  1424. else
  1425. {
  1426. $this->wPt=$this->fhPt;
  1427. $this->hPt=$this->fwPt;
  1428. $this->w=$this->fh;
  1429. $this->h=$this->fw;
  1430. }
  1431. $this->PageBreakTrigger=$this->h-$this->bMargin;
  1432. $this->CurOrientation=$orientation;
  1433. }
  1434. }
  1435. function _endpage()
  1436. {
  1437. //End of page contents
  1438. $this->state=1;
  1439. }
  1440. function _newobj()
  1441. {
  1442. //Begin a new object
  1443. $this->n++;
  1444. $this->offsets[$this->n]=strlen($this->buffer);
  1445. $this->_out($this->n.' 0 obj');
  1446. }
  1447. function _dounderline($x,$y,$txt)
  1448. {
  1449. //Underline text
  1450. $up=$this->CurrentFont['up'];
  1451. $ut=$this->CurrentFont['ut'];
  1452. $w=$this->GetStringWidth($txt)+$this->ws*substr_count($txt,' ');
  1453. return sprintf('%.2f %.2f %.2f %.2f re f',$x*$this->k,($this->h-($y-$up/1000*$this->FontSize))*$this->k,$w*$this->k,-$ut/1000*$this->FontSizePt);
  1454. }
  1455. function _parsejpg($file)
  1456. {
  1457. //Extract info from a JPEG file
  1458. $a=GetImageSize($file);
  1459. if(!$a)
  1460. $this->Error('Missing or incorrect image file: '.$file);
  1461. if($a[2]!=2)
  1462. $this->Error('Not a JPEG file: '.$file);
  1463. if(!isset($a['channels']) || $a['channels']==3)
  1464. $colspace='DeviceRGB';
  1465. elseif($a['channels']==4)
  1466. $colspace='DeviceCMYK';
  1467. else
  1468. $colspace='DeviceGray';
  1469. $bpc=isset($a['bits']) ? $a['bits'] : 8;
  1470. //Read whole file
  1471. $f=fopen($file,'rb');
  1472. $data='';
  1473. while(!feof($f))
  1474. $data.=fread($f,4096);
  1475. fclose($f);
  1476. return array('w'=>$a[0],'h'=>$a[1],'cs'=>$colspace,'bpc'=>$bpc,'f'=>'DCTDecode','data'=>$data);
  1477. }
  1478. function _parsepng($file)
  1479. {
  1480. //Extract info from a PNG file
  1481. $f=fopen($file,'rb');
  1482. if(!$f)
  1483. $this->Error('Can\'t open image file: '.$file);
  1484. //Check signature
  1485. if(fread($f,8)!=chr(137).'PNG'.chr(13).chr(10).chr(26).chr(10))
  1486. $this->Error('Not a PNG file: '.$file);
  1487. //Read header chunk
  1488. fread($f,4);
  1489. if(fread($f,4)!='IHDR')
  1490. $this->Error('Incorrect PNG file: '.$file);
  1491. $w=$this->_freadint($f);
  1492. $h=$this->_freadint($f);
  1493. $bpc=ord(fread($f,1));
  1494. if($bpc>8)
  1495. $this->Error('16-bit depth not supported: '.$file);
  1496. $ct=ord(fread($f,1));
  1497. if($ct==0)
  1498. $colspace='DeviceGray';
  1499. elseif($ct==2)
  1500. $colspace='DeviceRGB';
  1501. elseif($ct==3)
  1502. $colspace='Indexed';
  1503. else
  1504. $this->Error('Alpha channel not supported: '.$file);
  1505. if(ord(fread($f,1))!=0)
  1506. $this->Error('Unknown compression method: '.$file);
  1507. if(ord(fread($f,1))!=0)
  1508. $this->Error('Unknown filter method: '.$file);
  1509. if(ord(fread($f,1))!=0)
  1510. $this->Error('Interlacing not supported: '.$file);
  1511. fread($f,4);
  1512. $parms='/DecodeParms <</Predictor 15 /Colors '.($ct==2 ? 3 : 1).' /BitsPerComponent '.$bpc.' /Columns '.$w.'>>';
  1513. //Scan chunks looking for palette, transparency and image data
  1514. $pal='';
  1515. $trns='';
  1516. $data='';
  1517. do
  1518. {
  1519. $n=$this->_freadint($f);
  1520. $type=fread($f,4);
  1521. if($type=='PLTE')
  1522. {
  1523. //Read palette
  1524. $pal=fread($f,$n);
  1525. fread($f,4);
  1526. }
  1527. elseif($type=='tRNS')
  1528. {
  1529. //Read transparency info
  1530. $t=fread($f,$n);
  1531. if($ct==0)
  1532. $trns=array(ord(substr($t,1,1)));
  1533. elseif($ct==2)
  1534. $trns=array(ord(substr($t,1,1)),ord(substr($t,3,1)),ord(substr($t,5,1)));
  1535. else
  1536. {
  1537. $pos=strpos($t,chr(0));
  1538. if($pos!==false)
  1539. $trns=array($pos);
  1540. }
  1541. fread($f,4);
  1542. }
  1543. elseif($type=='IDAT')
  1544. {
  1545. //Read image data block
  1546. $data.=fread($f,$n);
  1547. fread($f,4);
  1548. }
  1549. elseif($type=='IEND')
  1550. break;
  1551. else
  1552. fread($f,$n+4);
  1553. }
  1554. while($n);
  1555. if($colspace=='Indexed' && empty($pal))
  1556. $this->Error('Missing palette in '.$file);
  1557. fclose($f);
  1558. return array('w'=>$w,'h'=>$h,'cs'=>$colspace,'bpc'=>$bpc,'f'=>'FlateDecode','parms'=>$parms,'pal'=>$pal,'trns'=>$trns,'data'=>$data);
  1559. }
  1560. function _freadint($f)
  1561. {
  1562. //Read a 4-byte integer from file
  1563. $a=unpack('Ni',fread($f,4));
  1564. return $a['i'];
  1565. }
  1566. function _textstring($s)
  1567. {
  1568. //Format a text string
  1569. return '('.$this->_escape($s).')';
  1570. }
  1571. function _escape($s)
  1572. {
  1573. //Add \ before \, ( and )
  1574. return str_replace(')','\\)',str_replace('(','\\(',str_replace('\\','\\\\',$s)));
  1575. }
  1576. function _putstream($s)
  1577. {
  1578. $this->_out('stream');
  1579. $this->_out($s);
  1580. $this->_out('endstream');
  1581. }
  1582. function _out($s)
  1583. {
  1584. //Add a line to the document
  1585. if($this->state==2)
  1586. $this->pages[$this->page].=$s."\n";
  1587. else
  1588. $this->buffer.=$s."\n";
  1589. }
  1590. //End of class
  1591. }
  1592. //Handle special IE contype request
  1593. if(isset($_SERVER['HTTP_USER_AGENT']) && $_SERVER['HTTP_USER_AGENT']=='contype')
  1594. {
  1595. header('Content-Type: application/pdf');
  1596. exit;
  1597. }
  1598. }
  1599. ///////////////////////////////////////////////////////
  1600. //// FPDF_TPL START ////
  1601. ///////////////////////////////////////////////////////
  1602. class FPDF_TPL extends FPDF {
  1603. /**
  1604. * Array of Tpl-Data
  1605. * @var array
  1606. */
  1607. var $tpls = array();
  1608. /**
  1609. * Current Template-ID
  1610. * @var int
  1611. */
  1612. var $tpl = 0;
  1613. /**
  1614. * "In Template"-Flag
  1615. * @var boolean
  1616. */
  1617. var $_intpl = false;
  1618. /**
  1619. * Nameprefix of Templates used in Resources-Dictonary
  1620. * @var string A String defining the Prefix used as Template-Object-Names. Have to beginn with an /
  1621. */
  1622. var $tplprefix = "/TPL";
  1623. /**
  1624. * Resources used By Templates and Pages
  1625. * @var array
  1626. */
  1627. var $_res = array();
  1628. /**
  1629. * Constructor
  1630. * See FPDF-Documentation
  1631. * @param string $orientation
  1632. * @param string $unit
  1633. * @param mixed $format
  1634. */
  1635. function fpdf_tpl($orientation='P',$unit='mm',$format='A4') {
  1636. parent::fpdf($orientation,$unit,$format);
  1637. }
  1638. /**
  1639. * Start a Template
  1640. *
  1641. * This method starts a template. You can give own coordinates to build an own sized
  1642. * Template. Pay attention, that the margins are adapted to the new templatesize.
  1643. * If you want to write outside the template, for example to build a clipped Template,
  1644. * you have to set the Margins and "Cursor"-Position manual after beginTemplate-Call.
  1645. *
  1646. * If no parameter is given, the template uses the current page-size.
  1647. * The Method returns an ID of the current Template. This ID is used later for using this template.
  1648. * Warning: A created Template is used in PDF at all events. Still if you don't use it after creation!
  1649. *
  1650. * @param int $x The x-coordinate given in user-unit
  1651. * @param int $y The y-coordinate given in user-unit
  1652. * @param int $w The width given in user-unit
  1653. * @param int $h The height given in user-unit
  1654. * @return int The ID of new created Template
  1655. */
  1656. function beginTemplate($x=null,$y=null,$w=null,$h=null) {
  1657. if ($this->page <= 0)
  1658. $this->error("You have to add a page to fpdf first!");
  1659. if ($x == null)
  1660. $x = 0;
  1661. if ($y == null)
  1662. $y = 0;
  1663. if ($w == null)
  1664. $w = $this->w;
  1665. if ($h == null)
  1666. $h = $this->h;
  1667. // Save settings
  1668. $this->tpl++;
  1669. $tpl =& $this->tpls[$this->tpl];
  1670. $tpl = array(
  1671. 'o_x' => $this->x,
  1672. 'o_y' => $this->y,
  1673. 'o_AutoPageBreak' => $this->AutoPageBreak,
  1674. 'o_bMargin' => $this->bMargin,
  1675. 'o_tMargin' => $this->tMargin,
  1676. 'o_lMargin' => $this->lMargin,
  1677. 'o_rMargin' => $this->rMargin,
  1678. 'o_h' => $this->h,
  1679. 'o_w' => $this->w,
  1680. 'buffer' => '',
  1681. 'x' => $x,
  1682. 'y' => $y,
  1683. 'w' => $w,
  1684. 'h' => $h
  1685. );
  1686. $this->SetAutoPageBreak(false);
  1687. // Define own high and width to calculate possitions correct
  1688. $this->h = $h;
  1689. $this->w = $w;
  1690. $this->_intpl = true;
  1691. $this->SetXY($x+$this->lMargin,$y+$this->tMargin);
  1692. $this->SetRightMargin($this->w-$w+$this->rMargin);
  1693. return $this->tpl;
  1694. }
  1695. /**
  1696. * End Template
  1697. *
  1698. * This method ends a template and reset initiated variables on beginTemplate.
  1699. *
  1700. * @return mixed If a template is opened, the ID is returned. If not a false is returned.
  1701. */
  1702. function endTemplate() {
  1703. if ($this->_intpl) {
  1704. $this->_intpl = false;
  1705. $tpl =& $this->tpls[$this->tpl];
  1706. $this->SetXY($tpl['o_x'], $tpl['o_y']);
  1707. $this->tMargin = $tpl['o_tMargin'];
  1708. $this->lMargin = $tpl['o_lMargin'];
  1709. $this->rMargin = $tpl['o_rMargin'];
  1710. $this->h = $tpl['o_h'];
  1711. $this->w = $tpl['o_w'];
  1712. $this->Se

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