2 /*******************************************************************************
\r
6 * Author: Olivier PLATHEY *
\r
7 * License: Freeware *
\r
9 * You may use, modify and redistribute this software as you wish. *
\r
10 *******************************************************************************/
\r
12 if(!class_exists('FPDF'))
\r
14 define('FPDF_VERSION','1.53');
\r
18 //Private properties
\r
19 var $page; //current page number
\r
20 var $n; //current object number
\r
21 var $offsets; //array of object offsets
\r
22 var $buffer; //buffer holding in-memory PDF
\r
23 var $pages; //array containing pages
\r
24 var $state; //current document state
\r
25 var $compress; //compression flag
\r
26 var $DefOrientation; //default orientation
\r
27 var $CurOrientation; //current orientation
\r
28 var $OrientationChanges; //array indicating orientation changes
\r
29 var $k; //scale factor (number of points in user unit)
\r
30 var $fwPt,$fhPt; //dimensions of page format in points
\r
31 var $fw,$fh; //dimensions of page format in user unit
\r
32 var $wPt,$hPt; //current dimensions of page in points
\r
33 var $w,$h; //current dimensions of page in user unit
\r
34 var $lMargin; //left margin
\r
35 var $tMargin; //top margin
\r
36 var $rMargin; //right margin
\r
37 var $bMargin; //page break margin
\r
38 var $cMargin; //cell margin
\r
39 var $x,$y; //current position in user unit for cell positioning
\r
40 var $lasth; //height of last cell printed
\r
41 var $LineWidth; //line width in user unit
\r
42 var $CoreFonts; //array of standard font names
\r
43 var $fonts; //array of used fonts
\r
44 var $FontFiles; //array of font files
\r
45 var $diffs; //array of encoding differences
\r
46 var $images; //array of used images
\r
47 var $PageLinks; //array of links in pages
\r
48 var $links; //array of internal links
\r
49 var $FontFamily; //current font family
\r
50 var $FontStyle; //current font style
\r
51 var $underline; //underlining flag
\r
52 var $CurrentFont; //current font info
\r
53 var $FontSizePt; //current font size in points
\r
54 var $FontSize; //current font size in user unit
\r
55 var $DrawColor; //commands for drawing color
\r
56 var $FillColor; //commands for filling color
\r
57 var $TextColor; //commands for text color
\r
58 var $ColorFlag; //indicates whether fill and text colors are different
\r
59 var $ws; //word spacing
\r
60 var $AutoPageBreak; //automatic page breaking
\r
61 var $PageBreakTrigger; //threshold used to trigger page breaks
\r
62 var $InFooter; //flag set when processing footer
\r
63 var $ZoomMode; //zoom display mode
\r
64 var $LayoutMode; //layout display mode
\r
66 var $subject; //subject
\r
67 var $author; //author
\r
68 var $keywords; //keywords
\r
69 var $creator; //creator
\r
70 var $AliasNbPages; //alias for total number of pages
\r
71 var $PDFVersion; //PDF version number
\r
73 /*******************************************************************************
\r
77 *******************************************************************************/
\r
78 function FPDF($orientation='P',$unit='mm',$format='A4')
\r
82 //Initialization of properties
\r
86 $this->pages=array();
\r
87 $this->OrientationChanges=array();
\r
89 $this->fonts=array();
\r
90 $this->FontFiles=array();
\r
91 $this->diffs=array();
\r
92 $this->images=array();
\r
93 $this->links=array();
\r
94 $this->InFooter=false;
\r
96 $this->FontFamily='';
\r
97 $this->FontStyle='';
\r
98 $this->FontSizePt=12;
\r
99 $this->underline=false;
\r
100 $this->DrawColor='0 G';
\r
101 $this->FillColor='0 g';
\r
102 $this->TextColor='0 g';
\r
103 $this->ColorFlag=false;
\r
106 $this->CoreFonts=array('courier'=>'Courier','courierB'=>'Courier-Bold','courierI'=>'Courier-Oblique','courierBI'=>'Courier-BoldOblique',
\r
107 'helvetica'=>'Helvetica','helveticaB'=>'Helvetica-Bold','helveticaI'=>'Helvetica-Oblique','helveticaBI'=>'Helvetica-BoldOblique',
\r
108 'times'=>'Times-Roman','timesB'=>'Times-Bold','timesI'=>'Times-Italic','timesBI'=>'Times-BoldItalic',
\r
109 'symbol'=>'Symbol','zapfdingbats'=>'ZapfDingbats');
\r
113 elseif($unit=='mm')
\r
115 elseif($unit=='cm')
\r
117 elseif($unit=='in')
\r
120 $this->Error('Incorrect unit: '.$unit);
\r
122 if(is_string($format))
\r
124 $format=strtolower($format);
\r
126 $format=array(841.89,1190.55);
\r
127 elseif($format=='a4')
\r
128 $format=array(595.28,841.89);
\r
129 elseif($format=='a5')
\r
130 $format=array(420.94,595.28);
\r
131 elseif($format=='letter')
\r
132 $format=array(612,792);
\r
133 elseif($format=='legal')
\r
134 $format=array(612,1008);
\r
136 $this->Error('Unknown page format: '.$format);
\r
137 $this->fwPt=$format[0];
\r
138 $this->fhPt=$format[1];
\r
142 $this->fwPt=$format[0]*$this->k;
\r
143 $this->fhPt=$format[1]*$this->k;
\r
145 $this->fw=$this->fwPt/$this->k;
\r
146 $this->fh=$this->fhPt/$this->k;
\r
148 $orientation=strtolower($orientation);
\r
149 if($orientation=='p' || $orientation=='portrait')
\r
151 $this->DefOrientation='P';
\r
152 $this->wPt=$this->fwPt;
\r
153 $this->hPt=$this->fhPt;
\r
155 elseif($orientation=='l' || $orientation=='landscape')
\r
157 $this->DefOrientation='L';
\r
158 $this->wPt=$this->fhPt;
\r
159 $this->hPt=$this->fwPt;
\r
162 $this->Error('Incorrect orientation: '.$orientation);
\r
163 $this->CurOrientation=$this->DefOrientation;
\r
164 $this->w=$this->wPt/$this->k;
\r
165 $this->h=$this->hPt/$this->k;
\r
166 //Page margins (1 cm)
\r
167 $margin=28.35/$this->k;
\r
168 $this->SetMargins($margin,$margin);
\r
169 //Interior cell margin (1 mm)
\r
170 $this->cMargin=$margin/10;
\r
171 //Line width (0.2 mm)
\r
172 $this->LineWidth=.567/$this->k;
\r
173 //Automatic page break
\r
174 $this->SetAutoPageBreak(true,2*$margin);
\r
175 //Full width display mode
\r
176 $this->SetDisplayMode('fullwidth');
\r
177 //Enable compression
\r
178 $this->SetCompression(true);
\r
179 //Set default PDF version number
\r
180 $this->PDFVersion='1.3';
\r
183 function SetMargins($left,$top,$right=-1)
\r
185 //Set left, top and right margins
\r
186 $this->lMargin=$left;
\r
187 $this->tMargin=$top;
\r
190 $this->rMargin=$right;
\r
193 function SetLeftMargin($margin)
\r
196 $this->lMargin=$margin;
\r
197 if($this->page>0 && $this->x<$margin)
\r
201 function SetTopMargin($margin)
\r
204 $this->tMargin=$margin;
\r
207 function SetRightMargin($margin)
\r
210 $this->rMargin=$margin;
\r
213 function SetAutoPageBreak($auto,$margin=0)
\r
215 //Set auto page break mode and triggering margin
\r
216 $this->AutoPageBreak=$auto;
\r
217 $this->bMargin=$margin;
\r
218 $this->PageBreakTrigger=$this->h-$margin;
\r
221 function SetDisplayMode($zoom,$layout='continuous')
\r
223 //Set display mode in viewer
\r
224 if($zoom=='fullpage' || $zoom=='fullwidth' || $zoom=='real' || $zoom=='default' || !is_string($zoom))
\r
225 $this->ZoomMode=$zoom;
\r
227 $this->Error('Incorrect zoom display mode: '.$zoom);
\r
228 if($layout=='single' || $layout=='continuous' || $layout=='two' || $layout=='default')
\r
229 $this->LayoutMode=$layout;
\r
231 $this->Error('Incorrect layout display mode: '.$layout);
\r
234 function SetCompression($compress)
\r
236 //Set page compression
\r
237 if(function_exists('gzcompress'))
\r
238 $this->compress=$compress;
\r
240 $this->compress=false;
\r
243 function SetTitle($title)
\r
245 //Title of document
\r
246 $this->title=$title;
\r
249 function SetSubject($subject)
\r
251 //Subject of document
\r
252 $this->subject=$subject;
\r
255 function SetAuthor($author)
\r
257 //Author of document
\r
258 $this->author=$author;
\r
261 function SetKeywords($keywords)
\r
263 //Keywords of document
\r
264 $this->keywords=$keywords;
\r
267 function SetCreator($creator)
\r
269 //Creator of document
\r
270 $this->creator=$creator;
\r
273 function AliasNbPages($alias='{nb}')
\r
275 //Define an alias for total number of pages
\r
276 $this->AliasNbPages=$alias;
\r
279 function Error($msg)
\r
282 die('<B>FPDF error: </B>'.$msg);
\r
293 //Terminate document
\r
294 if($this->state==3)
\r
299 $this->InFooter=true;
\r
301 $this->InFooter=false;
\r
308 function AddPage($orientation='')
\r
311 if($this->state==0)
\r
313 $family=$this->FontFamily;
\r
314 $style=$this->FontStyle.($this->underline ? 'U' : '');
\r
315 $size=$this->FontSizePt;
\r
316 $lw=$this->LineWidth;
\r
317 $dc=$this->DrawColor;
\r
318 $fc=$this->FillColor;
\r
319 $tc=$this->TextColor;
\r
320 $cf=$this->ColorFlag;
\r
324 $this->InFooter=true;
\r
326 $this->InFooter=false;
\r
331 $this->_beginpage($orientation);
\r
332 //Set line cap style to square
\r
333 $this->_out('2 J');
\r
335 $this->LineWidth=$lw;
\r
336 $this->_out(sprintf('%.2f w',$lw*$this->k));
\r
339 $this->SetFont($family,$style,$size);
\r
341 $this->DrawColor=$dc;
\r
344 $this->FillColor=$fc;
\r
347 $this->TextColor=$tc;
\r
348 $this->ColorFlag=$cf;
\r
351 //Restore line width
\r
352 if($this->LineWidth!=$lw)
\r
354 $this->LineWidth=$lw;
\r
355 $this->_out(sprintf('%.2f w',$lw*$this->k));
\r
359 $this->SetFont($family,$style,$size);
\r
361 if($this->DrawColor!=$dc)
\r
363 $this->DrawColor=$dc;
\r
366 if($this->FillColor!=$fc)
\r
368 $this->FillColor=$fc;
\r
371 $this->TextColor=$tc;
\r
372 $this->ColorFlag=$cf;
\r
377 //To be implemented in your own inherited class
\r
382 //To be implemented in your own inherited class
\r
387 //Get current page number
\r
388 return $this->page;
\r
391 function SetDrawColor($r,$g=-1,$b=-1)
\r
393 //Set color for all stroking operations
\r
394 if(($r==0 && $g==0 && $b==0) || $g==-1)
\r
395 $this->DrawColor=sprintf('%.3f G',$r/255);
\r
397 $this->DrawColor=sprintf('%.3f %.3f %.3f RG',$r/255,$g/255,$b/255);
\r
399 $this->_out($this->DrawColor);
\r
402 function SetFillColor($r,$g=-1,$b=-1)
\r
404 //Set color for all filling operations
\r
405 if(($r==0 && $g==0 && $b==0) || $g==-1)
\r
406 $this->FillColor=sprintf('%.3f g',$r/255);
\r
408 $this->FillColor=sprintf('%.3f %.3f %.3f rg',$r/255,$g/255,$b/255);
\r
409 $this->ColorFlag=($this->FillColor!=$this->TextColor);
\r
411 $this->_out($this->FillColor);
\r
414 function SetTextColor($r,$g=-1,$b=-1)
\r
416 //Set color for text
\r
417 if(($r==0 && $g==0 && $b==0) || $g==-1)
\r
418 $this->TextColor=sprintf('%.3f g',$r/255);
\r
420 $this->TextColor=sprintf('%.3f %.3f %.3f rg',$r/255,$g/255,$b/255);
\r
421 $this->ColorFlag=($this->FillColor!=$this->TextColor);
\r
424 function GetStringWidth($s)
\r
426 //Get width of a string in the current font
\r
428 $cw=&$this->CurrentFont['cw'];
\r
431 for($i=0;$i<$l;$i++)
\r
433 return $w*$this->FontSize/1000;
\r
436 function SetLineWidth($width)
\r
439 $this->LineWidth=$width;
\r
441 $this->_out(sprintf('%.2f w',$width*$this->k));
\r
444 function Line($x1,$y1,$x2,$y2)
\r
447 $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));
\r
450 function Rect($x,$y,$w,$h,$style='')
\r
455 elseif($style=='FD' || $style=='DF')
\r
459 $this->_out(sprintf('%.2f %.2f %.2f %.2f re %s',$x*$this->k,($this->h-$y)*$this->k,$w*$this->k,-$h*$this->k,$op));
\r
462 function AddFont($family,$style='',$file='')
\r
464 //Add a TrueType or Type1 font
\r
465 $family=strtolower($family);
\r
467 $file=str_replace(' ','',$family).strtolower($style).'.php';
\r
468 if($family=='arial')
\r
469 $family='helvetica';
\r
470 $style=strtoupper($style);
\r
473 $fontkey=$family.$style;
\r
474 if(isset($this->fonts[$fontkey]))
\r
475 $this->Error('Font already added: '.$family.' '.$style);
\r
476 include($this->_getfontpath().$file);
\r
478 $this->Error('Could not include font definition file');
\r
479 $i=count($this->fonts)+1;
\r
480 $this->fonts[$fontkey]=array('i'=>$i,'type'=>$type,'name'=>$name,'desc'=>$desc,'up'=>$up,'ut'=>$ut,'cw'=>$cw,'enc'=>$enc,'file'=>$file);
\r
483 //Search existing encodings
\r
485 $nb=count($this->diffs);
\r
486 for($i=1;$i<=$nb;$i++)
\r
488 if($this->diffs[$i]==$diff)
\r
497 $this->diffs[$d]=$diff;
\r
499 $this->fonts[$fontkey]['diff']=$d;
\r
503 if($type=='TrueType')
\r
504 $this->FontFiles[$file]=array('length1'=>$originalsize);
\r
506 $this->FontFiles[$file]=array('length1'=>$size1,'length2'=>$size2);
\r
510 function SetFont($family,$style='',$size=0)
\r
512 //Select a font; size given in points
\r
513 global $fpdf_charwidths;
\r
515 $family=strtolower($family);
\r
517 $family=$this->FontFamily;
\r
518 if($family=='arial')
\r
519 $family='helvetica';
\r
520 elseif($family=='symbol' || $family=='zapfdingbats')
\r
522 $style=strtoupper($style);
\r
523 if(strpos($style,'U')!==false)
\r
525 $this->underline=true;
\r
526 $style=str_replace('U','',$style);
\r
529 $this->underline=false;
\r
533 $size=$this->FontSizePt;
\r
534 //Test if font is already selected
\r
535 if($this->FontFamily==$family && $this->FontStyle==$style && $this->FontSizePt==$size)
\r
537 //Test if used for the first time
\r
538 $fontkey=$family.$style;
\r
539 if(!isset($this->fonts[$fontkey]))
\r
541 //Check if one of the standard fonts
\r
542 if(isset($this->CoreFonts[$fontkey]))
\r
544 if(!isset($fpdf_charwidths[$fontkey]))
\r
548 if($family=='times' || $family=='helvetica')
\r
549 $file.=strtolower($style);
\r
550 include($this->_getfontpath().$file.'.php');
\r
551 if(!isset($fpdf_charwidths[$fontkey]))
\r
552 $this->Error('Could not include font metric file');
\r
554 $i=count($this->fonts)+1;
\r
555 $this->fonts[$fontkey]=array('i'=>$i,'type'=>'core','name'=>$this->CoreFonts[$fontkey],'up'=>-100,'ut'=>50,'cw'=>$fpdf_charwidths[$fontkey]);
\r
558 $this->Error('Undefined font: '.$family.' '.$style);
\r
561 $this->FontFamily=$family;
\r
562 $this->FontStyle=$style;
\r
563 $this->FontSizePt=$size;
\r
564 $this->FontSize=$size/$this->k;
\r
565 $this->CurrentFont=&$this->fonts[$fontkey];
\r
567 $this->_out(sprintf('BT /F%d %.2f Tf ET',$this->CurrentFont['i'],$this->FontSizePt));
\r
570 function SetFontSize($size)
\r
572 //Set font size in points
\r
573 if($this->FontSizePt==$size)
\r
575 $this->FontSizePt=$size;
\r
576 $this->FontSize=$size/$this->k;
\r
578 $this->_out(sprintf('BT /F%d %.2f Tf ET',$this->CurrentFont['i'],$this->FontSizePt));
\r
583 //Create a new internal link
\r
584 $n=count($this->links)+1;
\r
585 $this->links[$n]=array(0,0);
\r
589 function SetLink($link,$y=0,$page=-1)
\r
591 //Set destination of internal link
\r
596 $this->links[$link]=array($page,$y);
\r
599 function Link($x,$y,$w,$h,$link)
\r
601 //Put a link on the page
\r
602 $this->PageLinks[$this->page][]=array($x*$this->k,$this->hPt-$y*$this->k,$w*$this->k,$h*$this->k,$link);
\r
605 function Text($x,$y,$txt)
\r
608 $s=sprintf('BT %.2f %.2f Td (%s) Tj ET',$x*$this->k,($this->h-$y)*$this->k,$this->_escape($txt));
\r
609 if($this->underline && $txt!='')
\r
610 $s.=' '.$this->_dounderline($x,$y,$txt);
\r
611 if($this->ColorFlag)
\r
612 $s='q '.$this->TextColor.' '.$s.' Q';
\r
616 function AcceptPageBreak()
\r
618 //Accept automatic page break or not
\r
619 return $this->AutoPageBreak;
\r
622 function Cell($w,$h=0,$txt='',$border=0,$ln=0,$align='',$fill=0,$link='')
\r
626 if($this->y+$h>$this->PageBreakTrigger && !$this->InFooter && $this->AcceptPageBreak())
\r
628 //Automatic page break
\r
634 $this->_out('0 Tw');
\r
636 $this->AddPage($this->CurOrientation);
\r
641 $this->_out(sprintf('%.3f Tw',$ws*$k));
\r
645 $w=$this->w-$this->rMargin-$this->x;
\r
647 if($fill==1 || $border==1)
\r
650 $op=($border==1) ? 'B' : 'f';
\r
653 $s=sprintf('%.2f %.2f %.2f %.2f re %s ',$this->x*$k,($this->h-$this->y)*$k,$w*$k,-$h*$k,$op);
\r
655 if(is_string($border))
\r
659 if(strpos($border,'L')!==false)
\r
660 $s.=sprintf('%.2f %.2f m %.2f %.2f l S ',$x*$k,($this->h-$y)*$k,$x*$k,($this->h-($y+$h))*$k);
\r
661 if(strpos($border,'T')!==false)
\r
662 $s.=sprintf('%.2f %.2f m %.2f %.2f l S ',$x*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-$y)*$k);
\r
663 if(strpos($border,'R')!==false)
\r
664 $s.=sprintf('%.2f %.2f m %.2f %.2f l S ',($x+$w)*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
\r
665 if(strpos($border,'B')!==false)
\r
666 $s.=sprintf('%.2f %.2f m %.2f %.2f l S ',$x*$k,($this->h-($y+$h))*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
\r
671 $dx=$w-$this->cMargin-$this->GetStringWidth($txt);
\r
672 elseif($align=='C')
\r
673 $dx=($w-$this->GetStringWidth($txt))/2;
\r
675 $dx=$this->cMargin;
\r
676 if($this->ColorFlag)
\r
677 $s.='q '.$this->TextColor.' ';
\r
678 $txt2=str_replace(')','\\)',str_replace('(','\\(',str_replace('\\','\\\\',$txt)));
\r
679 $s.=sprintf('BT %.2f %.2f Td (%s) Tj ET',($this->x+$dx)*$k,($this->h-($this->y+.5*$h+.3*$this->FontSize))*$k,$txt2);
\r
680 if($this->underline)
\r
681 $s.=' '.$this->_dounderline($this->x+$dx,$this->y+.5*$h+.3*$this->FontSize,$txt);
\r
682 if($this->ColorFlag)
\r
685 $this->Link($this->x+$dx,$this->y+.5*$h-.5*$this->FontSize,$this->GetStringWidth($txt),$this->FontSize,$link);
\r
695 $this->x=$this->lMargin;
\r
701 function MultiCell($w,$h,$txt,$border=0,$align='J',$fill=0)
\r
703 //Output text with automatic or explicit line breaks
\r
704 $cw=&$this->CurrentFont['cw'];
\r
706 $w=$this->w-$this->rMargin-$this->x;
\r
707 $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
\r
708 $s=str_replace("\r",'',$txt);
\r
710 if($nb>0 && $s[$nb-1]=="\n")
\r
724 if(strpos($border,'L')!==false)
\r
726 if(strpos($border,'R')!==false)
\r
728 $b=(strpos($border,'T')!==false) ? $b2.'T' : $b2;
\r
739 //Get next character
\r
743 //Explicit line break
\r
747 $this->_out('0 Tw');
\r
749 $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
\r
756 if($border && $nl==2)
\r
769 //Automatic line break
\r
777 $this->_out('0 Tw');
\r
779 $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
\r
785 $this->ws=($ns>1) ? ($wmax-$ls)/1000*$this->FontSize/($ns-1) : 0;
\r
786 $this->_out(sprintf('%.3f Tw',$this->ws*$this->k));
\r
788 $this->Cell($w,$h,substr($s,$j,$sep-$j),$b,2,$align,$fill);
\r
796 if($border && $nl==2)
\r
806 $this->_out('0 Tw');
\r
808 if($border && strpos($border,'B')!==false)
\r
810 $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
\r
811 $this->x=$this->lMargin;
\r
814 function Write($h,$txt,$link='')
\r
816 //Output text in flowing mode
\r
817 $cw=&$this->CurrentFont['cw'];
\r
818 $w=$this->w-$this->rMargin-$this->x;
\r
819 $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
\r
820 $s=str_replace("\r",'',$txt);
\r
829 //Get next character
\r
833 //Explicit line break
\r
834 $this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link);
\r
841 $this->x=$this->lMargin;
\r
842 $w=$this->w-$this->rMargin-$this->x;
\r
843 $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
\r
853 //Automatic line break
\r
856 if($this->x>$this->lMargin)
\r
858 //Move to next line
\r
859 $this->x=$this->lMargin;
\r
861 $w=$this->w-$this->rMargin-$this->x;
\r
862 $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
\r
869 $this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link);
\r
873 $this->Cell($w,$h,substr($s,$j,$sep-$j),0,2,'',0,$link);
\r
881 $this->x=$this->lMargin;
\r
882 $w=$this->w-$this->rMargin-$this->x;
\r
883 $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
\r
892 $this->Cell($l/1000*$this->FontSize,$h,substr($s,$j),0,0,'',0,$link);
\r
895 function Image($file,$x,$y,$w=0,$h=0,$type='',$link='')
\r
897 //Put an image on the page
\r
898 if(!isset($this->images[$file]))
\r
900 //First use of image, get info
\r
903 $pos=strrpos($file,'.');
\r
905 $this->Error('Image file has no extension and no type was specified: '.$file);
\r
906 $type=substr($file,$pos+1);
\r
908 $type=strtolower($type);
\r
909 $mqr=get_magic_quotes_runtime();
\r
910 set_magic_quotes_runtime(0);
\r
911 if($type=='jpg' || $type=='jpeg')
\r
912 $info=$this->_parsejpg($file);
\r
913 elseif($type=='png')
\r
914 $info=$this->_parsepng($file);
\r
917 //Allow for additional formats
\r
918 $mtd='_parse'.$type;
\r
919 if(!method_exists($this,$mtd))
\r
920 $this->Error('Unsupported image type: '.$type);
\r
921 $info=$this->$mtd($file);
\r
923 set_magic_quotes_runtime($mqr);
\r
924 $info['i']=count($this->images)+1;
\r
925 $this->images[$file]=$info;
\r
928 $info=$this->images[$file];
\r
929 //Automatic width and height calculation if needed
\r
932 //Put image at 72 dpi
\r
933 $w=$info['w']/$this->k;
\r
934 $h=$info['h']/$this->k;
\r
937 $w=$h*$info['w']/$info['h'];
\r
939 $h=$w*$info['h']/$info['w'];
\r
940 $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']));
\r
942 $this->Link($x,$y,$w,$h,$link);
\r
947 //Line feed; default value is last cell height
\r
948 $this->x=$this->lMargin;
\r
950 $this->y+=$this->lasth;
\r
967 $this->x=$this->w+$x;
\r
978 //Set y position and reset x
\r
979 $this->x=$this->lMargin;
\r
983 $this->y=$this->h+$y;
\r
986 function SetXY($x,$y)
\r
988 //Set x and y positions
\r
993 function Output($name='',$dest='')
\r
995 //Output PDF to some destination
\r
996 //Finish document if necessary
\r
999 //Normalize parameters
\r
1000 if(is_bool($dest))
\r
1001 $dest=$dest ? 'D' : 'F';
\r
1002 $dest=strtoupper($dest);
\r
1016 //Send to standard output
\r
1017 if(ob_get_contents())
\r
1018 $this->Error('Some data has already been output, can\'t send PDF file');
\r
1019 if(php_sapi_name()!='cli')
\r
1021 //We send to a browser
\r
1022 header('Content-Type: application/pdf');
\r
1023 if(headers_sent())
\r
1024 $this->Error('Some data has already been output to browser, can\'t send PDF file');
\r
1025 header('Content-Length: '.strlen($this->buffer));
\r
1026 header('Content-disposition: inline; filename="'.$name.'"');
\r
1028 echo $this->buffer;
\r
1032 if(ob_get_contents())
\r
1033 $this->Error('Some data has already been output, can\'t send PDF file');
\r
1034 if(isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'],'MSIE'))
\r
1035 header('Content-Type: application/force-download');
\r
1037 header('Content-Type: application/octet-stream');
\r
1038 if(headers_sent())
\r
1039 $this->Error('Some data has already been output to browser, can\'t send PDF file');
\r
1040 header('Content-Length: '.strlen($this->buffer));
\r
1041 header('Content-disposition: attachment; filename="'.$name.'"');
\r
1042 echo $this->buffer;
\r
1045 //Save to local file
\r
1046 $f=fopen($name,'wb');
\r
1048 $this->Error('Unable to create output file: '.$name);
\r
1049 fwrite($f,$this->buffer,strlen($this->buffer));
\r
1053 //Return as a string
\r
1054 return $this->buffer;
\r
1056 $this->Error('Incorrect output destination: '.$dest);
\r
1061 /*******************************************************************************
\r
1063 * Protected methods *
\r
1065 *******************************************************************************/
\r
1066 function _dochecks()
\r
1068 //Check for locale-related bug
\r
1070 $this->Error('Don\'t alter the locale before including class file');
\r
1071 //Check for decimal separator
\r
1072 if(sprintf('%.1f',1.0)!='1.0')
\r
1073 setlocale(LC_NUMERIC,'C');
\r
1076 function _getfontpath()
\r
1078 if(!defined('FPDF_FONTPATH') && is_dir(dirname(__FILE__).'/font'))
\r
1079 define('FPDF_FONTPATH',dirname(__FILE__).'/font/');
\r
1080 return defined('FPDF_FONTPATH') ? FPDF_FONTPATH : '';
\r
1083 function _putpages()
\r
1086 if(!empty($this->AliasNbPages))
\r
1088 //Replace number of pages
\r
1089 for($n=1;$n<=$nb;$n++)
\r
1090 $this->pages[$n]=str_replace($this->AliasNbPages,$nb,$this->pages[$n]);
\r
1092 if($this->DefOrientation=='P')
\r
1102 $filter=($this->compress) ? '/Filter /FlateDecode ' : '';
\r
1103 for($n=1;$n<=$nb;$n++)
\r
1107 $this->_out('<</Type /Page');
\r
1108 $this->_out('/Parent 1 0 R');
\r
1109 if(isset($this->OrientationChanges[$n]))
\r
1110 $this->_out(sprintf('/MediaBox [0 0 %.2f %.2f]',$hPt,$wPt));
\r
1111 $this->_out('/Resources 2 0 R');
\r
1112 if(isset($this->PageLinks[$n]))
\r
1115 $annots='/Annots [';
\r
1116 foreach($this->PageLinks[$n] as $pl)
\r
1118 $rect=sprintf('%.2f %.2f %.2f %.2f',$pl[0],$pl[1],$pl[0]+$pl[2],$pl[1]-$pl[3]);
\r
1119 $annots.='<</Type /Annot /Subtype /Link /Rect ['.$rect.'] /Border [0 0 0] ';
\r
1120 if(is_string($pl[4]))
\r
1121 $annots.='/A <</S /URI /URI '.$this->_textstring($pl[4]).'>>>>';
\r
1124 $l=$this->links[$pl[4]];
\r
1125 $h=isset($this->OrientationChanges[$l[0]]) ? $wPt : $hPt;
\r
1126 $annots.=sprintf('/Dest [%d 0 R /XYZ 0 %.2f null]>>',1+2*$l[0],$h-$l[1]*$this->k);
\r
1129 $this->_out($annots.']');
\r
1131 $this->_out('/Contents '.($this->n+1).' 0 R>>');
\r
1132 $this->_out('endobj');
\r
1134 $p=($this->compress) ? gzcompress($this->pages[$n]) : $this->pages[$n];
\r
1136 $this->_out('<<'.$filter.'/Length '.strlen($p).'>>');
\r
1137 $this->_putstream($p);
\r
1138 $this->_out('endobj');
\r
1141 $this->offsets[1]=strlen($this->buffer);
\r
1142 $this->_out('1 0 obj');
\r
1143 $this->_out('<</Type /Pages');
\r
1145 for($i=0;$i<$nb;$i++)
\r
1146 $kids.=(3+2*$i).' 0 R ';
\r
1147 $this->_out($kids.']');
\r
1148 $this->_out('/Count '.$nb);
\r
1149 $this->_out(sprintf('/MediaBox [0 0 %.2f %.2f]',$wPt,$hPt));
\r
1150 $this->_out('>>');
\r
1151 $this->_out('endobj');
\r
1154 function _putfonts()
\r
1157 foreach($this->diffs as $diff)
\r
1161 $this->_out('<</Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences ['.$diff.']>>');
\r
1162 $this->_out('endobj');
\r
1164 $mqr=get_magic_quotes_runtime();
\r
1165 set_magic_quotes_runtime(0);
\r
1166 foreach($this->FontFiles as $file=>$info)
\r
1168 //Font file embedding
\r
1170 $this->FontFiles[$file]['n']=$this->n;
\r
1172 $f=fopen($this->_getfontpath().$file,'rb',1);
\r
1174 $this->Error('Font file not found');
\r
1176 $font.=fread($f,8192);
\r
1178 $compressed=(substr($file,-2)=='.z');
\r
1179 if(!$compressed && isset($info['length2']))
\r
1181 $header=(ord($font{0})==128);
\r
1184 //Strip first binary header
\r
1185 $font=substr($font,6);
\r
1187 if($header && ord($font{$info['length1']})==128)
\r
1189 //Strip second binary header
\r
1190 $font=substr($font,0,$info['length1']).substr($font,$info['length1']+6);
\r
1193 $this->_out('<</Length '.strlen($font));
\r
1195 $this->_out('/Filter /FlateDecode');
\r
1196 $this->_out('/Length1 '.$info['length1']);
\r
1197 if(isset($info['length2']))
\r
1198 $this->_out('/Length2 '.$info['length2'].' /Length3 0');
\r
1199 $this->_out('>>');
\r
1200 $this->_putstream($font);
\r
1201 $this->_out('endobj');
\r
1203 set_magic_quotes_runtime($mqr);
\r
1204 foreach($this->fonts as $k=>$font)
\r
1207 $this->fonts[$k]['n']=$this->n+1;
\r
1208 $type=$font['type'];
\r
1209 $name=$font['name'];
\r
1214 $this->_out('<</Type /Font');
\r
1215 $this->_out('/BaseFont /'.$name);
\r
1216 $this->_out('/Subtype /Type1');
\r
1217 if($name!='Symbol' && $name!='ZapfDingbats')
\r
1218 $this->_out('/Encoding /WinAnsiEncoding');
\r
1219 $this->_out('>>');
\r
1220 $this->_out('endobj');
\r
1222 elseif($type=='Type1' || $type=='TrueType')
\r
1224 //Additional Type1 or TrueType font
\r
1226 $this->_out('<</Type /Font');
\r
1227 $this->_out('/BaseFont /'.$name);
\r
1228 $this->_out('/Subtype /'.$type);
\r
1229 $this->_out('/FirstChar 32 /LastChar 255');
\r
1230 $this->_out('/Widths '.($this->n+1).' 0 R');
\r
1231 $this->_out('/FontDescriptor '.($this->n+2).' 0 R');
\r
1234 if(isset($font['diff']))
\r
1235 $this->_out('/Encoding '.($nf+$font['diff']).' 0 R');
\r
1237 $this->_out('/Encoding /WinAnsiEncoding');
\r
1239 $this->_out('>>');
\r
1240 $this->_out('endobj');
\r
1245 for($i=32;$i<=255;$i++)
\r
1246 $s.=$cw[chr($i)].' ';
\r
1247 $this->_out($s.']');
\r
1248 $this->_out('endobj');
\r
1251 $s='<</Type /FontDescriptor /FontName /'.$name;
\r
1252 foreach($font['desc'] as $k=>$v)
\r
1253 $s.=' /'.$k.' '.$v;
\r
1254 $file=$font['file'];
\r
1256 $s.=' /FontFile'.($type=='Type1' ? '' : '2').' '.$this->FontFiles[$file]['n'].' 0 R';
\r
1257 $this->_out($s.'>>');
\r
1258 $this->_out('endobj');
\r
1262 //Allow for additional types
\r
1263 $mtd='_put'.strtolower($type);
\r
1264 if(!method_exists($this,$mtd))
\r
1265 $this->Error('Unsupported font type: '.$type);
\r
1266 $this->$mtd($font);
\r
1271 function _putimages()
\r
1273 $filter=($this->compress) ? '/Filter /FlateDecode ' : '';
\r
1274 reset($this->images);
\r
1275 while(list($file,$info)=each($this->images))
\r
1278 $this->images[$file]['n']=$this->n;
\r
1279 $this->_out('<</Type /XObject');
\r
1280 $this->_out('/Subtype /Image');
\r
1281 $this->_out('/Width '.$info['w']);
\r
1282 $this->_out('/Height '.$info['h']);
\r
1283 if($info['cs']=='Indexed')
\r
1284 $this->_out('/ColorSpace [/Indexed /DeviceRGB '.(strlen($info['pal'])/3-1).' '.($this->n+1).' 0 R]');
\r
1287 $this->_out('/ColorSpace /'.$info['cs']);
\r
1288 if($info['cs']=='DeviceCMYK')
\r
1289 $this->_out('/Decode [1 0 1 0 1 0 1 0]');
\r
1291 $this->_out('/BitsPerComponent '.$info['bpc']);
\r
1292 if(isset($info['f']))
\r
1293 $this->_out('/Filter /'.$info['f']);
\r
1294 if(isset($info['parms']))
\r
1295 $this->_out($info['parms']);
\r
1296 if(isset($info['trns']) && is_array($info['trns']))
\r
1299 for($i=0;$i<count($info['trns']);$i++)
\r
1300 $trns.=$info['trns'][$i].' '.$info['trns'][$i].' ';
\r
1301 $this->_out('/Mask ['.$trns.']');
\r
1303 $this->_out('/Length '.strlen($info['data']).'>>');
\r
1304 $this->_putstream($info['data']);
\r
1305 unset($this->images[$file]['data']);
\r
1306 $this->_out('endobj');
\r
1308 if($info['cs']=='Indexed')
\r
1311 $pal=($this->compress) ? gzcompress($info['pal']) : $info['pal'];
\r
1312 $this->_out('<<'.$filter.'/Length '.strlen($pal).'>>');
\r
1313 $this->_putstream($pal);
\r
1314 $this->_out('endobj');
\r
1319 function _putxobjectdict()
\r
1321 foreach($this->images as $image)
\r
1322 $this->_out('/I'.$image['i'].' '.$image['n'].' 0 R');
\r
1325 function _putresourcedict()
\r
1327 $this->_out('/ProcSet [/PDF /Text /ImageB /ImageC /ImageI]');
\r
1328 $this->_out('/Font <<');
\r
1329 foreach($this->fonts as $font)
\r
1330 $this->_out('/F'.$font['i'].' '.$font['n'].' 0 R');
\r
1331 $this->_out('>>');
\r
1332 $this->_out('/XObject <<');
\r
1333 $this->_putxobjectdict();
\r
1334 $this->_out('>>');
\r
1337 function _putresources()
\r
1339 $this->_putfonts();
\r
1340 $this->_putimages();
\r
1341 //Resource dictionary
\r
1342 $this->offsets[2]=strlen($this->buffer);
\r
1343 $this->_out('2 0 obj');
\r
1344 $this->_out('<<');
\r
1345 $this->_putresourcedict();
\r
1346 $this->_out('>>');
\r
1347 $this->_out('endobj');
\r
1350 function _putinfo()
\r
1352 $this->_out('/Producer '.$this->_textstring('FPDF '.FPDF_VERSION));
\r
1353 if(!empty($this->title))
\r
1354 $this->_out('/Title '.$this->_textstring($this->title));
\r
1355 if(!empty($this->subject))
\r
1356 $this->_out('/Subject '.$this->_textstring($this->subject));
\r
1357 if(!empty($this->author))
\r
1358 $this->_out('/Author '.$this->_textstring($this->author));
\r
1359 if(!empty($this->keywords))
\r
1360 $this->_out('/Keywords '.$this->_textstring($this->keywords));
\r
1361 if(!empty($this->creator))
\r
1362 $this->_out('/Creator '.$this->_textstring($this->creator));
\r
1363 $this->_out('/CreationDate '.$this->_textstring('D:'.date('YmdHis')));
\r
1366 function _putcatalog()
\r
1368 $this->_out('/Type /Catalog');
\r
1369 $this->_out('/Pages 1 0 R');
\r
1370 if($this->ZoomMode=='fullpage')
\r
1371 $this->_out('/OpenAction [3 0 R /Fit]');
\r
1372 elseif($this->ZoomMode=='fullwidth')
\r
1373 $this->_out('/OpenAction [3 0 R /FitH null]');
\r
1374 elseif($this->ZoomMode=='real')
\r
1375 $this->_out('/OpenAction [3 0 R /XYZ null null 1]');
\r
1376 elseif(!is_string($this->ZoomMode))
\r
1377 $this->_out('/OpenAction [3 0 R /XYZ null null '.($this->ZoomMode/100).']');
\r
1378 if($this->LayoutMode=='single')
\r
1379 $this->_out('/PageLayout /SinglePage');
\r
1380 elseif($this->LayoutMode=='continuous')
\r
1381 $this->_out('/PageLayout /OneColumn');
\r
1382 elseif($this->LayoutMode=='two')
\r
1383 $this->_out('/PageLayout /TwoColumnLeft');
\r
1386 function _putheader()
\r
1388 $this->_out('%PDF-'.$this->PDFVersion);
\r
1391 function _puttrailer()
\r
1393 $this->_out('/Size '.($this->n+1));
\r
1394 $this->_out('/Root '.$this->n.' 0 R');
\r
1395 $this->_out('/Info '.($this->n-1).' 0 R');
\r
1398 function _enddoc()
\r
1400 $this->_putheader();
\r
1401 $this->_putpages();
\r
1402 $this->_putresources();
\r
1405 $this->_out('<<');
\r
1406 $this->_putinfo();
\r
1407 $this->_out('>>');
\r
1408 $this->_out('endobj');
\r
1411 $this->_out('<<');
\r
1412 $this->_putcatalog();
\r
1413 $this->_out('>>');
\r
1414 $this->_out('endobj');
\r
1416 $o=strlen($this->buffer);
\r
1417 $this->_out('xref');
\r
1418 $this->_out('0 '.($this->n+1));
\r
1419 $this->_out('0000000000 65535 f ');
\r
1420 for($i=1;$i<=$this->n;$i++)
\r
1421 $this->_out(sprintf('%010d 00000 n ',$this->offsets[$i]));
\r
1423 $this->_out('trailer');
\r
1424 $this->_out('<<');
\r
1425 $this->_puttrailer();
\r
1426 $this->_out('>>');
\r
1427 $this->_out('startxref');
\r
1429 $this->_out('%%EOF');
\r
1433 function _beginpage($orientation)
\r
1436 $this->pages[$this->page]='';
\r
1438 $this->x=$this->lMargin;
\r
1439 $this->y=$this->tMargin;
\r
1440 $this->FontFamily='';
\r
1441 //Page orientation
\r
1443 $orientation=$this->DefOrientation;
\r
1446 $orientation=strtoupper($orientation{0});
\r
1447 if($orientation!=$this->DefOrientation)
\r
1448 $this->OrientationChanges[$this->page]=true;
\r
1450 if($orientation!=$this->CurOrientation)
\r
1452 //Change orientation
\r
1453 if($orientation=='P')
\r
1455 $this->wPt=$this->fwPt;
\r
1456 $this->hPt=$this->fhPt;
\r
1457 $this->w=$this->fw;
\r
1458 $this->h=$this->fh;
\r
1462 $this->wPt=$this->fhPt;
\r
1463 $this->hPt=$this->fwPt;
\r
1464 $this->w=$this->fh;
\r
1465 $this->h=$this->fw;
\r
1467 $this->PageBreakTrigger=$this->h-$this->bMargin;
\r
1468 $this->CurOrientation=$orientation;
\r
1472 function _endpage()
\r
1474 //End of page contents
\r
1478 function _newobj()
\r
1480 //Begin a new object
\r
1482 $this->offsets[$this->n]=strlen($this->buffer);
\r
1483 $this->_out($this->n.' 0 obj');
\r
1486 function _dounderline($x,$y,$txt)
\r
1489 $up=$this->CurrentFont['up'];
\r
1490 $ut=$this->CurrentFont['ut'];
\r
1491 $w=$this->GetStringWidth($txt)+$this->ws*substr_count($txt,' ');
\r
1492 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);
\r
1495 function _parsejpg($file)
\r
1497 //Extract info from a JPEG file
\r
1498 $a=GetImageSize($file);
\r
1500 $this->Error('Missing or incorrect image file: '.$file);
\r
1502 $this->Error('Not a JPEG file: '.$file);
\r
1503 if(!isset($a['channels']) || $a['channels']==3)
\r
1504 $colspace='DeviceRGB';
\r
1505 elseif($a['channels']==4)
\r
1506 $colspace='DeviceCMYK';
\r
1508 $colspace='DeviceGray';
\r
1509 $bpc=isset($a['bits']) ? $a['bits'] : 8;
\r
1511 $f=fopen($file,'rb');
\r
1514 $data.=fread($f,4096);
\r
1516 return array('w'=>$a[0],'h'=>$a[1],'cs'=>$colspace,'bpc'=>$bpc,'f'=>'DCTDecode','data'=>$data);
\r
1519 function _parsepng($file)
\r
1521 //Extract info from a PNG file
\r
1522 $f=fopen($file,'rb');
\r
1524 $this->Error('Can\'t open image file: '.$file);
\r
1526 if(fread($f,8)!=chr(137).'PNG'.chr(13).chr(10).chr(26).chr(10))
\r
1527 $this->Error('Not a PNG file: '.$file);
\r
1528 //Read header chunk
\r
1530 if(fread($f,4)!='IHDR')
\r
1531 $this->Error('Incorrect PNG file: '.$file);
\r
1532 $w=$this->_freadint($f);
\r
1533 $h=$this->_freadint($f);
\r
1534 $bpc=ord(fread($f,1));
\r
1536 $this->Error('16-bit depth not supported: '.$file);
\r
1537 $ct=ord(fread($f,1));
\r
1539 $colspace='DeviceGray';
\r
1541 $colspace='DeviceRGB';
\r
1543 $colspace='Indexed';
\r
1545 $this->Error('Alpha channel not supported: '.$file);
\r
1546 if(ord(fread($f,1))!=0)
\r
1547 $this->Error('Unknown compression method: '.$file);
\r
1548 if(ord(fread($f,1))!=0)
\r
1549 $this->Error('Unknown filter method: '.$file);
\r
1550 if(ord(fread($f,1))!=0)
\r
1551 $this->Error('Interlacing not supported: '.$file);
\r
1553 $parms='/DecodeParms <</Predictor 15 /Colors '.($ct==2 ? 3 : 1).' /BitsPerComponent '.$bpc.' /Columns '.$w.'>>';
\r
1554 //Scan chunks looking for palette, transparency and image data
\r
1560 $n=$this->_freadint($f);
\r
1561 $type=fread($f,4);
\r
1565 $pal=fread($f,$n);
\r
1568 elseif($type=='tRNS')
\r
1570 //Read transparency info
\r
1573 $trns=array(ord(substr($t,1,1)));
\r
1575 $trns=array(ord(substr($t,1,1)),ord(substr($t,3,1)),ord(substr($t,5,1)));
\r
1578 $pos=strpos($t,chr(0));
\r
1580 $trns=array($pos);
\r
1584 elseif($type=='IDAT')
\r
1586 //Read image data block
\r
1587 $data.=fread($f,$n);
\r
1590 elseif($type=='IEND')
\r
1596 if($colspace=='Indexed' && empty($pal))
\r
1597 $this->Error('Missing palette in '.$file);
\r
1599 return array('w'=>$w,'h'=>$h,'cs'=>$colspace,'bpc'=>$bpc,'f'=>'FlateDecode','parms'=>$parms,'pal'=>$pal,'trns'=>$trns,'data'=>$data);
\r
1602 function _freadint($f)
\r
1604 //Read a 4-byte integer from file
\r
1605 $a=unpack('Ni',fread($f,4));
\r
1609 function _textstring($s)
\r
1611 //Format a text string
\r
1612 return '('.$this->_escape($s).')';
\r
1615 function _escape($s)
\r
1617 //Add \ before \, ( and )
\r
1618 return str_replace(')','\\)',str_replace('(','\\(',str_replace('\\','\\\\',$s)));
\r
1621 function _putstream($s)
\r
1623 $this->_out('stream');
\r
1625 $this->_out('endstream');
\r
1630 //Add a line to the document
\r
1631 if($this->state==2)
\r
1632 $this->pages[$this->page].=$s."\n";
\r
1634 $this->buffer.=$s."\n";
\r
1639 //Handle special IE contype request
\r
1640 if(isset($_SERVER['HTTP_USER_AGENT']) && $_SERVER['HTTP_USER_AGENT']=='contype')
\r
1642 header('Content-Type: application/pdf');
\r