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

/include/fpdf/fpdf.php

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