PageRenderTime 92ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/fpdf/fpdf.php

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