2 /*******************************************************************************
7 * Author: Olivier PLATHEY *
8 *******************************************************************************/
10 define('FPDF_VERSION','1.6');
14 var $page; //current page number
15 var $n; //current object number
16 var $offsets; //array of object offsets
17 var $buffer; //buffer holding in-memory PDF
18 var $pages; //array containing pages
19 var $state; //current document state
20 var $compress; //compression flag
21 var $k; //scale factor (number of points in user unit)
22 var $DefOrientation; //default orientation
23 var $CurOrientation; //current orientation
24 var $PageFormats; //available page formats
25 var $DefPageFormat; //default page format
26 var $CurPageFormat; //current page format
27 var $PageSizes; //array storing non-default page sizes
28 var $wPt,$hPt; //dimensions of current page in points
29 var $w,$h; //dimensions of current page in user unit
30 var $lMargin; //left margin
31 var $tMargin; //top margin
32 var $rMargin; //right margin
33 var $bMargin; //page break margin
34 var $cMargin; //cell margin
35 var $x,$y; //current position in user unit
36 var $lasth; //height of last printed cell
37 var $LineWidth; //line width in user unit
38 var $CoreFonts; //array of standard font names
39 var $fonts; //array of used fonts
40 var $FontFiles; //array of font files
41 var $diffs; //array of encoding differences
42 var $FontFamily; //current font family
43 var $FontStyle; //current font style
44 var $underline; //underlining flag
45 var $CurrentFont; //current font info
46 var $FontSizePt; //current font size in points
47 var $FontSize; //current font size in user unit
48 var $DrawColor; //commands for drawing color
49 var $FillColor; //commands for filling color
50 var $TextColor; //commands for text color
51 var $ColorFlag; //indicates whether fill and text colors are different
52 var $ws; //word spacing
53 var $images; //array of used images
54 var $PageLinks; //array of links in pages
55 var $links; //array of internal links
56 var $AutoPageBreak; //automatic page breaking
57 var $PageBreakTrigger; //threshold used to trigger page breaks
58 var $InHeader; //flag set when processing header
59 var $InFooter; //flag set when processing footer
60 var $ZoomMode; //zoom display mode
61 var $LayoutMode; //layout display mode
63 var $subject; //subject
65 var $keywords; //keywords
66 var $creator; //creator
67 var $AliasNbPages; //alias for total number of pages
68 var $PDFVersion; //PDF version number
70 /*******************************************************************************
74 *******************************************************************************/
75 function FPDF($orientation='P', $unit='mm', $format='A4')
79 //Initialization of properties
84 $this->PageSizes
=array();
87 $this->FontFiles
=array();
89 $this->images
=array();
91 $this->InHeader
=false;
92 $this->InFooter
=false;
97 $this->underline
=false;
98 $this->DrawColor
='0 G';
99 $this->FillColor
='0 g';
100 $this->TextColor
='0 g';
101 $this->ColorFlag
=false;
104 $this->CoreFonts
=array('courier'=>'Courier', 'courierB'=>'Courier-Bold', 'courierI'=>'Courier-Oblique', 'courierBI'=>'Courier-BoldOblique',
105 'helvetica'=>'Helvetica', 'helveticaB'=>'Helvetica-Bold', 'helveticaI'=>'Helvetica-Oblique', 'helveticaBI'=>'Helvetica-BoldOblique',
106 'times'=>'Times-Roman', 'timesB'=>'Times-Bold', 'timesI'=>'Times-Italic', 'timesBI'=>'Times-BoldItalic',
107 'symbol'=>'Symbol', 'zapfdingbats'=>'ZapfDingbats');
118 $this->Error('Incorrect unit: '.$unit);
120 $this->PageFormats
=array('a3'=>array(841.89,1190.55), 'a4'=>array(595.28,841.89), 'a5'=>array(420.94,595.28),
121 'letter'=>array(612,792), 'legal'=>array(612,1008));
122 if(is_string($format))
123 $format=$this->_getpageformat($format);
124 $this->DefPageFormat
=$format;
125 $this->CurPageFormat
=$format;
127 $orientation=strtolower($orientation);
128 if($orientation=='p' ||
$orientation=='portrait')
130 $this->DefOrientation
='P';
131 $this->w
=$this->DefPageFormat
[0];
132 $this->h
=$this->DefPageFormat
[1];
134 elseif($orientation=='l' ||
$orientation=='landscape')
136 $this->DefOrientation
='L';
137 $this->w
=$this->DefPageFormat
[1];
138 $this->h
=$this->DefPageFormat
[0];
141 $this->Error('Incorrect orientation: '.$orientation);
142 $this->CurOrientation
=$this->DefOrientation
;
143 $this->wPt
=$this->w
*$this->k
;
144 $this->hPt
=$this->h
*$this->k
;
145 //Page margins (1 cm)
146 $margin=28.35/$this->k
;
147 $this->SetMargins($margin,$margin);
148 //Interior cell margin (1 mm)
149 $this->cMargin
=$margin/10;
150 //Line width (0.2 mm)
151 $this->LineWidth
=.567/$this->k
;
152 //Automatic page break
153 $this->SetAutoPageBreak(true,2*$margin);
154 //Full width display mode
155 $this->SetDisplayMode('fullwidth');
157 $this->SetCompression(true);
158 //Set default PDF version number
159 $this->PDFVersion
='1.3';
162 function SetMargins($left, $top, $right=null)
164 //Set left, top and right margins
165 $this->lMargin
=$left;
169 $this->rMargin
=$right;
172 function SetLeftMargin($margin)
175 $this->lMargin
=$margin;
176 if($this->page
>0 && $this->x
<$margin)
180 function SetTopMargin($margin)
183 $this->tMargin
=$margin;
186 function SetRightMargin($margin)
189 $this->rMargin
=$margin;
192 function SetAutoPageBreak($auto, $margin=0)
194 //Set auto page break mode and triggering margin
195 $this->AutoPageBreak
=$auto;
196 $this->bMargin
=$margin;
197 $this->PageBreakTrigger
=$this->h
-$margin;
200 function SetDisplayMode($zoom, $layout='continuous')
202 //Set display mode in viewer
203 if($zoom=='fullpage' ||
$zoom=='fullwidth' ||
$zoom=='real' ||
$zoom=='default' ||
!is_string($zoom))
204 $this->ZoomMode
=$zoom;
206 $this->Error('Incorrect zoom display mode: '.$zoom);
207 if($layout=='single' ||
$layout=='continuous' ||
$layout=='two' ||
$layout=='default')
208 $this->LayoutMode
=$layout;
210 $this->Error('Incorrect layout display mode: '.$layout);
213 function SetCompression($compress)
215 //Set page compression
216 if(function_exists('gzcompress'))
217 $this->compress
=$compress;
219 $this->compress
=false;
222 function SetTitle($title, $isUTF8=false)
226 $title=$this->_UTF8toUTF16($title);
230 function SetSubject($subject, $isUTF8=false)
232 //Subject of document
234 $subject=$this->_UTF8toUTF16($subject);
235 $this->subject
=$subject;
238 function SetAuthor($author, $isUTF8=false)
242 $author=$this->_UTF8toUTF16($author);
243 $this->author
=$author;
246 function SetKeywords($keywords, $isUTF8=false)
248 //Keywords of document
250 $keywords=$this->_UTF8toUTF16($keywords);
251 $this->keywords
=$keywords;
254 function SetCreator($creator, $isUTF8=false)
256 //Creator of document
258 $creator=$this->_UTF8toUTF16($creator);
259 $this->creator
=$creator;
262 function AliasNbPages($alias='{nb}')
264 //Define an alias for total number of pages
265 $this->AliasNbPages
=$alias;
271 die('<b>FPDF error:</b> '.$msg);
288 $this->InFooter
=true;
290 $this->InFooter
=false;
297 function AddPage($orientation='', $format='')
302 $family=$this->FontFamily
;
303 $style=$this->FontStyle
.($this->underline ?
'U' : '');
304 $size=$this->FontSizePt
;
305 $lw=$this->LineWidth
;
306 $dc=$this->DrawColor
;
307 $fc=$this->FillColor
;
308 $tc=$this->TextColor
;
309 $cf=$this->ColorFlag
;
313 $this->InFooter
=true;
315 $this->InFooter
=false;
320 $this->_beginpage($orientation,$format);
321 //Set line cap style to square
324 $this->LineWidth
=$lw;
325 $this->_out(sprintf('%.2F w',$lw*$this->k
));
328 $this->SetFont($family,$style,$size);
330 $this->DrawColor
=$dc;
333 $this->FillColor
=$fc;
336 $this->TextColor
=$tc;
337 $this->ColorFlag
=$cf;
339 $this->InHeader
=true;
341 $this->InHeader
=false;
343 if($this->LineWidth
!=$lw)
345 $this->LineWidth
=$lw;
346 $this->_out(sprintf('%.2F w',$lw*$this->k
));
350 $this->SetFont($family,$style,$size);
352 if($this->DrawColor
!=$dc)
354 $this->DrawColor
=$dc;
357 if($this->FillColor
!=$fc)
359 $this->FillColor
=$fc;
362 $this->TextColor
=$tc;
363 $this->ColorFlag
=$cf;
368 //To be implemented in your own inherited class
373 //To be implemented in your own inherited class
378 //Get current page number
382 function SetDrawColor($r, $g=null, $b=null)
384 //Set color for all stroking operations
385 if(($r==0 && $g==0 && $b==0) ||
$g===null)
386 $this->DrawColor
=sprintf('%.3F G',$r/255);
388 $this->DrawColor
=sprintf('%.3F %.3F %.3F RG',$r/255,$g/255,$b/255);
390 $this->_out($this->DrawColor
);
393 function SetFillColor($r, $g=null, $b=null)
395 //Set color for all filling operations
396 if(($r==0 && $g==0 && $b==0) ||
$g===null)
397 $this->FillColor
=sprintf('%.3F g',$r/255);
399 $this->FillColor
=sprintf('%.3F %.3F %.3F rg',$r/255,$g/255,$b/255);
400 $this->ColorFlag
=($this->FillColor
!=$this->TextColor
);
402 $this->_out($this->FillColor
);
405 function SetTextColor($r, $g=null, $b=null)
408 if(($r==0 && $g==0 && $b==0) ||
$g===null)
409 $this->TextColor
=sprintf('%.3F g',$r/255);
411 $this->TextColor
=sprintf('%.3F %.3F %.3F rg',$r/255,$g/255,$b/255);
412 $this->ColorFlag
=($this->FillColor
!=$this->TextColor
);
415 function GetStringWidth($s)
417 //Get width of a string in the current font
419 $cw = &$this->CurrentFont
['cw'];
424 return $w*$this->FontSize
/1000;
427 function SetLineWidth($width)
430 $this->LineWidth
=$width;
432 $this->_out(sprintf('%.2F w',$width*$this->k
));
435 function Line($x1, $y1, $x2, $y2)
438 $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
));
441 function Rect($x, $y, $w, $h, $style='')
446 elseif($style=='FD' ||
$style=='DF')
450 $this->_out(sprintf('%.2F %.2F %.2F %.2F re %s',$x*$this->k
,($this->h
-$y)*$this->k
,$w*$this->k
,-$h*$this->k
,$op));
453 function AddFont($family, $style='', $file='')
455 //Add a TrueType or Type1 font
456 $family=strtolower($family);
458 $file=str_replace(' ','',$family).strtolower($style).'.php';
461 $style=strtoupper($style);
464 $fontkey=$family.$style;
465 if(isset($this->fonts
[$fontkey]))
467 include($this->_getfontpath().$file);
469 $this->Error('Could not include font definition file');
470 $i=count($this->fonts
)+
1;
471 $this->fonts
[$fontkey]=array('i'=>$i, 'type'=>$type, 'name'=>$name, 'desc'=>$desc, 'up'=>$up, 'ut'=>$ut, 'cw'=>$cw, 'enc'=>$enc, 'file'=>$file);
474 //Search existing encodings
476 $nb=count($this->diffs
);
477 for($i=1;$i<=$nb;$i++
)
479 if($this->diffs
[$i]==$diff)
488 $this->diffs
[$d]=$diff;
490 $this->fonts
[$fontkey]['diff']=$d;
494 if($type=='TrueType')
495 $this->FontFiles
[$file]=array('length1'=>$originalsize);
497 $this->FontFiles
[$file]=array('length1'=>$size1, 'length2'=>$size2);
501 function SetFont($family, $style='', $size=0)
503 //Select a font; size given in points
504 global $fpdf_charwidths;
506 $family=strtolower($family);
508 $family=$this->FontFamily
;
511 elseif($family=='symbol' ||
$family=='zapfdingbats')
513 $style=strtoupper($style);
514 if(strpos($style,'U')!==false)
516 $this->underline
=true;
517 $style=str_replace('U','',$style);
520 $this->underline
=false;
524 $size=$this->FontSizePt
;
525 //Test if font is already selected
526 if($this->FontFamily
==$family && $this->FontStyle
==$style && $this->FontSizePt
==$size)
528 //Test if used for the first time
529 $fontkey=$family.$style;
530 if(!isset($this->fonts
[$fontkey]))
532 //Check if one of the standard fonts
533 if(isset($this->CoreFonts
[$fontkey]))
535 if(!isset($fpdf_charwidths[$fontkey]))
539 if($family=='times' ||
$family=='helvetica')
540 $file.=strtolower($style);
541 include($this->_getfontpath().$file.'.php');
542 if(!isset($fpdf_charwidths[$fontkey]))
543 $this->Error('Could not include font metric file');
545 $i=count($this->fonts
)+
1;
546 $name=$this->CoreFonts
[$fontkey];
547 $cw=$fpdf_charwidths[$fontkey];
548 $this->fonts
[$fontkey]=array('i'=>$i, 'type'=>'core', 'name'=>$name, 'up'=>-100, 'ut'=>50, 'cw'=>$cw);
551 $this->Error('Undefined font: '.$family.' '.$style);
554 $this->FontFamily
=$family;
555 $this->FontStyle
=$style;
556 $this->FontSizePt
=$size;
557 $this->FontSize
=$size/$this->k
;
558 $this->CurrentFont
= &$this->fonts
[$fontkey];
560 $this->_out(sprintf('BT /F%d %.2F Tf ET',$this->CurrentFont
['i'],$this->FontSizePt
));
563 function SetFontSize($size)
565 //Set font size in points
566 if($this->FontSizePt
==$size)
568 $this->FontSizePt
=$size;
569 $this->FontSize
=$size/$this->k
;
571 $this->_out(sprintf('BT /F%d %.2F Tf ET',$this->CurrentFont
['i'],$this->FontSizePt
));
576 //Create a new internal link
577 $n=count($this->links
)+
1;
578 $this->links
[$n]=array(0, 0);
582 function SetLink($link, $y=0, $page=-1)
584 //Set destination of internal link
589 $this->links
[$link]=array($page, $y);
592 function Link($x, $y, $w, $h, $link)
594 //Put a link on the page
595 $this->PageLinks
[$this->page
][]=array($x*$this->k
, $this->hPt
-$y*$this->k
, $w*$this->k
, $h*$this->k
, $link);
598 function Text($x, $y, $txt)
601 $s=sprintf('BT %.2F %.2F Td (%s) Tj ET',$x*$this->k
,($this->h
-$y)*$this->k
,$this->_escape($txt));
602 if($this->underline
&& $txt!='')
603 $s.=' '.$this->_dounderline($x,$y,$txt);
605 $s='q '.$this->TextColor
.' '.$s.' Q';
609 function AcceptPageBreak()
611 //Accept automatic page break or not
612 return $this->AutoPageBreak
;
615 function Cell($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link='')
619 if($this->y+
$h>$this->PageBreakTrigger
&& !$this->InHeader
&& !$this->InFooter
&& $this->AcceptPageBreak())
621 //Automatic page break
629 $this->AddPage($this->CurOrientation
,$this->CurPageFormat
);
634 $this->_out(sprintf('%.3F Tw',$ws*$k));
638 $w=$this->w
-$this->rMargin
-$this->x
;
640 if($fill ||
$border==1)
643 $op=($border==1) ?
'B' : 'f';
646 $s=sprintf('%.2F %.2F %.2F %.2F re %s ',$this->x
*$k,($this->h
-$this->y
)*$k,$w*$k,-$h*$k,$op);
648 if(is_string($border))
652 if(strpos($border,'L')!==false)
653 $s.=sprintf('%.2F %.2F m %.2F %.2F l S ',$x*$k,($this->h
-$y)*$k,$x*$k,($this->h
-($y+
$h))*$k);
654 if(strpos($border,'T')!==false)
655 $s.=sprintf('%.2F %.2F m %.2F %.2F l S ',$x*$k,($this->h
-$y)*$k,($x+
$w)*$k,($this->h
-$y)*$k);
656 if(strpos($border,'R')!==false)
657 $s.=sprintf('%.2F %.2F m %.2F %.2F l S ',($x+
$w)*$k,($this->h
-$y)*$k,($x+
$w)*$k,($this->h
-($y+
$h))*$k);
658 if(strpos($border,'B')!==false)
659 $s.=sprintf('%.2F %.2F m %.2F %.2F l S ',$x*$k,($this->h
-($y+
$h))*$k,($x+
$w)*$k,($this->h
-($y+
$h))*$k);
664 $dx=$w-$this->cMargin
-$this->GetStringWidth($txt);
666 $dx=($w-$this->GetStringWidth($txt))/2;
670 $s.='q '.$this->TextColor
.' ';
671 $txt2=str_replace(')','\\)',str_replace('(','\\(',str_replace('\\','\\\\',$txt)));
672 $s.=sprintf('BT %.2F %.2F Td (%s) Tj ET',($this->x+
$dx)*$k,($this->h
-($this->y+
.5*$h+
.3*$this->FontSize
))*$k,$txt2);
674 $s.=' '.$this->_dounderline($this->x+
$dx,$this->y+
.5*$h+
.3*$this->FontSize
,$txt);
678 $this->Link($this->x+
$dx,$this->y+
.5*$h-.5*$this->FontSize
,$this->GetStringWidth($txt),$this->FontSize
,$link);
688 $this->x
=$this->lMargin
;
694 function MultiCell($w, $h, $txt, $border=0, $align='J', $fill=false)
696 //Output text with automatic or explicit line breaks
697 $cw = &$this->CurrentFont
['cw'];
699 $w=$this->w
-$this->rMargin
-$this->x
;
700 $wmax=($w-2*$this->cMargin
)*1000/$this->FontSize
;
701 $s=str_replace("\r",'',$txt);
703 if($nb>0 && $s[$nb-1]=="\n")
717 if(strpos($border,'L')!==false)
719 if(strpos($border,'R')!==false)
721 $b=(strpos($border,'T')!==false) ?
$b2.'T' : $b2;
736 //Explicit line break
742 $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
749 if($border && $nl==2)
762 //Automatic line break
772 $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
778 $this->ws
=($ns>1) ?
($wmax-$ls)/1000*$this->FontSize
/($ns-1) : 0;
779 $this->_out(sprintf('%.3F Tw',$this->ws
*$this->k
));
781 $this->Cell($w,$h,substr($s,$j,$sep-$j),$b,2,$align,$fill);
789 if($border && $nl==2)
801 if($border && strpos($border,'B')!==false)
803 $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
804 $this->x
=$this->lMargin
;
807 function Write($h, $txt, $link='')
809 //Output text in flowing mode
810 $cw = &$this->CurrentFont
['cw'];
811 $w=$this->w
-$this->rMargin
-$this->x
;
812 $wmax=($w-2*$this->cMargin
)*1000/$this->FontSize
;
813 $s=str_replace("\r",'',$txt);
826 //Explicit line break
827 $this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link);
834 $this->x
=$this->lMargin
;
835 $w=$this->w
-$this->rMargin
-$this->x
;
836 $wmax=($w-2*$this->cMargin
)*1000/$this->FontSize
;
846 //Automatic line break
849 if($this->x
>$this->lMargin
)
852 $this->x
=$this->lMargin
;
854 $w=$this->w
-$this->rMargin
-$this->x
;
855 $wmax=($w-2*$this->cMargin
)*1000/$this->FontSize
;
862 $this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link);
866 $this->Cell($w,$h,substr($s,$j,$sep-$j),0,2,'',0,$link);
874 $this->x
=$this->lMargin
;
875 $w=$this->w
-$this->rMargin
-$this->x
;
876 $wmax=($w-2*$this->cMargin
)*1000/$this->FontSize
;
885 $this->Cell($l/1000*$this->FontSize
,$h,substr($s,$j),0,0,'',0,$link);
890 //Line feed; default value is last cell height
891 $this->x
=$this->lMargin
;
893 $this->y+
=$this->lasth
;
898 function Image($file, $x=null, $y=null, $w=0, $h=0, $type='', $link='')
900 //Put an image on the page
901 if(!isset($this->images
[$file]))
903 //First use of this image, get info
906 $pos=strrpos($file,'.');
908 $this->Error('Image file has no extension and no type was specified: '.$file);
909 $type=substr($file,$pos+
1);
911 $type=strtolower($type);
915 if(!method_exists($this,$mtd))
916 $this->Error('Unsupported image type: '.$type);
917 $info=$this->$mtd($file);
918 $info['i']=count($this->images
)+
1;
919 $this->images
[$file]=$info;
922 $info=$this->images
[$file];
923 //Automatic width and height calculation if needed
926 //Put image at 72 dpi
927 $w=$info['w']/$this->k
;
928 $h=$info['h']/$this->k
;
931 $w=$h*$info['w']/$info['h'];
933 $h=$w*$info['h']/$info['w'];
937 if($this->y+
$h>$this->PageBreakTrigger
&& !$this->InHeader
&& !$this->InFooter
&& $this->AcceptPageBreak())
939 //Automatic page break
941 $this->AddPage($this->CurOrientation
,$this->CurPageFormat
);
949 $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']));
951 $this->Link($x,$y,$w,$h,$link);
966 $this->x
=$this->w+
$x;
977 //Set y position and reset x
978 $this->x
=$this->lMargin
;
982 $this->y
=$this->h+
$y;
985 function SetXY($x, $y)
987 //Set x and y positions
992 function Output($name='', $dest='')
994 //Output PDF to some destination
997 $dest=strtoupper($dest);
1011 //Send to standard output
1013 $this->Error('Some data has already been output, can\'t send PDF file');
1014 if(php_sapi_name()!='cli')
1016 //We send to a browser
1017 header('Content-Type: application/pdf');
1019 $this->Error('Some data has already been output, can\'t send PDF file');
1020 header('Content-Length: '.strlen($this->buffer
));
1021 header('Content-Disposition: inline; filename="'.$name.'"');
1022 header('Cache-Control: private, max-age=0, must-revalidate');
1023 header('Pragma: public');
1024 ini_set('zlib.output_compression','0');
1031 $this->Error('Some data has already been output, can\'t send PDF file');
1032 header('Content-Type: application/x-download');
1034 $this->Error('Some data has already been output, can\'t send PDF file');
1035 header('Content-Length: '.strlen($this->buffer
));
1036 header('Content-Disposition: attachment; filename="'.$name.'"');
1037 header('Cache-Control: private, max-age=0, must-revalidate');
1038 header('Pragma: public');
1039 ini_set('zlib.output_compression','0');
1043 //Save to local file
1044 $f=fopen($name,'wb');
1046 $this->Error('Unable to create output file: '.$name);
1047 fwrite($f,$this->buffer
,strlen($this->buffer
));
1051 //Return as a string
1052 return $this->buffer
;
1054 $this->Error('Incorrect output destination: '.$dest);
1059 /*******************************************************************************
1061 * Protected methods *
1063 *******************************************************************************/
1064 function _dochecks()
1066 //Check availability of %F
1067 if(sprintf('%.1F',1.0)!='1.0')
1068 $this->Error('This version of PHP is not supported');
1069 //Check mbstring overloading
1070 if(ini_get('mbstring.func_overload') & 2)
1071 $this->Error('mbstring overloading must be disabled');
1072 //Disable runtime magic quotes
1073 if(get_magic_quotes_runtime())
1074 @set_magic_quotes_runtime
(0);
1077 function _getpageformat($format)
1079 $format=strtolower($format);
1080 if(!isset($this->PageFormats
[$format]))
1081 $this->Error('Unknown page format: '.$format);
1082 $a=$this->PageFormats
[$format];
1083 return array($a[0]/$this->k
, $a[1]/$this->k
);
1086 function _getfontpath()
1088 if(!defined('FPDF_FONTPATH') && is_dir(dirname(__FILE__
).'/font'))
1089 define('FPDF_FONTPATH',dirname(__FILE__
).'/font/');
1090 return defined('FPDF_FONTPATH') ? FPDF_FONTPATH
: '';
1093 function _beginpage($orientation, $format)
1096 $this->pages
[$this->page
]='';
1098 $this->x
=$this->lMargin
;
1099 $this->y
=$this->tMargin
;
1100 $this->FontFamily
='';
1102 if($orientation=='')
1103 $orientation=$this->DefOrientation
;
1105 $orientation=strtoupper($orientation[0]);
1107 $format=$this->DefPageFormat
;
1110 if(is_string($format))
1111 $format=$this->_getpageformat($format);
1113 if($orientation!=$this->CurOrientation ||
$format[0]!=$this->CurPageFormat
[0] ||
$format[1]!=$this->CurPageFormat
[1])
1116 if($orientation=='P')
1118 $this->w
=$format[0];
1119 $this->h
=$format[1];
1123 $this->w
=$format[1];
1124 $this->h
=$format[0];
1126 $this->wPt
=$this->w
*$this->k
;
1127 $this->hPt
=$this->h
*$this->k
;
1128 $this->PageBreakTrigger
=$this->h
-$this->bMargin
;
1129 $this->CurOrientation
=$orientation;
1130 $this->CurPageFormat
=$format;
1132 if($orientation!=$this->DefOrientation ||
$format[0]!=$this->DefPageFormat
[0] ||
$format[1]!=$this->DefPageFormat
[1])
1133 $this->PageSizes
[$this->page
]=array($this->wPt
, $this->hPt
);
1141 function _escape($s)
1143 //Escape special characters in strings
1144 $s=str_replace('\\','\\\\',$s);
1145 $s=str_replace('(','\\(',$s);
1146 $s=str_replace(')','\\)',$s);
1147 $s=str_replace("\r",'\\r',$s);
1151 function _textstring($s)
1153 //Format a text string
1154 return '('.$this->_escape($s).')';
1157 function _UTF8toUTF16($s)
1159 //Convert UTF-8 to UTF-16BE with BOM
1171 $res.=chr((($c1 & 0x0F)<<4) +
(($c2 & 0x3C)>>2));
1172 $res.=chr((($c2 & 0x03)<<6) +
($c3 & 0x3F));
1178 $res.=chr(($c1 & 0x1C)>>2);
1179 $res.=chr((($c1 & 0x03)<<6) +
($c2 & 0x3F));
1183 //Single-byte character
1184 $res.="\0".chr($c1);
1190 function _dounderline($x, $y, $txt)
1193 $up=$this->CurrentFont
['up'];
1194 $ut=$this->CurrentFont
['ut'];
1195 $w=$this->GetStringWidth($txt)+
$this->ws
*substr_count($txt,' ');
1196 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
);
1199 function _parsejpg($file)
1201 //Extract info from a JPEG file
1202 $a=GetImageSize($file);
1204 $this->Error('Missing or incorrect image file: '.$file);
1206 $this->Error('Not a JPEG file: '.$file);
1207 if(!isset($a['channels']) ||
$a['channels']==3)
1208 $colspace='DeviceRGB';
1209 elseif($a['channels']==4)
1210 $colspace='DeviceCMYK';
1212 $colspace='DeviceGray';
1213 $bpc=isset($a['bits']) ?
$a['bits'] : 8;
1215 $f=fopen($file,'rb');
1218 $data.=fread($f,8192);
1220 return array('w'=>$a[0], 'h'=>$a[1], 'cs'=>$colspace, 'bpc'=>$bpc, 'f'=>'DCTDecode', 'data'=>$data);
1223 function _parsepng($file)
1225 //Extract info from a PNG file
1226 $f=fopen($file,'rb');
1228 $this->Error('Can\'t open image file: '.$file);
1230 if($this->_readstream($f,8)!=chr(137).'PNG'.chr(13).chr(10).chr(26).chr(10))
1231 $this->Error('Not a PNG file: '.$file);
1233 $this->_readstream($f,4);
1234 if($this->_readstream($f,4)!='IHDR')
1235 $this->Error('Incorrect PNG file: '.$file);
1236 $w=$this->_readint($f);
1237 $h=$this->_readint($f);
1238 $bpc=ord($this->_readstream($f,1));
1240 $this->Error('16-bit depth not supported: '.$file);
1241 $ct=ord($this->_readstream($f,1));
1243 $colspace='DeviceGray';
1245 $colspace='DeviceRGB';
1247 $colspace='Indexed';
1249 $this->Error('Alpha channel not supported: '.$file);
1250 if(ord($this->_readstream($f,1))!=0)
1251 $this->Error('Unknown compression method: '.$file);
1252 if(ord($this->_readstream($f,1))!=0)
1253 $this->Error('Unknown filter method: '.$file);
1254 if(ord($this->_readstream($f,1))!=0)
1255 $this->Error('Interlacing not supported: '.$file);
1256 $this->_readstream($f,4);
1257 $parms='/DecodeParms <</Predictor 15 /Colors '.($ct==2 ?
3 : 1).' /BitsPerComponent '.$bpc.' /Columns '.$w.'>>';
1258 //Scan chunks looking for palette, transparency and image data
1264 $n=$this->_readint($f);
1265 $type=$this->_readstream($f,4);
1269 $pal=$this->_readstream($f,$n);
1270 $this->_readstream($f,4);
1272 elseif($type=='tRNS')
1274 //Read transparency info
1275 $t=$this->_readstream($f,$n);
1277 $trns=array(ord(substr($t,1,1)));
1279 $trns=array(ord(substr($t,1,1)), ord(substr($t,3,1)), ord(substr($t,5,1)));
1282 $pos=strpos($t,chr(0));
1286 $this->_readstream($f,4);
1288 elseif($type=='IDAT')
1290 //Read image data block
1291 $data.=$this->_readstream($f,$n);
1292 $this->_readstream($f,4);
1294 elseif($type=='IEND')
1297 $this->_readstream($f,$n+
4);
1300 if($colspace=='Indexed' && empty($pal))
1301 $this->Error('Missing palette in '.$file);
1303 return array('w'=>$w, 'h'=>$h, 'cs'=>$colspace, 'bpc'=>$bpc, 'f'=>'FlateDecode', 'parms'=>$parms, 'pal'=>$pal, 'trns'=>$trns, 'data'=>$data);
1306 function _readstream($f, $n)
1308 //Read n bytes from stream
1310 while($n>0 && !feof($f))
1314 $this->Error('Error while reading stream');
1319 $this->Error('Unexpected end of stream');
1323 function _readint($f)
1325 //Read a 4-byte integer from stream
1326 $a=unpack('Ni',$this->_readstream($f,4));
1330 function _parsegif($file)
1332 //Extract info from a GIF file (via PNG conversion)
1333 if(!function_exists('imagepng'))
1334 $this->Error('GD extension is required for GIF support');
1335 if(!function_exists('imagecreatefromgif'))
1336 $this->Error('GD has no GIF read support');
1337 $im=imagecreatefromgif($file);
1339 $this->Error('Missing or incorrect image file: '.$file);
1340 imageinterlace($im,0);
1341 $tmp=tempnam('.','gif');
1343 $this->Error('Unable to create a temporary file');
1344 if(!imagepng($im,$tmp))
1345 $this->Error('Error while saving to temporary file');
1347 $info=$this->_parsepng($tmp);
1354 //Begin a new object
1356 $this->offsets
[$this->n
]=strlen($this->buffer
);
1357 $this->_out($this->n
.' 0 obj');
1360 function _putstream($s)
1362 $this->_out('stream');
1364 $this->_out('endstream');
1369 //Add a line to the document
1371 $this->pages
[$this->page
].=$s."\n";
1373 $this->buffer
.=$s."\n";
1376 function _putpages()
1379 if(!empty($this->AliasNbPages
))
1381 //Replace number of pages
1382 for($n=1;$n<=$nb;$n++
)
1383 $this->pages
[$n]=str_replace($this->AliasNbPages
,$nb,$this->pages
[$n]);
1385 if($this->DefOrientation
=='P')
1387 $wPt=$this->DefPageFormat
[0]*$this->k
;
1388 $hPt=$this->DefPageFormat
[1]*$this->k
;
1392 $wPt=$this->DefPageFormat
[1]*$this->k
;
1393 $hPt=$this->DefPageFormat
[0]*$this->k
;
1395 $filter=($this->compress
) ?
'/Filter /FlateDecode ' : '';
1396 for($n=1;$n<=$nb;$n++
)
1400 $this->_out('<</Type /Page');
1401 $this->_out('/Parent 1 0 R');
1402 if(isset($this->PageSizes
[$n]))
1403 $this->_out(sprintf('/MediaBox [0 0 %.2F %.2F]',$this->PageSizes
[$n][0],$this->PageSizes
[$n][1]));
1404 $this->_out('/Resources 2 0 R');
1405 if(isset($this->PageLinks
[$n]))
1408 $annots='/Annots [';
1409 foreach($this->PageLinks
[$n] as $pl)
1411 $rect=sprintf('%.2F %.2F %.2F %.2F',$pl[0],$pl[1],$pl[0]+
$pl[2],$pl[1]-$pl[3]);
1412 $annots.='<</Type /Annot /Subtype /Link /Rect ['.$rect.'] /Border [0 0 0] ';
1413 if(is_string($pl[4]))
1414 $annots.='/A <</S /URI /URI '.$this->_textstring($pl[4]).'>>>>';
1417 $l=$this->links
[$pl[4]];
1418 $h=isset($this->PageSizes
[$l[0]]) ?
$this->PageSizes
[$l[0]][1] : $hPt;
1419 $annots.=sprintf('/Dest [%d 0 R /XYZ 0 %.2F null]>>',1+
2*$l[0],$h-$l[1]*$this->k
);
1422 $this->_out($annots.']');
1424 $this->_out('/Contents '.($this->n+
1).' 0 R>>');
1425 $this->_out('endobj');
1427 $p=($this->compress
) ?
gzcompress($this->pages
[$n]) : $this->pages
[$n];
1429 $this->_out('<<'.$filter.'/Length '.strlen($p).'>>');
1430 $this->_putstream($p);
1431 $this->_out('endobj');
1434 $this->offsets
[1]=strlen($this->buffer
);
1435 $this->_out('1 0 obj');
1436 $this->_out('<</Type /Pages');
1438 for($i=0;$i<$nb;$i++
)
1439 $kids.=(3+
2*$i).' 0 R ';
1440 $this->_out($kids.']');
1441 $this->_out('/Count '.$nb);
1442 $this->_out(sprintf('/MediaBox [0 0 %.2F %.2F]',$wPt,$hPt));
1444 $this->_out('endobj');
1447 function _putfonts()
1450 foreach($this->diffs
as $diff)
1454 $this->_out('<</Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences ['.$diff.']>>');
1455 $this->_out('endobj');
1457 foreach($this->FontFiles
as $file=>$info)
1459 //Font file embedding
1461 $this->FontFiles
[$file]['n']=$this->n
;
1463 $f=fopen($this->_getfontpath().$file,'rb',1);
1465 $this->Error('Font file not found');
1467 $font.=fread($f,8192);
1469 $compressed=(substr($file,-2)=='.z');
1470 if(!$compressed && isset($info['length2']))
1472 $header=(ord($font[0])==128);
1475 //Strip first binary header
1476 $font=substr($font,6);
1478 if($header && ord($font[$info['length1']])==128)
1480 //Strip second binary header
1481 $font=substr($font,0,$info['length1']).substr($font,$info['length1']+
6);
1484 $this->_out('<</Length '.strlen($font));
1486 $this->_out('/Filter /FlateDecode');
1487 $this->_out('/Length1 '.$info['length1']);
1488 if(isset($info['length2']))
1489 $this->_out('/Length2 '.$info['length2'].' /Length3 0');
1491 $this->_putstream($font);
1492 $this->_out('endobj');
1494 foreach($this->fonts
as $k=>$font)
1497 $this->fonts
[$k]['n']=$this->n+
1;
1498 $type=$font['type'];
1499 $name=$font['name'];
1504 $this->_out('<</Type /Font');
1505 $this->_out('/BaseFont /'.$name);
1506 $this->_out('/Subtype /Type1');
1507 if($name!='Symbol' && $name!='ZapfDingbats')
1508 $this->_out('/Encoding /WinAnsiEncoding');
1510 $this->_out('endobj');
1512 elseif($type=='Type1' ||
$type=='TrueType')
1514 //Additional Type1 or TrueType font
1516 $this->_out('<</Type /Font');
1517 $this->_out('/BaseFont /'.$name);
1518 $this->_out('/Subtype /'.$type);
1519 $this->_out('/FirstChar 32 /LastChar 255');
1520 $this->_out('/Widths '.($this->n+
1).' 0 R');
1521 $this->_out('/FontDescriptor '.($this->n+
2).' 0 R');
1524 if(isset($font['diff']))
1525 $this->_out('/Encoding '.($nf+
$font['diff']).' 0 R');
1527 $this->_out('/Encoding /WinAnsiEncoding');
1530 $this->_out('endobj');
1535 for($i=32;$i<=255;$i++
)
1536 $s.=$cw[chr($i)].' ';
1537 $this->_out($s.']');
1538 $this->_out('endobj');
1541 $s='<</Type /FontDescriptor /FontName /'.$name;
1542 foreach($font['desc'] as $k=>$v)
1544 $file=$font['file'];
1546 $s.=' /FontFile'.($type=='Type1' ?
'' : '2').' '.$this->FontFiles
[$file]['n'].' 0 R';
1547 $this->_out($s.'>>');
1548 $this->_out('endobj');
1552 //Allow for additional types
1553 $mtd='_put'.strtolower($type);
1554 if(!method_exists($this,$mtd))
1555 $this->Error('Unsupported font type: '.$type);
1561 function _putimages()
1563 $filter=($this->compress
) ?
'/Filter /FlateDecode ' : '';
1564 reset($this->images
);
1565 while(list($file,$info)=each($this->images
))
1568 $this->images
[$file]['n']=$this->n
;
1569 $this->_out('<</Type /XObject');
1570 $this->_out('/Subtype /Image');
1571 $this->_out('/Width '.$info['w']);
1572 $this->_out('/Height '.$info['h']);
1573 if($info['cs']=='Indexed')
1574 $this->_out('/ColorSpace [/Indexed /DeviceRGB '.(strlen($info['pal'])/3-1).' '.($this->n+
1).' 0 R]');
1577 $this->_out('/ColorSpace /'.$info['cs']);
1578 if($info['cs']=='DeviceCMYK')
1579 $this->_out('/Decode [1 0 1 0 1 0 1 0]');
1581 $this->_out('/BitsPerComponent '.$info['bpc']);
1582 if(isset($info['f']))
1583 $this->_out('/Filter /'.$info['f']);
1584 if(isset($info['parms']))
1585 $this->_out($info['parms']);
1586 if(isset($info['trns']) && is_array($info['trns']))
1589 for($i=0;$i<count($info['trns']);$i++
)
1590 $trns.=$info['trns'][$i].' '.$info['trns'][$i].' ';
1591 $this->_out('/Mask ['.$trns.']');
1593 $this->_out('/Length '.strlen($info['data']).'>>');
1594 $this->_putstream($info['data']);
1595 unset($this->images
[$file]['data']);
1596 $this->_out('endobj');
1598 if($info['cs']=='Indexed')
1601 $pal=($this->compress
) ?
gzcompress($info['pal']) : $info['pal'];
1602 $this->_out('<<'.$filter.'/Length '.strlen($pal).'>>');
1603 $this->_putstream($pal);
1604 $this->_out('endobj');
1609 function _putxobjectdict()
1611 foreach($this->images
as $image)
1612 $this->_out('/I'.$image['i'].' '.$image['n'].' 0 R');
1615 function _putresourcedict()
1617 $this->_out('/ProcSet [/PDF /Text /ImageB /ImageC /ImageI]');
1618 $this->_out('/Font <<');
1619 foreach($this->fonts
as $font)
1620 $this->_out('/F'.$font['i'].' '.$font['n'].' 0 R');
1622 $this->_out('/XObject <<');
1623 $this->_putxobjectdict();
1627 function _putresources()
1630 $this->_putimages();
1631 //Resource dictionary
1632 $this->offsets
[2]=strlen($this->buffer
);
1633 $this->_out('2 0 obj');
1635 $this->_putresourcedict();
1637 $this->_out('endobj');
1642 $this->_out('/Producer '.$this->_textstring('FPDF '.FPDF_VERSION
));
1643 if(!empty($this->title
))
1644 $this->_out('/Title '.$this->_textstring($this->title
));
1645 if(!empty($this->subject
))
1646 $this->_out('/Subject '.$this->_textstring($this->subject
));
1647 if(!empty($this->author
))
1648 $this->_out('/Author '.$this->_textstring($this->author
));
1649 if(!empty($this->keywords
))
1650 $this->_out('/Keywords '.$this->_textstring($this->keywords
));
1651 if(!empty($this->creator
))
1652 $this->_out('/Creator '.$this->_textstring($this->creator
));
1653 $this->_out('/CreationDate '.$this->_textstring('D:'.@date
('YmdHis')));
1656 function _putcatalog()
1658 $this->_out('/Type /Catalog');
1659 $this->_out('/Pages 1 0 R');
1660 if($this->ZoomMode
=='fullpage')
1661 $this->_out('/OpenAction [3 0 R /Fit]');
1662 elseif($this->ZoomMode
=='fullwidth')
1663 $this->_out('/OpenAction [3 0 R /FitH null]');
1664 elseif($this->ZoomMode
=='real')
1665 $this->_out('/OpenAction [3 0 R /XYZ null null 1]');
1666 elseif(!is_string($this->ZoomMode
))
1667 $this->_out('/OpenAction [3 0 R /XYZ null null '.($this->ZoomMode
/100).']');
1668 if($this->LayoutMode
=='single')
1669 $this->_out('/PageLayout /SinglePage');
1670 elseif($this->LayoutMode
=='continuous')
1671 $this->_out('/PageLayout /OneColumn');
1672 elseif($this->LayoutMode
=='two')
1673 $this->_out('/PageLayout /TwoColumnLeft');
1676 function _putheader()
1678 $this->_out('%PDF-'.$this->PDFVersion
);
1681 function _puttrailer()
1683 $this->_out('/Size '.($this->n+
1));
1684 $this->_out('/Root '.$this->n
.' 0 R');
1685 $this->_out('/Info '.($this->n
-1).' 0 R');
1690 $this->_putheader();
1692 $this->_putresources();
1698 $this->_out('endobj');
1702 $this->_putcatalog();
1704 $this->_out('endobj');
1706 $o=strlen($this->buffer
);
1707 $this->_out('xref');
1708 $this->_out('0 '.($this->n+
1));
1709 $this->_out('0000000000 65535 f ');
1710 for($i=1;$i<=$this->n
;$i++
)
1711 $this->_out(sprintf('%010d 00000 n ',$this->offsets
[$i]));
1713 $this->_out('trailer');
1715 $this->_puttrailer();
1717 $this->_out('startxref');
1719 $this->_out('%%EOF');
1725 //Handle special IE contype request
1726 if(isset($_SERVER['HTTP_USER_AGENT']) && $_SERVER['HTTP_USER_AGENT']=='contype')
1728 header('Content-Type: application/pdf');