PageRenderTime 62ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/pmfv2-1-0-alpha-1/fpdf/fpdf.php

https://github.com/redbugz/rootstech2013
PHP | 1995 lines | 1569 code | 100 blank | 326 comment | 253 complexity | eb5ddd93194699723f44dfd060319262 MD5 | raw file
Possible License(s): AGPL-1.0, BSD-3-Clause

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

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

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