fpdf.php
上传用户:gzy2002
上传日期:2010-02-11
资源大小:1785k
文件大小:39k
源码类别:

电子政务应用

开发平台:

Java

  1. <?php
  2. /****************************************************************************
  3. * Software: FPDF                                                            *
  4. * Version:  1.51                                                            *
  5. * Date:     2002/08/03                                                      *
  6. * Author:   Olivier PLATHEY                                                 *
  7. * License:  Freeware                                                        *
  8. *                                                                           *
  9. * You may use and modify this software as you wish.                         *
  10. ****************************************************************************/
  11. # $Id: fpdf.php,v 1.2 2003/03/01 07:34:33 wwf Exp $ 
  12. define('FPDF_VERSION','1.51');
  13. class FPDF
  14. {
  15. //Private properties
  16. var $page;               //current page number
  17. var $n;                  //current object number
  18. var $offsets;            //array of object offsets
  19. var $buffer;             //buffer holding in-memory PDF
  20. var $pages;              //array containing pages
  21. var $state;              //current document state
  22. var $compress;           //compression flag
  23. var $DefOrientation;     //default orientation
  24. var $CurOrientation;     //current orientation
  25. var $OrientationChanges; //array indicating orientation changes
  26. var $fwPt,$fhPt;         //dimensions of page format in points
  27. var $fw,$fh;             //dimensions of page format in user unit
  28. var $wPt,$hPt;           //current dimensions of page in points
  29. var $k;                  //scale factor (number of points in user unit)
  30. var $w,$h;               //current dimensions of page in user unit
  31. var $lMargin;            //left margin
  32. var $tMargin;            //top margin
  33. var $rMargin;            //right margin
  34. var $bMargin;            //page break margin
  35. var $cMargin;            //cell margin
  36. var $x,$y;               //current position in user unit for cell positionning
  37. var $lasth;              //height of last cell printed
  38. var $LineWidth;          //line width in user unit
  39. var $CoreFonts;          //array of standard font names
  40. var $fonts;              //array of used fonts
  41. var $FontFiles;          //array of font files
  42. var $diffs;              //array of encoding differences
  43. var $images;             //array of used images
  44. var $PageLinks;          //array of links in pages
  45. var $links;              //array of internal links
  46. var $FontFamily;         //current font family
  47. var $FontStyle;          //current font style
  48. var $underline;          //underlining flag
  49. var $CurrentFont;        //current font info
  50. var $FontSizePt;         //current font size in points
  51. var $FontSize;           //current font size in user unit
  52. var $DrawColor;          //commands for drawing color
  53. var $FillColor;          //commands for filling color
  54. var $TextColor;          //commands for text color
  55. var $ColorFlag;          //indicates whether fill and text colors are different
  56. var $ws;                 //word spacing
  57. var $AutoPageBreak;      //automatic page breaking
  58. var $PageBreakTrigger;   //threshold used to trigger page breaks
  59. var $InFooter;           //flag set when processing footer
  60. var $ZoomMode;           //zoom display mode
  61. var $LayoutMode;         //layout display mode
  62. var $title;              //title
  63. var $subject;            //subject
  64. var $author;             //author
  65. var $keywords;           //keywords
  66. var $creator;            //creator
  67. var $AliasNbPages;       //alias for total number of pages
  68. /****************************************************************************
  69. *                                                                           *
  70. *                              Public methods                               *
  71. *                                                                           *
  72. ****************************************************************************/
  73. function FPDF($orientation='P',$unit='mm',$format='A4')
  74. {
  75. //Check for PHP locale-related bug
  76. if(1.1==1)
  77. $this->Error('Don't alter the locale before including class file');
  78. //Initialization of properties
  79. $this->page=0;
  80. $this->n=2;
  81. $this->buffer='';
  82. $this->pages=array();
  83. $this->OrientationChanges=array();
  84. $this->state=0;
  85. $this->fonts=array();
  86. $this->FontFiles=array();
  87. $this->diffs=array();
  88. $this->images=array();
  89. $this->links=array();
  90. $this->InFooter=false;
  91. $this->FontFamily='';
  92. $this->FontStyle='';
  93. $this->FontSizePt=12;
  94. $this->underline=false;
  95. $this->DrawColor='0 G';
  96. $this->FillColor='0 g';
  97. $this->TextColor='0 g';
  98. $this->ColorFlag=false;
  99. $this->ws=0;
  100. //Standard fonts
  101. $this->CoreFonts['courier']='Courier';
  102. $this->CoreFonts['courierB']='Courier-Bold';
  103. $this->CoreFonts['courierI']='Courier-Oblique';
  104. $this->CoreFonts['courierBI']='Courier-BoldOblique';
  105. $this->CoreFonts['helvetica']='Helvetica';
  106. $this->CoreFonts['helveticaB']='Helvetica-Bold';
  107. $this->CoreFonts['helveticaI']='Helvetica-Oblique';
  108. $this->CoreFonts['helveticaBI']='Helvetica-BoldOblique';
  109. $this->CoreFonts['times']='Times-Roman';
  110. $this->CoreFonts['timesB']='Times-Bold';
  111. $this->CoreFonts['timesI']='Times-Italic';
  112. $this->CoreFonts['timesBI']='Times-BoldItalic';
  113. $this->CoreFonts['symbol']='Symbol';
  114. $this->CoreFonts['zapfdingbats']='ZapfDingbats';
  115. //Scale factor
  116. if($unit=='pt')
  117. $this->k=1;
  118. elseif($unit=='mm')
  119. $this->k=72/25.4;
  120. elseif($unit=='cm')
  121. $this->k=72/2.54;
  122. elseif($unit=='in')
  123. $this->k=72;
  124. else
  125. $this->Error('Incorrect unit: '.$unit);
  126. //Page format
  127. if(is_string($format))
  128. {
  129. $format=strtolower($format);
  130. if($format=='a3')
  131. $format=array(841.89,1190.55);
  132. elseif($format=='a4')
  133. $format=array(595.28,841.89);
  134. elseif($format=='a5')
  135. $format=array(420.94,595.28);
  136. elseif($format=='letter')
  137. $format=array(612,792);
  138. elseif($format=='legal')
  139. $format=array(612,1008);
  140. else
  141. $this->Error('Unknown page format: '.$format);
  142. $this->fwPt=$format[0];
  143. $this->fhPt=$format[1];
  144. }
  145. else
  146. {
  147. $this->fwPt=$format[0]*$this->k;
  148. $this->fhPt=$format[1]*$this->k;
  149. }
  150. $this->fw=$this->fwPt/$this->k;
  151. $this->fh=$this->fhPt/$this->k;
  152. //Page orientation
  153. $orientation=strtolower($orientation);
  154. if($orientation=='p' or $orientation=='portrait')
  155. {
  156. $this->DefOrientation='P';
  157. $this->wPt=$this->fwPt;
  158. $this->hPt=$this->fhPt;
  159. }
  160. elseif($orientation=='l' or $orientation=='landscape')
  161. {
  162. $this->DefOrientation='L';
  163. $this->wPt=$this->fhPt;
  164. $this->hPt=$this->fwPt;
  165. }
  166. else
  167. $this->Error('Incorrect orientation: '.$orientation);
  168. $this->CurOrientation=$this->DefOrientation;
  169. $this->w=$this->wPt/$this->k;
  170. $this->h=$this->hPt/$this->k;
  171. //Page margins (1 cm)
  172. $margin=28.35/$this->k;
  173. $this->SetMargins($margin,$margin);
  174. //Interior cell margin (1 mm)
  175. $this->cMargin=$margin/10;
  176. //Line width (0.2 mm)
  177. $this->LineWidth=.567/$this->k;
  178. //Automatic page break
  179. $this->SetAutoPageBreak(true,2*$margin);
  180. //Full width display mode
  181. $this->SetDisplayMode('fullwidth');
  182. //Compression
  183. $this->SetCompression(true);
  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 and $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' or $zoom=='fullwidth' or $zoom=='real' or $zoom=='default' or !is_string($zoom))
  222. $this->ZoomMode=$zoom;
  223. elseif($zoom=='zoom')
  224. $this->ZoomMode=$layout;
  225. else
  226. $this->Error('Incorrect zoom display mode: '.$zoom);
  227. if($layout=='single' or $layout=='continuous' or $layout=='two' or $layout=='default')
  228. $this->LayoutMode=$layout;
  229. elseif($zoom!='zoom')
  230. $this->Error('Incorrect layout display mode: '.$layout);
  231. }
  232. function SetCompression($compress)
  233. {
  234. //Set page compression
  235. if(function_exists('gzcompress'))
  236. $this->compress=$compress;
  237. else
  238. $this->compress=false;
  239. }
  240. function SetTitle($title)
  241. {
  242. //Title of document
  243. $this->title=$title;
  244. }
  245. function SetSubject($subject)
  246. {
  247. //Subject of document
  248. $this->subject=$subject;
  249. }
  250. function SetAuthor($author)
  251. {
  252. //Author of document
  253. $this->author=$author;
  254. }
  255. function SetKeywords($keywords)
  256. {
  257. //Keywords of document
  258. $this->keywords=$keywords;
  259. }
  260. function SetCreator($creator)
  261. {
  262. //Creator of document
  263. $this->creator=$creator;
  264. }
  265. function AliasNbPages($alias='{nb}')
  266. {
  267. //Define an alias for total number of pages
  268. $this->AliasNbPages=$alias;
  269. }
  270. function Error($msg)
  271. {
  272. //Fatal error
  273. die('<B>FPDF error: </B>'.$msg);
  274. }
  275. function Open()
  276. {
  277. //Begin document
  278. $this->_begindoc();
  279. }
  280. function Close()
  281. {
  282. //Terminate document
  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. $family=$this->FontFamily;
  298. $style=$this->FontStyle.($this->underline ? 'U' : '');
  299. $size=$this->FontSizePt;
  300. $lw=$this->LineWidth;
  301. $dc=$this->DrawColor;
  302. $fc=$this->FillColor;
  303. $tc=$this->TextColor;
  304. $cf=$this->ColorFlag;
  305. if($this->page>0)
  306. {
  307. //Page footer
  308. $this->InFooter=true;
  309. $this->Footer();
  310. $this->InFooter=false;
  311. //Close page
  312. $this->_endpage();
  313. }
  314. //Start new page
  315. $this->_beginpage($orientation);
  316. //Set line cap style to square
  317. $this->_out('2 J');
  318. //Set line width
  319. $this->LineWidth=$lw;
  320. $this->_out(sprintf('%.2f w',$lw*$this->k));
  321. //Set font
  322. if($family)
  323. $this->SetFont($family,$style,$size);
  324. //Set colors
  325. $this->DrawColor=$dc;
  326. if($dc!='0 G')
  327. $this->_out($dc);
  328. $this->FillColor=$fc;
  329. if($fc!='0 g')
  330. $this->_out($fc);
  331. $this->TextColor=$tc;
  332. $this->ColorFlag=$cf;
  333. //Page header
  334. $this->Header();
  335. //Restore line width
  336. if($this->LineWidth!=$lw)
  337. {
  338. $this->LineWidth=$lw;
  339. $this->_out(sprintf('%.2f w',$lw*$this->k));
  340. }
  341. //Restore font
  342. if($family)
  343. $this->SetFont($family,$style,$size);
  344. //Restore colors
  345. if($this->DrawColor!=$dc)
  346. {
  347. $this->DrawColor=$dc;
  348. $this->_out($dc);
  349. }
  350. if($this->FillColor!=$fc)
  351. {
  352. $this->FillColor=$fc;
  353. $this->_out($fc);
  354. }
  355. $this->TextColor=$tc;
  356. $this->ColorFlag=$cf;
  357. }
  358. function Header()
  359. {
  360. //To be implemented in your own inherited class
  361. }
  362. function Footer()
  363. {
  364. //To be implemented in your own inherited class
  365. }
  366. function PageNo()
  367. {
  368. //Get current page number
  369. return $this->page;
  370. }
  371. function SetDrawColor($r,$g=-1,$b=-1)
  372. {
  373. //Set color for all stroking operations
  374. if(($r==0 and $g==0 and $b==0) or $g==-1)
  375. $this->DrawColor=sprintf('%.3f G',$r/255);
  376. else
  377. $this->DrawColor=sprintf('%.3f %.3f %.3f RG',$r/255,$g/255,$b/255);
  378. if($this->page>0)
  379. $this->_out($this->DrawColor);
  380. }
  381. function SetFillColor($r,$g=-1,$b=-1)
  382. {
  383. //Set color for all filling operations
  384. if(($r==0 and $g==0 and $b==0) or $g==-1)
  385. $this->FillColor=sprintf('%.3f g',$r/255);
  386. else
  387. $this->FillColor=sprintf('%.3f %.3f %.3f rg',$r/255,$g/255,$b/255);
  388. $this->ColorFlag=($this->FillColor!=$this->TextColor);
  389. if($this->page>0)
  390. $this->_out($this->FillColor);
  391. }
  392. function SetTextColor($r,$g=-1,$b=-1)
  393. {
  394. //Set color for text
  395. if(($r==0 and $g==0 and $b==0) or $g==-1)
  396. $this->TextColor=sprintf('%.3f g',$r/255);
  397. else
  398. $this->TextColor=sprintf('%.3f %.3f %.3f rg',$r/255,$g/255,$b/255);
  399. $this->ColorFlag=($this->FillColor!=$this->TextColor);
  400. }
  401. function GetStringWidth($s)
  402. {
  403. //Get width of a string in the current font
  404. $s=(string)$s;
  405. $cw=&$this->CurrentFont['cw'];
  406. $w=0;
  407. $l=strlen($s);
  408. for($i=0;$i<$l;$i++)
  409. $w+=$cw[$s{$i}];
  410. return $w*$this->FontSize/1000;
  411. }
  412. function SetLineWidth($width)
  413. {
  414. //Set line width
  415. $this->LineWidth=$width;
  416. if($this->page>0)
  417. $this->_out(sprintf('%.2f w',$width*$this->k));
  418. }
  419. function Line($x1,$y1,$x2,$y2)
  420. {
  421. //Draw a line
  422. $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));
  423. }
  424. function Rect($x,$y,$w,$h,$style='')
  425. {
  426. //Draw a rectangle
  427. if($style=='F')
  428. $op='f';
  429. elseif($style=='FD' or $style=='DF')
  430. $op='B';
  431. else
  432. $op='S';
  433. $this->_out(sprintf('%.2f %.2f %.2f %.2f re %s',$x*$this->k,($this->h-$y)*$this->k,$w*$this->k,-$h*$this->k,$op));
  434. }
  435. function AddFont($family,$style='',$file='')
  436. {
  437. //Add a TrueType or Type1 font
  438. $family=strtolower($family);
  439. if($family=='arial')
  440. $family='helvetica';
  441. $style=strtoupper($style);
  442. if($style=='IB')
  443. $style='BI';
  444. if(isset($this->fonts[$family.$style]))
  445. $this->Error('Font already added: '.$family.' '.$style);
  446. if($file=='')
  447. $file=str_replace(' ','',$family).strtolower($style).'.php';
  448. if(defined('FPDF_FONTPATH'))
  449. $file=FPDF_FONTPATH.$file;
  450. include($file);
  451. if(!isset($name))
  452. $this->Error('Could not include font definition file');
  453. $i=count($this->fonts)+1;
  454. $this->fonts[$family.$style]=array('i'=>$i,'type'=>$type,'name'=>$name,'desc'=>$desc,'up'=>$up,'ut'=>$ut,'cw'=>$cw,'enc'=>$enc,'file'=>$file);
  455. if($diff)
  456. {
  457. //Search existing encodings
  458. $d=0;
  459. $nb=count($this->diffs);
  460. for($i=1;$i<=$nb;$i++)
  461. if($this->diffs[$i]==$diff)
  462. {
  463. $d=$i;
  464. break;
  465. }
  466. if($d==0)
  467. {
  468. $d=$nb+1;
  469. $this->diffs[$d]=$diff;
  470. }
  471. $this->fonts[$family.$style]['diff']=$d;
  472. }
  473. if($file)
  474. {
  475. if($type=='TrueType')
  476. $this->FontFiles[$file]=array('length1'=>$originalsize);
  477. else
  478. $this->FontFiles[$file]=array('length1'=>$size1,'length2'=>$size2);
  479. }
  480. }
  481. function SetFont($family,$style='',$size=0)
  482. {
  483. //Select a font; size given in points
  484. global $fpdf_charwidths;
  485. $family=strtolower($family);
  486. if($family=='')
  487. $family=$this->FontFamily;
  488. if($family=='arial')
  489. $family='helvetica';
  490. elseif($family=='symbol' or $family=='zapfdingbats')
  491. $style='';
  492. $style=strtoupper($style);
  493. if(is_int(strpos($style,'U')))
  494. {
  495. $this->underline=true;
  496. $style=str_replace('U','',$style);
  497. }
  498. else
  499. $this->underline=false;
  500. if($style=='IB')
  501. $style='BI';
  502. if($size==0)
  503. $size=$this->FontSizePt;
  504. //Test if font is already selected
  505. if($this->FontFamily==$family and $this->FontStyle==$style and $this->FontSizePt==$size)
  506. return;
  507. //Test if used for the first time
  508. $fontkey=$family.$style;
  509. if(!isset($this->fonts[$fontkey]))
  510. {
  511. //Check if one of the standard fonts
  512. if(isset($this->CoreFonts[$fontkey]))
  513. {
  514. if(!isset($fpdf_charwidths[$fontkey]))
  515. {
  516. //Load metric file
  517. $file=$family;
  518. if($family=='times' or $family=='helvetica')
  519. $file.=strtolower($style);
  520. $file.='.php';
  521. if(defined('FPDF_FONTPATH'))
  522. $file=FPDF_FONTPATH.$file;
  523. include($file);
  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. $txt=str_replace(')','\)',str_replace('(','\(',str_replace('\','\\',$txt)));
  577. $s=sprintf('BT %.2f %.2f Td (%s) Tj ET',$x*$this->k,($this->h-$y)*$this->k,$txt);
  578. if($this->underline and $txt!='')
  579. $s.=' '.$this->_dounderline($x,$y,$txt);
  580. if($this->ColorFlag)
  581. $s='q '.$this->TextColor.' '.$s.' Q';
  582. $this->_out($s);
  583. }
  584. function AcceptPageBreak()
  585. {
  586. //Accept automatic page break or not
  587. return $this->AutoPageBreak;
  588. }
  589. function Cell($w,$h=0,$txt='',$border=0,$ln=0,$align='',$fill=0,$link='')
  590. {
  591. //Output a cell
  592. $k=$this->k;
  593. if($this->y+$h>$this->PageBreakTrigger and !$this->InFooter and $this->AcceptPageBreak())
  594. {
  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 or $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(is_int(strpos($border,'L')))
  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(is_int(strpos($border,'T')))
  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(is_int(strpos($border,'R')))
  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(is_int(strpos($border,'B')))
  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. $txt=str_replace(')','\)',str_replace('(','\(',str_replace('\','\\',$txt)));
  643. if($this->ColorFlag)
  644. $s.='q '.$this->TextColor.' ';
  645. $s.=sprintf('BT %.2f %.2f Td (%s) Tj ET',($this->x+$dx)*$k,($this->h-($this->y+.5*$h+.3*$this->FontSize))*$k,$txt);
  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 and $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(is_int(strpos($border,'L')))
  690. $b2.='L';
  691. if(is_int(strpos($border,'R')))
  692. $b2.='R';
  693. $b=is_int(strpos($border,'T')) ? $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 and $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 and $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 and is_int(strpos($border,'B')))
  774. $b.='B';
  775. $this->Cell($w,$h,substr($s,$j,$i),$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. {
  814. $sep=$i;
  815. $ls=$l;
  816. }
  817. $l+=$cw[$c];
  818. if($l>$wmax)
  819. {
  820. //Automatic line break
  821. if($sep==-1)
  822. {
  823. if($this->x>$this->lMargin)
  824. {
  825. //Move to next line
  826. $this->x=$this->lMargin;
  827. $this->y+=$h;
  828. $w=$this->w-$this->rMargin-$this->x;
  829. $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
  830. $i++;
  831. $nl++;
  832. continue;
  833. }
  834. if($i==$j)
  835. $i++;
  836. $this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link);
  837. }
  838. else
  839. {
  840. $this->Cell($w,$h,substr($s,$j,$sep-$j),0,2,'',0,$link);
  841. $i=$sep+1;
  842. }
  843. $sep=-1;
  844. $j=$i;
  845. $l=0;
  846. if($nl==1)
  847. {
  848. $this->x=$this->lMargin;
  849. $w=$this->w-$this->rMargin-$this->x;
  850. $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
  851. }
  852. $nl++;
  853. }
  854. else
  855. $i++;
  856. }
  857. //Last chunk
  858. if($i!=$j)
  859. $this->Cell($l/1000*$this->FontSize,$h,substr($s,$j,$i),0,0,'',0,$link);
  860. }
  861. function Image($file,$x,$y,$w,$h=0,$type='',$link='')
  862. {
  863. //Put an image on the page
  864. if(!isset($this->images[$file]))
  865. {
  866. //First use of image, get info
  867. if($type=='')
  868. {
  869. $pos=strrpos($file,'.');
  870. if(!$pos)
  871. $this->Error('Image file has no extension and no type was specified: '.$file);
  872. $type=substr($file,$pos+1);
  873. }
  874. $type=strtolower($type);
  875. $mqr=get_magic_quotes_runtime();
  876. set_magic_quotes_runtime(0);
  877. if($type=='jpg' or $type=='jpeg')
  878. $info=$this->_parsejpg($file);
  879. elseif($type=='png')
  880. $info=$this->_parsepng($file);
  881. else
  882. $this->Error('Unsupported image file type: '.$type);
  883. set_magic_quotes_runtime($mqr);
  884. $info['i']=count($this->images)+1;
  885. $this->images[$file]=$info;
  886. }
  887. else
  888. $info=$this->images[$file];
  889. //Automatic width or height calculation
  890. if($w==0)
  891. $w=$h*$info['w']/$info['h'];
  892. if($h==0)
  893. $h=$w*$info['h']/$info['w'];
  894. $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']));
  895. if($link)
  896. $this->Link($x,$y,$w,$h,$link);
  897. }
  898. function Ln($h='')
  899. {
  900. //Line feed; default value is last cell height
  901. $this->x=$this->lMargin;
  902. if(is_string($h))
  903. $this->y+=$this->lasth;
  904. else
  905. $this->y+=$h;
  906. }
  907. function GetX()
  908. {
  909. //Get x position
  910. return $this->x;
  911. }
  912. function SetX($x)
  913. {
  914. //Set x position
  915. if($x>=0)
  916. $this->x=$x;
  917. else
  918. $this->x=$this->w+$x;
  919. }
  920. function GetY()
  921. {
  922. //Get y position
  923. return $this->y;
  924. }
  925. function SetY($y)
  926. {
  927. //Set y position and reset x
  928. $this->x=$this->lMargin;
  929. if($y>=0)
  930. $this->y=$y;
  931. else
  932. $this->y=$this->h+$y;
  933. }
  934. function SetXY($x,$y)
  935. {
  936. //Set x and y positions
  937. $this->SetY($y);
  938. $this->SetX($x);
  939. }
  940. function Output($file='',$download=false)
  941. {
  942. //Output PDF to file or browser
  943. global $HTTP_ENV_VARS;
  944. if($this->state<3)
  945. $this->Close();
  946. if($file=='')
  947. {
  948. //Send to browser
  949. Header('Content-Type: application/pdf');
  950. if(headers_sent())
  951. $this->Error('Some data has already been output to browser, can't send PDF file');
  952. Header('Content-Length: '.strlen($this->buffer));
  953. Header('Content-disposition: inline; filename=doc.pdf');
  954. echo $this->buffer;
  955. }
  956. else
  957. {
  958. if($download)
  959. {
  960. //Download file
  961. if(isset($HTTP_ENV_VARS['HTTP_USER_AGENT']) and strpos($HTTP_ENV_VARS['HTTP_USER_AGENT'],'MSIE 5.5'))
  962. Header('Content-Type: application/dummy');
  963. else
  964. Header('Content-Type: application/octet-stream');
  965. if(headers_sent())
  966. $this->Error('Some data has already been output to browser, can't send PDF file');
  967. Header('Content-Length: '.strlen($this->buffer));
  968. Header('Content-disposition: attachment; filename='.$file);
  969. echo $this->buffer;
  970. }
  971. else
  972. {
  973. //Save file locally
  974. $f=fopen($file,'wb');
  975. if(!$f)
  976. $this->Error('Unable to create output file: '.$file);
  977. fwrite($f,$this->buffer,strlen($this->buffer));
  978. fclose($f);
  979. }
  980. }
  981. }
  982. /****************************************************************************
  983. *                                                                           *
  984. *                              Private methods                              *
  985. *                                                                           *
  986. ****************************************************************************/
  987. function _begindoc()
  988. {
  989. //Start document
  990. $this->state=1;
  991. $this->_out('%PDF-1.3');
  992. }
  993. function _putpages()
  994. {
  995. $nb=$this->page;
  996. if(!empty($this->AliasNbPages))
  997. {
  998. //Replace number of pages
  999. for($n=1;$n<=$nb;$n++)
  1000. $this->pages[$n]=str_replace($this->AliasNbPages,$nb,$this->pages[$n]);
  1001. }
  1002. if($this->DefOrientation=='P')
  1003. {
  1004. $wPt=$this->fwPt;
  1005. $hPt=$this->fhPt;
  1006. }
  1007. else
  1008. {
  1009. $wPt=$this->fhPt;
  1010. $hPt=$this->fwPt;
  1011. }
  1012. $filter=($this->compress) ? '/Filter /FlateDecode ' : '';
  1013. for($n=1;$n<=$nb;$n++)
  1014. {
  1015. //Page
  1016. $this->_newobj();
  1017. $this->_out('<</Type /Page');
  1018. $this->_out('/Parent 1 0 R');
  1019. if(isset($this->OrientationChanges[$n]))
  1020. $this->_out(sprintf('/MediaBox [0 0 %.2f %.2f]',$hPt,$wPt));
  1021. $this->_out('/Resources 2 0 R');
  1022. if(isset($this->PageLinks[$n]))
  1023. {
  1024. //Links
  1025. $annots='/Annots [';
  1026. foreach($this->PageLinks[$n] as $pl)
  1027. {
  1028. $rect=sprintf('%.2f %.2f %.2f %.2f',$pl[0],$pl[1],$pl[0]+$pl[2],$pl[1]-$pl[3]);
  1029. $annots.='<</Type /Annot /Subtype /Link /Rect ['.$rect.'] /Border [0 0 0] ';
  1030. if(is_string($pl[4]))
  1031. $annots.='/A <</S /URI /URI '.$this->_textstring($pl[4]).'>>>>';
  1032. else
  1033. {
  1034. $l=$this->links[$pl[4]];
  1035. $h=isset($this->OrientationChanges[$l[0]]) ? $wPt : $hPt;
  1036. $annots.=sprintf('/Dest [%d 0 R /XYZ 0 %.2f null]>>',1+2*$l[0],$h-$l[1]*$this->k);
  1037. }
  1038. }
  1039. $this->_out($annots.']');
  1040. }
  1041. $this->_out('/Contents '.($this->n+1).' 0 R>>');
  1042. $this->_out('endobj');
  1043. //Page content
  1044. $p=($this->compress) ? gzcompress($this->pages[$n]) : $this->pages[$n];
  1045. $this->_newobj();
  1046. $this->_out('<<'.$filter.'/Length '.strlen($p).'>>');
  1047. $this->_putstream($p);
  1048. $this->_out('endobj');
  1049. }
  1050. //Pages root
  1051. $this->offsets[1]=strlen($this->buffer);
  1052. $this->_out('1 0 obj');
  1053. $this->_out('<</Type /Pages');
  1054. $kids='/Kids [';
  1055. for($i=0;$i<$nb;$i++)
  1056. $kids.=(3+2*$i).' 0 R ';
  1057. $this->_out($kids.']');
  1058. $this->_out('/Count '.$nb);
  1059. $this->_out(sprintf('/MediaBox [0 0 %.2f %.2f]',$wPt,$hPt));
  1060. $this->_out('>>');
  1061. $this->_out('endobj');
  1062. }
  1063. function _putfonts()
  1064. {
  1065. $nf=$this->n;
  1066. foreach($this->diffs as $diff)
  1067. {
  1068. //Encodings
  1069. $this->_newobj();
  1070. $this->_out('<</Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences ['.$diff.']>>');
  1071. $this->_out('endobj');
  1072. }
  1073. $mqr=get_magic_quotes_runtime();
  1074. set_magic_quotes_runtime(0);
  1075. foreach($this->FontFiles as $file=>$info)
  1076. {
  1077. //Font file embedding
  1078. $this->_newobj();
  1079. $this->FontFiles[$file]['n']=$this->n;
  1080. if(defined('FPDF_FONTPATH'))
  1081. $file=FPDF_FONTPATH.$file;
  1082. $size=filesize($file);
  1083. if(!$size)
  1084. $this->Error('Font file not found');
  1085. $this->_out('<</Length '.$size);
  1086. if(substr($file,-2)=='.z')
  1087. $this->_out('/Filter /FlateDecode');
  1088. $this->_out('/Length1 '.$info['length1']);
  1089. if(isset($info['length2']))
  1090. $this->_out('/Length2 '.$info['length2'].' /Length3 0');
  1091. $this->_out('>>');
  1092. $f=fopen($file,'rb');
  1093. $this->_putstream(fread($f,$size));
  1094. fclose($f);
  1095. $this->_out('endobj');
  1096. }
  1097. set_magic_quotes_runtime($mqr);
  1098. foreach($this->fonts as $k=>$font)
  1099. {
  1100. //Font objects
  1101. $this->_newobj();
  1102. $this->fonts[$k]['n']=$this->n;
  1103. $name=$font['name'];
  1104. $this->_out('<</Type /Font');
  1105. $this->_out('/BaseFont /'.$name);
  1106. if($font['type']=='core')
  1107. {
  1108. //Standard font
  1109. $this->_out('/Subtype /Type1');
  1110. if($name!='Symbol' and $name!='ZapfDingbats')
  1111. $this->_out('/Encoding /WinAnsiEncoding');
  1112. }
  1113. else
  1114. {
  1115. //Additional font
  1116. $this->_out('/Subtype /'.$font['type']);
  1117. $this->_out('/FirstChar 32');
  1118. $this->_out('/LastChar 255');
  1119. $this->_out('/Widths '.($this->n+1).' 0 R');
  1120. $this->_out('/FontDescriptor '.($this->n+2).' 0 R');
  1121. if($font['enc'])
  1122. {
  1123. if(isset($font['diff']))
  1124. $this->_out('/Encoding '.($nf+$font['diff']).' 0 R');
  1125. else
  1126. $this->_out('/Encoding /WinAnsiEncoding');
  1127. }
  1128. }
  1129. $this->_out('>>');
  1130. $this->_out('endobj');
  1131. if($font['type']!='core')
  1132. {
  1133. //Widths
  1134. $this->_newobj();
  1135. $cw=&$font['cw'];
  1136. $s='[';
  1137. for($i=32;$i<=255;$i++)
  1138. $s.=$cw[chr($i)].' ';
  1139. $this->_out($s.']');
  1140. $this->_out('endobj');
  1141. //Descriptor
  1142. $this->_newobj();
  1143. $s='<</Type /FontDescriptor /FontName /'.$name;
  1144. foreach($font['desc'] as $k=>$v)
  1145. $s.=' /'.$k.' '.$v;
  1146. $file=$font['file'];
  1147. if($file)
  1148. $s.=' /FontFile'.($font['type']=='Type1' ? '' : '2').' '.$this->FontFiles[$file]['n'].' 0 R';
  1149. $this->_out($s.'>>');
  1150. $this->_out('endobj');
  1151. }
  1152. }
  1153. }
  1154. function _putimages()
  1155. {
  1156. $filter=($this->compress) ? '/Filter /FlateDecode ' : '';
  1157. foreach($this->images as $file=>$info)
  1158. {
  1159. $this->_newobj();
  1160. $this->images[$file]['n']=$this->n;
  1161. $this->_out('<</Type /XObject');
  1162. $this->_out('/Subtype /Image');
  1163. $this->_out('/Width '.$info['w']);
  1164. $this->_out('/Height '.$info['h']);
  1165. if($info['cs']=='Indexed')
  1166. $this->_out('/ColorSpace [/Indexed /DeviceRGB '.(strlen($info['pal'])/3-1).' '.($this->n+1).' 0 R]');
  1167. else
  1168. {
  1169. $this->_out('/ColorSpace /'.$info['cs']);
  1170. if($info['cs']=='DeviceCMYK')
  1171. $this->_out('/Decode [1 0 1 0 1 0 1 0]');
  1172. }
  1173. $this->_out('/BitsPerComponent '.$info['bpc']);
  1174. $this->_out('/Filter /'.$info['f']);
  1175. if(isset($info['parms']))
  1176. $this->_out($info['parms']);
  1177. if(isset($info['trns']) and is_array($info['trns']))
  1178. {
  1179. $trns='';
  1180. for($i=0;$i<count($info['trns']);$i++)
  1181. $trns.=$info['trns'][$i].' '.$info['trns'][$i].' ';
  1182. $this->_out('/Mask ['.$trns.']');
  1183. }
  1184. $this->_out('/Length '.strlen($info['data']).'>>');
  1185. $this->_putstream($info['data']);
  1186. $this->_out('endobj');
  1187. //Palette
  1188. if($info['cs']=='Indexed')
  1189. {
  1190. $this->_newobj();
  1191. $pal=($this->compress) ? gzcompress($info['pal']) : $info['pal'];
  1192. $this->_out('<<'.$filter.'/Length '.strlen($pal).'>>');
  1193. $this->_putstream($pal);
  1194. $this->_out('endobj');
  1195. }
  1196. }
  1197. }
  1198. function _putresources()
  1199. {
  1200. $this->_putfonts();
  1201. $this->_putimages();
  1202. //Resource dictionary
  1203. $this->offsets[2]=strlen($this->buffer);
  1204. $this->_out('2 0 obj');
  1205. $this->_out('<</ProcSet [/PDF /Text /ImageB /ImageC /ImageI]');
  1206. $this->_out('/Font <<');
  1207. foreach($this->fonts as $font)
  1208. $this->_out('/F'.$font['i'].' '.$font['n'].' 0 R');
  1209. $this->_out('>>');
  1210. if(count($this->images))
  1211. {
  1212. $this->_out('/XObject <<');
  1213. foreach($this->images as $image)
  1214. $this->_out('/I'.$image['i'].' '.$image['n'].' 0 R');
  1215. $this->_out('>>');
  1216. }
  1217. $this->_out('>>');
  1218. $this->_out('endobj');
  1219. }
  1220. function _putinfo()
  1221. {
  1222. $this->_out('/Producer '.$this->_textstring('FPDF '.FPDF_VERSION));
  1223. if(!empty($this->title))
  1224. $this->_out('/Title '.$this->_textstring($this->title));
  1225. if(!empty($this->subject))
  1226. $this->_out('/Subject '.$this->_textstring($this->subject));
  1227. if(!empty($this->author))
  1228. $this->_out('/Author '.$this->_textstring($this->author));
  1229. if(!empty($this->keywords))
  1230. $this->_out('/Keywords '.$this->_textstring($this->keywords));
  1231. if(!empty($this->creator))
  1232. $this->_out('/Creator '.$this->_textstring($this->creator));
  1233. $this->_out('/CreationDate '.$this->_textstring('D:'.date('YmdHis')));
  1234. }
  1235. function _putcatalog()
  1236. {
  1237. $this->_out('/Type /Catalog');
  1238. $this->_out('/Pages 1 0 R');
  1239. if($this->ZoomMode=='fullpage')
  1240. $this->_out('/OpenAction [3 0 R /Fit]');
  1241. elseif($this->ZoomMode=='fullwidth')
  1242. $this->_out('/OpenAction [3 0 R /FitH null]');
  1243. elseif($this->ZoomMode=='real')
  1244. $this->_out('/OpenAction [3 0 R /XYZ null null 1]');
  1245. elseif(!is_string($this->ZoomMode))
  1246. $this->_out('/OpenAction [3 0 R /XYZ null null '.($this->ZoomMode/100).']');
  1247. if($this->LayoutMode=='single')
  1248. $this->_out('/PageLayout /SinglePage');
  1249. elseif($this->LayoutMode=='continuous')
  1250. $this->_out('/PageLayout /OneColumn');
  1251. elseif($this->LayoutMode=='two')
  1252. $this->_out('/PageLayout /TwoColumnLeft');
  1253. }
  1254. function _puttrailer()
  1255. {
  1256. $this->_out('/Size '.($this->n+1));
  1257. $this->_out('/Root '.$this->n.' 0 R');
  1258. $this->_out('/Info '.($this->n-1).' 0 R');
  1259. }
  1260. function _enddoc()
  1261. {
  1262. $this->_putpages();
  1263. $this->_putresources();
  1264. //Info
  1265. $this->_newobj();
  1266. $this->_out('<<');
  1267. $this->_putinfo();
  1268. $this->_out('>>');
  1269. $this->_out('endobj');
  1270. //Catalog
  1271. $this->_newobj();
  1272. $this->_out('<<');
  1273. $this->_putcatalog();
  1274. $this->_out('>>');
  1275. $this->_out('endobj');
  1276. //Cross-ref
  1277. $o=strlen($this->buffer);
  1278. $this->_out('xref');
  1279. $this->_out('0 '.($this->n+1));
  1280. $this->_out('0000000000 65535 f ');
  1281. for($i=1;$i<=$this->n;$i++)
  1282. $this->_out(sprintf('%010d 00000 n ',$this->offsets[$i]));
  1283. //Trailer
  1284. $this->_out('trailer');
  1285. $this->_out('<<');
  1286. $this->_puttrailer();
  1287. $this->_out('>>');
  1288. $this->_out('startxref');
  1289. $this->_out($o);
  1290. $this->_out('%%EOF');
  1291. $this->state=3;
  1292. }
  1293. function _beginpage($orientation)
  1294. {
  1295. $this->page++;
  1296. $this->pages[$this->page]='';
  1297. $this->state=2;
  1298. $this->x=$this->lMargin;
  1299. $this->y=$this->tMargin;
  1300. $this->lasth=0;
  1301. $this->FontFamily='';
  1302. //Page orientation
  1303. if(!$orientation)
  1304. $orientation=$this->DefOrientation;
  1305. else
  1306. {
  1307. $orientation=strtoupper($orientation{0});
  1308. if($orientation!=$this->DefOrientation)
  1309. $this->OrientationChanges[$this->page]=true;
  1310. }
  1311. if($orientation!=$this->CurOrientation)
  1312. {
  1313. //Change orientation
  1314. if($orientation=='P')
  1315. {
  1316. $this->wPt=$this->fwPt;
  1317. $this->hPt=$this->fhPt;
  1318. $this->w=$this->fw;
  1319. $this->h=$this->fh;
  1320. }
  1321. else
  1322. {
  1323. $this->wPt=$this->fhPt;
  1324. $this->hPt=$this->fwPt;
  1325. $this->w=$this->fh;
  1326. $this->h=$this->fw;
  1327. }
  1328. $this->PageBreakTrigger=$this->h-$this->bMargin;
  1329. $this->CurOrientation=$orientation;
  1330. }
  1331. }
  1332. function _endpage()
  1333. {
  1334. //End of page contents
  1335. $this->state=1;
  1336. }
  1337. function _newobj()
  1338. {
  1339. //Begin a new object
  1340. $this->n++;
  1341. $this->offsets[$this->n]=strlen($this->buffer);
  1342. $this->_out($this->n.' 0 obj');
  1343. }
  1344. function _dounderline($x,$y,$txt)
  1345. {
  1346. //Underline text
  1347. $up=$this->CurrentFont['up'];
  1348. $ut=$this->CurrentFont['ut'];
  1349. $w=$this->GetStringWidth($txt)+$this->ws*substr_count($txt,' ');
  1350. 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);
  1351. }
  1352. function _parsejpg($file)
  1353. {
  1354. //Extract info from a JPEG file
  1355. $a=GetImageSize($file);
  1356. if(!$a)
  1357. $this->Error('Missing or incorrect image file: '.$file);
  1358. if($a[2]!=2)
  1359. $this->Error('Not a JPEG file: '.$file);
  1360. if(!isset($a['channels']) or $a['channels']==3)
  1361. $colspace='DeviceRGB';
  1362. elseif($a['channels']==4)
  1363. $colspace='DeviceCMYK';
  1364. else
  1365. $colspace='DeviceGray';
  1366. $bpc=isset($a['bits']) ? $a['bits'] : 8;
  1367. //Read whole file
  1368. $f=fopen($file,'rb');
  1369. $data=fread($f,filesize($file));
  1370. fclose($f);
  1371. return array('w'=>$a[0],'h'=>$a[1],'cs'=>$colspace,'bpc'=>$bpc,'f'=>'DCTDecode','data'=>$data);
  1372. }
  1373. function _parsepng($file)
  1374. {
  1375. //Extract info from a PNG file
  1376. $f=fopen($file,'rb');
  1377. if(!$f)
  1378. $this->Error('Can't open image file: '.$file);
  1379. //Check signature
  1380. if(fread($f,8)!=chr(137).'PNG'.chr(13).chr(10).chr(26).chr(10))
  1381. $this->Error('Not a PNG file: '.$file);
  1382. //Read header chunk
  1383. fread($f,4);
  1384. if(fread($f,4)!='IHDR')
  1385. $this->Error('Incorrect PNG file: '.$file);
  1386. $w=$this->_freadint($f);
  1387. $h=$this->_freadint($f);
  1388. $bpc=ord(fread($f,1));
  1389. if($bpc>8)
  1390. $this->Error('16-bit depth not supported: '.$file);
  1391. $ct=ord(fread($f,1));
  1392. if($ct==0)
  1393. $colspace='DeviceGray';
  1394. elseif($ct==2)
  1395. $colspace='DeviceRGB';
  1396. elseif($ct==3)
  1397. $colspace='Indexed';
  1398. else
  1399. $this->Error('Alpha channel not supported: '.$file);
  1400. if(ord(fread($f,1))!=0)
  1401. $this->Error('Unknown compression method: '.$file);
  1402. if(ord(fread($f,1))!=0)
  1403. $this->Error('Unknown filter method: '.$file);
  1404. if(ord(fread($f,1))!=0)
  1405. $this->Error('Interlacing not supported: '.$file);
  1406. fread($f,4);
  1407. $parms='/DecodeParms <</Predictor 15 /Colors '.($ct==2 ? 3 : 1).' /BitsPerComponent '.$bpc.' /Columns '.$w.'>>';
  1408. //Scan chunks looking for palette, transparency and image data
  1409. $pal='';
  1410. $trns='';
  1411. $data='';
  1412. do
  1413. {
  1414. $n=$this->_freadint($f);
  1415. $type=fread($f,4);
  1416. if($type=='PLTE')
  1417. {
  1418. //Read palette
  1419. $pal=fread($f,$n);
  1420. fread($f,4);
  1421. }
  1422. elseif($type=='tRNS')
  1423. {
  1424. //Read transparency info
  1425. $t=fread($f,$n);
  1426. if($ct==0)
  1427. $trns=array(ord(substr($t,1,1)));
  1428. elseif($ct==2)
  1429. $trns=array(ord(substr($t,1,1)),ord(substr($t,3,1)),ord(substr($t,5,1)));
  1430. else
  1431. {
  1432. $pos=strpos($t,chr(0));
  1433. if(is_int($pos))
  1434. $trns=array($pos);
  1435. }
  1436. fread($f,4);
  1437. }
  1438. elseif($type=='IDAT')
  1439. {
  1440. //Read image data block
  1441. $data.=fread($f,$n);
  1442. fread($f,4);
  1443. }
  1444. elseif($type=='IEND')
  1445. break;
  1446. else
  1447. fread($f,$n+4);
  1448. }
  1449. while($n);
  1450. if($colspace=='Indexed' and empty($pal))
  1451. $this->Error('Missing palette in '.$file);
  1452. fclose($f);
  1453. return array('w'=>$w,'h'=>$h,'cs'=>$colspace,'bpc'=>$bpc,'f'=>'FlateDecode','parms'=>$parms,'pal'=>$pal,'trns'=>$trns,'data'=>$data);
  1454. }
  1455. function _freadint($f)
  1456. {
  1457. //Read a 4-byte integer from file
  1458. $i=ord(fread($f,1))<<24;
  1459. $i+=ord(fread($f,1))<<16;
  1460. $i+=ord(fread($f,1))<<8;
  1461. $i+=ord(fread($f,1));
  1462. return $i;
  1463. }
  1464. function _textstring($s)
  1465. {
  1466. //Format a text string
  1467. return '('.$this->_escape($s).')';
  1468. }
  1469. function _escape($s)
  1470. {
  1471. //Add  before , ( and )
  1472. return str_replace(')','\)',str_replace('(','\(',str_replace('\','\\',$s)));
  1473. }
  1474. function _putstream($s)
  1475. {
  1476. $this->_out('stream');
  1477. $this->_out($s);
  1478. $this->_out('endstream');
  1479. }
  1480. function _out($s)
  1481. {
  1482. //Add a line to the document
  1483. if($this->state==2)
  1484. $this->pages[$this->page].=$s."n";
  1485. else
  1486. $this->buffer.=$s."n";
  1487. }
  1488. //End of class
  1489. }
  1490. //Handle silly IE contype request
  1491. if(isset($HTTP_ENV_VARS['HTTP_USER_AGENT']) and $HTTP_ENV_VARS['HTTP_USER_AGENT']=='contype')
  1492. {
  1493. Header('Content-Type: application/pdf');
  1494. exit;
  1495. }
  1496. ?>