2 /*******************************************************************************
6 * Author: Olivier PLATHEY *
9 * You may use and modify this software as you wish. *
10 *******************************************************************************/
12 if (!class_exists('FPDF')) {
14 define('FPDF_FONTPATH',dirname(__FILE__)."/fpdf/");
15 define('FPDF_VERSION','1.52');
20 var $page; //current page number
21 var $n; //current object number
22 var $offsets; //array of object offsets
23 var $buffer; //buffer holding in-memory PDF
24 var $pages; //array containing pages
25 var $state; //current document state
26 var $compress; //compression flag
27 var $DefOrientation; //default orientation
28 var $CurOrientation; //current orientation
29 var $OrientationChanges; //array indicating orientation changes
30 var $k; //scale factor (number of points in user unit)
31 var $fwPt,$fhPt; //dimensions of page format in points
32 var $fw,$fh; //dimensions of page format in user unit
33 var $wPt,$hPt; //current dimensions of page in points
34 var $w,$h; //current dimensions of page in user unit
35 var $lMargin; //left margin
36 var $tMargin; //top margin
37 var $rMargin; //right margin
38 var $bMargin; //page break margin
39 var $cMargin; //cell margin
40 var $x,$y; //current position in user unit for cell positioning
41 var $lasth; //height of last cell printed
42 var $LineWidth; //line width in user unit
43 var $CoreFonts; //array of standard font names
44 var $fonts; //array of used fonts
45 var $FontFiles; //array of font files
46 var $diffs; //array of encoding differences
47 var $images; //array of used images
48 var $PageLinks; //array of links in pages
49 var $links; //array of internal links
50 var $FontFamily; //current font family
51 var $FontStyle; //current font style
52 var $underline; //underlining flag
53 var $CurrentFont; //current font info
54 var $FontSizePt; //current font size in points
55 var $FontSize; //current font size in user unit
56 var $DrawColor; //commands for drawing color
57 var $FillColor; //commands for filling color
58 var $TextColor; //commands for text color
59 var $ColorFlag; //indicates whether fill and text colors are different
60 var $ws; //word spacing
61 var $AutoPageBreak; //automatic page breaking
62 var $PageBreakTrigger; //threshold used to trigger page breaks
63 var $InFooter; //flag set when processing footer
64 var $ZoomMode; //zoom display mode
65 var $LayoutMode; //layout display mode
67 var $subject; //subject
69 var $keywords; //keywords
70 var $creator; //creator
71 var $producer; //Producer
72 var $AliasNbPages; //alias for total number of pages
74 /*******************************************************************************
78 *******************************************************************************/
79 function FPDF ($orientation='P', $unit='mm', $format='A4') {
82 //Initialization of properties
87 $this->OrientationChanges=array();
90 $this->FontFiles=array();
92 $this->images=array();
94 $this->InFooter=false;
99 $this->underline=false;
100 $this->DrawColor='0 G';
101 $this->FillColor='0 g';
102 $this->TextColor='0 g';
103 $this->ColorFlag=false;
106 $this->CoreFonts=array('japanees'=>'Japanees',
107 'courier'=>'Courier','courierB'=>'Courier-Bold','courierI'=>'Courier-Oblique','courierBI'=>'Courier-BoldOblique',
108 'helvetica'=>'Helvetica','helveticaB'=>'Helvetica-Bold','helveticaI'=>'Helvetica-Oblique','helveticaBI'=>'Helvetica-BoldOblique',
109 'times'=>'Times-Roman','timesB'=>'Times-Bold','timesI'=>'Times-Italic','timesBI'=>'Times-BoldItalic',
110 'symbol'=>'Symbol','zapfdingbats'=>'ZapfDingbats');
121 $this->Error('Incorrect unit: '.$unit);
123 if (is_string($format))
125 $format=strtolower($format);
127 $format=array(841.89,1190.55);
128 elseif ($format=='a4')
129 $format=array(595.28,841.89);
130 elseif ($format=='a5')
131 $format=array(420.94,595.28);
132 elseif ($format=='letter')
133 $format=array(612,792);
134 elseif ($format=='legal')
135 $format=array(612,1008);
137 $this->Error('Unknown page format: '.$format);
138 $this->fwPt=$format[0];
139 $this->fhPt=$format[1];
143 $this->fwPt=$format[0]*$this->k;
144 $this->fhPt=$format[1]*$this->k;
146 $this->fw=$this->fwPt/$this->k;
147 $this->fh=$this->fhPt/$this->k;
149 $orientation=strtolower($orientation);
150 if ($orientation=='p' or $orientation=='portrait')
152 $this->DefOrientation='P';
153 $this->wPt=$this->fwPt;
154 $this->hPt=$this->fhPt;
156 elseif ($orientation=='l' or $orientation=='landscape')
158 $this->DefOrientation='L';
159 $this->wPt=$this->fhPt;
160 $this->hPt=$this->fwPt;
163 $this->Error('Incorrect orientation: '.$orientation);
164 $this->CurOrientation=$this->DefOrientation;
165 $this->w=$this->wPt/$this->k;
166 $this->h=$this->hPt/$this->k;
167 //Page margins (1 cm)
168 $margin=28.35/$this->k;
169 $this->SetMargins($margin,$margin);
170 //Interior cell margin (1 mm)
171 $this->cMargin=$margin/10;
172 //Line width (0.2 mm)
173 $this->LineWidth=.567/$this->k;
174 //Automatic page break
175 $this->SetAutoPageBreak(true,2*$margin);
176 //Full width display mode
177 $this->SetDisplayMode('fullwidth');
179 $this->SetCompression(true);
182 function SetMargins($left,$top,$right=-1) {
183 //Set left, top and right margins
184 $this->lMargin=$left;
188 $this->rMargin=$right;
191 function SetLeftMargin($margin) {
193 $this->lMargin=$margin;
194 if ($this->page>0 and $this->x<$margin)
198 function SetTopMargin($margin) {
200 $this->tMargin=$margin;
203 function SetRightMargin($margin) {
205 $this->rMargin=$margin;
208 function SetAutoPageBreak($auto,$margin=0) {
209 //Set auto page break mode and triggering margin
210 $this->AutoPageBreak=$auto;
211 $this->bMargin=$margin;
212 $this->PageBreakTrigger=$this->h-$margin;
215 function SetDisplayMode($zoom,$layout='continuous') {
216 //Set display mode in viewer
217 if ($zoom=='fullpage' or $zoom=='fullwidth' or $zoom=='real' or $zoom=='default' or !is_string($zoom))
218 $this->ZoomMode=$zoom;
220 $this->Error('Incorrect zoom display mode: '.$zoom);
221 if ($layout=='single' or $layout=='continuous' or $layout=='two' or $layout=='default')
222 $this->LayoutMode=$layout;
224 $this->Error('Incorrect layout display mode: '.$layout);
227 function SetCompression($compress) {
228 //Set page compression
229 if (function_exists('gzcompress'))
230 $this->compress=$compress;
232 $this->compress=false;
235 function SetTitle($title) {
240 function SetSubject($subject) {
241 //Subject of document
242 $this->subject=$subject;
245 function SetAuthor($author) {
247 $this->author=$author;
250 function SetKeywords($keywords) {
251 //Keywords of document
252 $this->keywords=$keywords;
255 function SetCreator($creator) {
256 //Creator of document
257 $this->creator=$creator;
260 function SetProducer($producer) {
261 //Producer of document
262 $this->producer=$producer;
265 function AliasNbPages($alias='{nb}') {
266 //Define an alias for total number of pages
267 $this->AliasNbPages=$alias;
270 function Error($msg) {
272 die('<B>FPDF error: </B>'.$msg);
288 $this->InFooter=true;
290 $this->InFooter=false;
297 function AddPage($orientation='') {
301 $family=$this->FontFamily;
302 $style=$this->FontStyle.($this->underline ? 'U' : '');
303 $size=$this->FontSizePt;
304 $lw=$this->LineWidth;
305 $dc=$this->DrawColor;
306 $fc=$this->FillColor;
307 $tc=$this->TextColor;
308 $cf=$this->ColorFlag;
311 $this->InFooter=true;
313 $this->InFooter=false;
318 $this->_beginpage($orientation);
319 //Set line cap style to square
322 $this->LineWidth=$lw;
323 $this->_out(sprintf('%.2f w',$lw*$this->k));
326 $this->SetFont($family,$style,$size);
328 $this->DrawColor=$dc;
331 $this->FillColor=$fc;
334 $this->TextColor=$tc;
335 $this->ColorFlag=$cf;
339 if ($this->LineWidth!=$lw) {
340 $this->LineWidth=$lw;
341 $this->_out(sprintf('%.2f w',$lw*$this->k));
345 $this->SetFont($family,$style,$size);
347 if ($this->DrawColor!=$dc) {
348 $this->DrawColor=$dc;
351 if ($this->FillColor!=$fc) {
352 $this->FillColor=$fc;
355 $this->TextColor=$tc;
356 $this->ColorFlag=$cf;
360 //To be implemented in your own inherited class
364 //To be implemented in your own inherited class
368 //Get current page number
372 function SetDrawColor($r,$g=-1,$b=-1) {
373 //Set color for all stroking operations
374 if (($r==0 and $g==0 and $b==0) or $g==-1)
375 $this->DrawColor=sprintf('%.3f G',$r/255);
377 $this->DrawColor=sprintf('%.3f %.3f %.3f RG',$r/255,$g/255,$b/255);
379 $this->_out($this->DrawColor);
382 function SetFillColor($r,$g=-1,$b=-1) {
383 //Set color for all filling operations
384 if (($r==0 and $g==0 and $b==0) or $g==-1)
385 $this->FillColor=sprintf('%.3f g',$r/255);
387 $this->FillColor=sprintf('%.3f %.3f %.3f rg',$r/255,$g/255,$b/255);
388 $this->ColorFlag=($this->FillColor!=$this->TextColor);
390 $this->_out($this->FillColor);
393 function SetTextColor($r,$g=-1,$b=-1) {
395 if (($r==0 and $g==0 and $b==0) or $g==-1)
396 $this->TextColor=sprintf('%.3f g',$r/255);
398 $this->TextColor=sprintf('%.3f %.3f %.3f rg',$r/255,$g/255,$b/255);
399 $this->ColorFlag=($this->FillColor!=$this->TextColor);
402 function GetStringWidth($s) {
403 //Get width of a string in the current font
405 $cw=&$this->CurrentFont['cw'];
410 return $w*$this->FontSize/1000;
413 function SetLineWidth($width) {
415 $this->LineWidth=$width;
417 $this->_out(sprintf('%.2f w',$width*$this->k));
420 function Line($x1,$y1,$x2,$y2) {
422 $this->_out(sprintf('%.2f %.2f m %.2f %.2f l S',$x1*$this->k,($this->h-$y1)*$this->k,$x2*$this->k,($this->h-$y2)*$this->k));
425 function Rect($x,$y,$w,$h,$style='') {
429 elseif ($style=='FD' or $style=='DF')
433 $this->_out(sprintf('%.2f %.2f %.2f %.2f re %s',$x*$this->k,($this->h-$y)*$this->k,$w*$this->k,-$h*$this->k,$op));
436 function AddFont($family,$style='',$file='') {
437 //Add a TrueType or Type1 font
438 $family=strtolower($family);
439 if ($family=='arial')
441 $style=strtoupper($style);
444 if (isset($this->fonts[$family.$style]))
445 $this->Error('Font already added: '.$family.' '.$style);
447 $file=str_replace(' ','',$family).strtolower($style).'.php';
448 if (defined('FPDF_FONTPATH'))
449 $file = FPDF_FONTPATH . $file;
452 $this->Error('Could not include font definition file');
453 $i=count($this->fonts)+1;
454 $this->fonts[$family.$style]=array('i'=>$i,'type'=>$type,'name'=>$name,'desc'=>$desc,'up'=>$up,'ut'=>$ut,'cw'=>$cw,'enc'=>$enc,'file'=>$file);
456 //Search existing encodings
458 $nb=count($this->diffs);
459 for ($i=1;$i<=$nb;$i++)
460 if ($this->diffs[$i]==$diff) {
466 $this->diffs[$d]=$diff;
468 $this->fonts[$family.$style]['diff']=$d;
471 if ($type=='TrueType')
472 $this->FontFiles[$file]=array('length1'=>$originalsize);
474 $this->FontFiles[$file]=array('length1'=>$size1,'length2'=>$size2);
478 function SetFont($family,$style='',$size=0) {
479 //Select a font; size given in points
480 global $fpdf_charwidths;
482 $family=strtolower($family);
484 $family=$this->FontFamily;
485 if ($family=='arial')
487 elseif ($family=='symbol' or $family=='zapfdingbats')
489 $style=strtoupper($style);
490 if (is_int(strpos($style,'U'))) {
491 $this->underline=true;
492 $style=str_replace('U','',$style);
495 $this->underline=false;
499 $size=$this->FontSizePt;
500 //Test if font is already selected
501 if ($this->FontFamily==$family and $this->FontStyle==$style and $this->FontSizePt==$size)
503 //Test if used for the first time
504 $fontkey=$family.$style;
505 if (!isset($this->fonts[$fontkey])) {
506 //Check if one of the standard fonts
507 if (isset($this->CoreFonts[$fontkey])) {
508 if (!isset($fpdf_charwidths[$fontkey])) {
511 if ($family=='times' or $family=='helvetica')
512 $file.=strtolower($style);
514 if (defined('FPDF_FONTPATH'))
515 $file=FPDF_FONTPATH.$file;
517 if (!isset($fpdf_charwidths[$fontkey]))
518 $this->Error('Could not include font metric file');
520 $i=count($this->fonts)+1;
521 $this->fonts[$fontkey]=array('i'=>$i,'type'=>'core','name'=>$this->CoreFonts[$fontkey],'up'=>-100,'ut'=>50,'cw'=>$fpdf_charwidths[$fontkey]);
524 $this->Error('Undefined font: '.$family.' '.$style);
527 $this->FontFamily=$family;
528 $this->FontStyle=$style;
529 $this->FontSizePt=$size;
530 $this->FontSize=$size/$this->k;
531 $this->CurrentFont=&$this->fonts[$fontkey];
533 $this->_out(sprintf('BT /F%d %.2f Tf ET',$this->CurrentFont['i'],$this->FontSizePt));
536 function SetFontSize($size) {
537 //Set font size in points
538 if ($this->FontSizePt==$size)
540 $this->FontSizePt=$size;
541 $this->FontSize=$size/$this->k;
543 $this->_out(sprintf('BT /F%d %.2f Tf ET',$this->CurrentFont['i'],$this->FontSizePt));
547 //Create a new internal link
548 $n=count($this->links)+1;
549 $this->links[$n]=array(0,0);
553 function SetLink($link,$y=0,$page=-1) {
554 //Set destination of internal link
559 $this->links[$link]=array($page,$y);
562 function Link($x,$y,$w,$h,$link) {
563 //Put a link on the page
564 $this->PageLinks[$this->page][]=array($x*$this->k,$this->hPt-$y*$this->k,$w*$this->k,$h*$this->k,$link);
567 function Text($x,$y,$txt) {
569 $s=sprintf('BT %.2f %.2f Td (%s) Tj ET',$x*$this->k,($this->h-$y)*$this->k,$this->_escape($txt));
570 if ($this->underline and $txt!='')
571 $s.=' '.$this->_dounderline($x,$y,$txt);
572 if ($this->ColorFlag)
573 $s='q '.$this->TextColor.' '.$s.' Q';
577 function AcceptPageBreak() {
578 //Accept automatic page break or not
579 return $this->AutoPageBreak;
582 function Cell($w,$h=0,$txt='',$border=0,$ln=0,$align='',$fill=0,$link='') {
585 if ($this->y+$h>$this->PageBreakTrigger and !$this->InFooter and $this->AcceptPageBreak()) {
586 //Automatic page break
593 $this->AddPage($this->CurOrientation);
597 $this->_out(sprintf('%.3f Tw',$ws*$k));
601 $w=$this->w-$this->rMargin-$this->x;
603 if ($fill==1 or $border==1) {
605 $op=($border==1) ? 'B' : 'f';
608 $s=sprintf('%.2f %.2f %.2f %.2f re %s ',$this->x*$k,($this->h-$this->y)*$k,$w*$k,-$h*$k,$op);
610 if (is_string($border)) {
613 if (is_int(strpos($border,'L')))
614 $s.=sprintf('%.2f %.2f m %.2f %.2f l S ',$x*$k,($this->h-$y)*$k,$x*$k,($this->h-($y+$h))*$k);
615 if (is_int(strpos($border,'T')))
616 $s.=sprintf('%.2f %.2f m %.2f %.2f l S ',$x*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-$y)*$k);
617 if (is_int(strpos($border,'R')))
618 $s.=sprintf('%.2f %.2f m %.2f %.2f l S ',($x+$w)*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
619 if (is_int(strpos($border,'B')))
620 $s.=sprintf('%.2f %.2f m %.2f %.2f l S ',$x*$k,($this->h-($y+$h))*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
624 $dx=$w-$this->cMargin-$this->GetStringWidth($txt);
626 $dx=($w-$this->GetStringWidth($txt))/2;
629 if ($this->ColorFlag)
630 $s.='q '.$this->TextColor.' ';
631 $txt2=str_replace(')','\\)',str_replace('(','\\(',str_replace('\\','\\\\',$txt)));
632 $s.=sprintf('BT %.2f %.2f Td (%s) Tj ET',($this->x+$dx)*$k,($this->h-($this->y+.5*$h+.3*$this->FontSize))*$k,$txt2);
633 if ($this->underline)
634 $s.=' '.$this->_dounderline($this->x+$dx,$this->y+.5*$h+.3*$this->FontSize,$txt);
635 if ($this->ColorFlag)
638 $this->Link($this->x+$dx,$this->y+.5*$h-.5*$this->FontSize,$this->GetStringWidth($txt),$this->FontSize,$link);
647 $this->x=$this->lMargin;
653 function MultiCell($w,$h,$txt,$border=0,$align='J',$fill=0) {
654 //Output text with automatic or explicit line breaks
655 $cw=&$this->CurrentFont['cw'];
657 $w=$this->w-$this->rMargin-$this->x;
658 $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
659 $s=str_replace("\r",'',$txt);
661 if ($nb>0 and $s[$nb-1]=="\n")
673 if (is_int(strpos($border,'L')))
675 if (is_int(strpos($border,'R')))
677 $b=is_int(strpos($border,'T')) ? $b2.'T' : $b2;
690 //Explicit line break
695 $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
702 if ($border and $nl==2)
713 //Automatic line break
721 $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
726 $this->ws=($ns>1) ? ($wmax-$ls)/1000*$this->FontSize/($ns-1) : 0;
727 $this->_out(sprintf('%.3f Tw',$this->ws*$this->k));
729 $this->Cell($w,$h,substr($s,$j,$sep-$j),$b,2,$align,$fill);
737 if ($border and $nl==2)
748 if ($border and is_int(strpos($border,'B')))
750 $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
751 $this->x=$this->lMargin;
754 function Write($h,$txt,$link='') {
755 //Output text in flowing mode
756 $cw=&$this->CurrentFont['cw'];
757 $w=$this->w - $this->rMargin - $this->x;
758 $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
759 $s=str_replace("\r",'',$txt);
770 //Explicit line break
771 $this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link);
777 $this->x=$this->lMargin;
778 $w=$this->w-$this->rMargin-$this->x;
779 $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
788 //Automatic line break
790 if ($this->x>$this->lMargin) {
792 $this->x=$this->lMargin;
794 $w=$this->w-$this->rMargin-$this->x;
795 $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
802 $this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link);
805 $this->Cell($w,$h,substr($s,$j,$sep-$j),0,2,'',0,$link);
812 $this->x=$this->lMargin;
813 $w=$this->w-$this->rMargin-$this->x;
814 $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
823 $this->Cell($l/1000*$this->FontSize,$h,substr($s,$j),0,0,'',0,$link);
826 function Image($file,$x,$y,$w=0,$h=0,$type='',$link='') {
827 //Put an image on the page
828 if (!isset($this->images[$file])) {
829 //First use of image, get info
831 $pos=strrpos($file,'.');
833 $this->Error('Image file has no extension and no type was specified: '.$file);
834 $type=substr($file,$pos+1);
836 $type=strtolower($type);
837 $mqr=get_magic_quotes_runtime();
838 set_magic_quotes_runtime(0);
839 if ($type=='jpg' or $type=='jpeg')
840 $info=$this->_parsejpg($file);
841 elseif ($type=='png')
842 $info=$this->_parsepng($file);
843 elseif ($type=='gif')
844 $info=$this->_parsegif ($file);
847 //Allow for additional formats
849 if (!method_exists($this,$mtd))
850 $this->Error('Unsupported image type: '.$type);
851 $info=$this->$mtd($file);
853 set_magic_quotes_runtime($mqr);
854 $info['i']=count($this->images)+1;
855 $this->images[$file]=$info;
858 $info=$this->images[$file];
859 //Automatic width and height calculation if needed
860 if ($w==0 and $h==0) {
861 //Put image at 72 dpi
862 $w=$info['w']/$this->k;
863 $h=$info['h']/$this->k;
866 $w=$h*$info['w']/$info['h'];
868 $h=$w*$info['h']/$info['w'];
869 $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']));
871 $this->Link($x,$y,$w,$h,$link);
875 //Line feed; default value is last cell height
876 $this->x=$this->lMargin;
878 $this->y+=$this->lasth;
893 $this->x=$this->w+$x;
902 //Set y position and reset x
903 $this->x=$this->lMargin;
907 $this->y=$this->h+$y;
910 function SetXY($x,$y) {
911 //Set x and y positions
916 function Output($name='',$dest='') {
917 //Output PDF to some destination
918 global $HTTP_SERVER_VARS;
920 //Finish document if necessary
923 //Normalize parameters
925 $dest = $dest ? 'D' : 'F';
926 $dest = strtoupper($dest);
937 //Send to standard output
938 if (isset($HTTP_SERVER_VARS['SERVER_NAME'])) {
939 //We send to a browser
940 Header('Content-Type: application/pdf');
942 $this->Error('Some data has already been output to browser, can\'t send PDF file');
943 Header('Content-Length: '.strlen($this->buffer));
944 Header('Content-disposition: inline; filename='.$name);
950 if (isset($HTTP_SERVER_VARS['HTTP_USER_AGENT']) and strpos($HTTP_SERVER_VARS['HTTP_USER_AGENT'],'MSIE'))
951 Header('Content-Type: application/force-download');
953 Header('Content-Type: application/octet-stream');
955 $this->Error('Some data has already been output to browser, can\'t send PDF file');
956 Header('Content-Length: '.strlen($this->buffer));
957 Header('Content-disposition: attachment; filename='.$name);
962 $f=fopen($name,'wb');
964 $this->Error('Unable to create output file: '.$name);
965 fwrite($f,$this->buffer,strlen($this->buffer));
970 return $this->buffer;
972 $this->Error('Incorrect output destination: '.$dest);
977 /*******************************************************************************
979 * Protected methods *
981 *******************************************************************************/
982 function _dochecks() {
983 //Check for locale-related bug. we must have "." as comma seperator
984 $attempts = array("C","en","en_us","English");
986 while (1.1 == 1 and $i++ < count($attempts)) {
987 setlocale(LC_NUMERIC,$attempts[$i]);
990 $this->Error('1.1 == 1: Wrong locale with comma as dot. German? Don\'t alter the locale before including class file');
992 //Check for decimal separator
993 if (sprintf('%.1f',1.0)!='1.0')
994 setlocale(LC_NUMERIC,'C');
997 function _begindoc() {
1000 $this->_out('%PDF-1.3');
1003 function _putpages() {
1005 if (!empty($this->AliasNbPages)) {
1006 //Replace number of pages
1007 for($n=1;$n<=$nb;$n++)
1008 $this->pages[$n]=str_replace($this->AliasNbPages,$nb,$this->pages[$n]);
1010 if ($this->DefOrientation=='P') {
1019 $filter=($this->compress) ? '/Filter /FlateDecode ' : '';
1020 for($n=1;$n<=$nb;$n++) {
1023 $this->_out('<</Type /Page');
1024 $this->_out('/Parent 1 0 R');
1025 if (isset($this->OrientationChanges[$n]))
1026 $this->_out(sprintf('/MediaBox [0 0 %.2f %.2f]',$hPt,$wPt));
1027 $this->_out('/Resources 2 0 R');
1028 if (isset($this->PageLinks[$n])) {
1030 $annots='/Annots [';
1031 foreach($this->PageLinks[$n] as $pl) {
1032 $rect=sprintf('%.2f %.2f %.2f %.2f',$pl[0],$pl[1],$pl[0]+$pl[2],$pl[1]-$pl[3]);
1033 $annots.='<</Type /Annot /Subtype /Link /Rect ['.$rect.'] /Border [0 0 0] ';
1034 if (is_string($pl[4]))
1035 $annots.='/A <</S /URI /URI '.$this->_textstring($pl[4]).'>>>>';
1038 $l=$this->links[$pl[4]];
1039 $h=isset($this->OrientationChanges[$l[0]]) ? $wPt : $hPt;
1040 $annots.=sprintf('/Dest [%d 0 R /XYZ 0 %.2f null]>>',1+2*$l[0],$h-$l[1]*$this->k);
1043 $this->_out($annots.']');
1045 $this->_out('/Contents '.($this->n+1).' 0 R>>');
1046 $this->_out('endobj');
1048 $p=($this->compress) ? gzcompress($this->pages[$n]) : $this->pages[$n];
1050 $this->_out('<<'.$filter.'/Length '.strlen($p).'>>');
1051 $this->_putstream($p);
1052 $this->_out('endobj');
1055 $this->offsets[1]=strlen($this->buffer);
1056 $this->_out('1 0 obj');
1057 $this->_out('<</Type /Pages');
1059 for($i=0;$i<$nb;$i++)
1060 $kids.=(3+2*$i).' 0 R ';
1061 $this->_out($kids.']');
1062 $this->_out('/Count '.$nb);
1063 $this->_out(sprintf('/MediaBox [0 0 %.2f %.2f]',$wPt,$hPt));
1065 $this->_out('endobj');
1068 function _putfonts() {
1070 foreach($this->diffs as $diff) {
1073 $this->_out('<</Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences ['.$diff.']>>');
1074 $this->_out('endobj');
1076 $mqr=get_magic_quotes_runtime();
1077 set_magic_quotes_runtime(0);
1078 foreach($this->FontFiles as $file=>$info) {
1079 //Font file embedding
1081 $this->FontFiles[$file]['n']=$this->n;
1082 if (defined('FPDF_FONTPATH'))
1083 $file=FPDF_FONTPATH.$file;
1084 $size=filesize($file);
1086 $this->Error('Font file not found');
1087 $this->_out('<</Length '.$size);
1088 if (substr($file,-2)=='.z')
1089 $this->_out('/Filter /FlateDecode');
1090 $this->_out('/Length1 '.$info['length1']);
1091 if (isset($info['length2']))
1092 $this->_out('/Length2 '.$info['length2'].' /Length3 0');
1094 $f=fopen($file,'rb');
1095 $this->_putstream(fread($f,$size));
1097 $this->_out('endobj');
1099 set_magic_quotes_runtime($mqr);
1100 foreach($this->fonts as $k=>$font) {
1102 $this->fonts[$k]['n']=$this->n+1;
1103 $type=$font['type'];
1104 $name=$font['name'];
1105 if ($type=='core') {
1108 $this->_out('<</Type /Font');
1109 $this->_out('/BaseFont /'.$name);
1110 $this->_out('/Subtype /Type1');
1111 if ($name!='Symbol' and $name!='ZapfDingbats')
1112 $this->_out('/Encoding /WinAnsiEncoding');
1114 $this->_out('endobj');
1116 elseif ($type=='Type1' or $type=='TrueType') {
1117 //Additional Type1 or TrueType font
1119 $this->_out('<</Type /Font');
1120 $this->_out('/BaseFont /'.$name);
1121 $this->_out('/Subtype /'.$type);
1122 $this->_out('/FirstChar 32 /LastChar 255');
1123 $this->_out('/Widths '.($this->n+1).' 0 R');
1124 $this->_out('/FontDescriptor '.($this->n+2).' 0 R');
1126 if (isset($font['diff']))
1127 $this->_out('/Encoding '.($nf+$font['diff']).' 0 R');
1129 $this->_out('/Encoding /WinAnsiEncoding');
1132 $this->_out('endobj');
1137 for($i=32;$i<=255;$i++)
1138 $s.=$cw[chr($i)].' ';
1139 $this->_out($s.']');
1140 $this->_out('endobj');
1143 $s='<</Type /FontDescriptor /FontName /'.$name;
1144 foreach($font['desc'] as $k=>$v)
1146 $file=$font['file'];
1148 $s.=' /FontFile'.($type=='Type1' ? '' : '2').' '.$this->FontFiles[$file]['n'].' 0 R';
1149 $this->_out($s.'>>');
1150 $this->_out('endobj');
1154 //Allow for additional types
1155 $mtd='_put'.strtolower($type);
1156 if (!method_exists($this,$mtd))
1157 $this->Error('Unsupported font type: '.$type);
1163 function _putimages() {
1164 $filter=($this->compress) ? '/Filter /FlateDecode ' : '';
1165 reset($this->images);
1166 while(list($file,$info)=each($this->images)) {
1168 $this->images[$file]['n']=$this->n;
1169 $this->_out('<</Type /XObject');
1170 $this->_out('/Subtype /Image');
1171 $this->_out('/Width '.$info['w']);
1172 $this->_out('/Height '.$info['h']);
1173 if ($info['cs']=='Indexed')
1174 $this->_out('/ColorSpace [/Indexed /DeviceRGB '.(strlen($info['pal'])/3-1).' '.($this->n+1).' 0 R]');
1177 $this->_out('/ColorSpace /'.$info['cs']);
1178 if ($info['cs']=='DeviceCMYK')
1179 $this->_out('/Decode [1 0 1 0 1 0 1 0]');
1181 $this->_out('/BitsPerComponent '.$info['bpc']);
1182 $this->_out('/Filter /'.$info['f']);
1183 if (isset($info['parms']))
1184 $this->_out($info['parms']);
1185 if (isset($info['trns']) and is_array($info['trns'])) {
1187 for($i=0;$i<count($info['trns']);$i++)
1188 $trns.=$info['trns'][$i].' '.$info['trns'][$i].' ';
1189 $this->_out('/Mask ['.$trns.']');
1191 $this->_out('/Length '.strlen($info['data']).'>>');
1192 $this->_putstream($info['data']);
1193 unset($this->images[$file]['data']);
1194 $this->_out('endobj');
1196 if ($info['cs']=='Indexed') {
1198 $pal=($this->compress) ? gzcompress($info['pal']) : $info['pal'];
1199 $this->_out('<<'.$filter.'/Length '.strlen($pal).'>>');
1200 $this->_putstream($pal);
1201 $this->_out('endobj');
1206 function _putresources() {
1208 $this->_putimages();
1209 //Resource dictionary
1210 $this->offsets[2]=strlen($this->buffer);
1211 $this->_out('2 0 obj');
1212 $this->_out('<</ProcSet [/PDF /Text /ImageB /ImageC /ImageI]');
1213 $this->_out('/Font <<');
1214 foreach($this->fonts as $font)
1215 $this->_out('/F'.$font['i'].' '.$font['n'].' 0 R');
1217 if (count($this->images)) {
1218 $this->_out('/XObject <<');
1219 foreach($this->images as $image)
1220 $this->_out('/I'.$image['i'].' '.$image['n'].' 0 R');
1224 $this->_out('endobj');
1227 function _putinfo() {
1228 if (!empty($this->producer))
1229 $this->_out('/Producer '.$this->_textstring($this->producer));
1230 if (!empty($this->title))
1231 $this->_out('/Title '.$this->_textstring($this->title));
1232 if (!empty($this->subject))
1233 $this->_out('/Subject '.$this->_textstring($this->subject));
1234 if (!empty($this->author))
1235 $this->_out('/Author '.$this->_textstring($this->author));
1236 if (!empty($this->keywords))
1237 $this->_out('/Keywords '.$this->_textstring($this->keywords));
1238 if (!empty($this->creator))
1239 $this->_out('/Creator '.$this->_textstring($this->creator));
1240 $this->_out('/CreationDate '.$this->_textstring('D:'.date('YmdHis')));
1243 function _putcatalog() {
1244 $this->_out('/Type /Catalog');
1245 $this->_out('/Pages 1 0 R');
1246 if ($this->ZoomMode=='fullpage')
1247 $this->_out('/OpenAction [3 0 R /Fit]');
1248 elseif ($this->ZoomMode=='fullwidth')
1249 $this->_out('/OpenAction [3 0 R /FitH null]');
1250 elseif ($this->ZoomMode=='real')
1251 $this->_out('/OpenAction [3 0 R /XYZ null null 1]');
1252 elseif (!is_string($this->ZoomMode))
1253 $this->_out('/OpenAction [3 0 R /XYZ null null '.($this->ZoomMode/100).']');
1254 if ($this->LayoutMode=='single')
1255 $this->_out('/PageLayout /SinglePage');
1256 elseif ($this->LayoutMode=='continuous')
1257 $this->_out('/PageLayout /OneColumn');
1258 elseif ($this->LayoutMode=='two')
1259 $this->_out('/PageLayout /TwoColumnLeft');
1262 function _puttrailer() {
1263 $this->_out('/Size '.($this->n+1));
1264 $this->_out('/Root '.$this->n.' 0 R');
1265 $this->_out('/Info '.($this->n-1).' 0 R');
1268 function _enddoc() {
1270 $this->_putresources();
1276 $this->_out('endobj');
1280 $this->_putcatalog();
1282 $this->_out('endobj');
1284 $o=strlen($this->buffer);
1285 $this->_out('xref');
1286 $this->_out('0 '.($this->n+1));
1287 $this->_out('0000000000 65535 f ');
1288 for($i=1;$i<=$this->n;$i++)
1289 $this->_out(sprintf('%010d 00000 n ',$this->offsets[$i]));
1291 $this->_out('trailer');
1293 $this->_puttrailer();
1295 $this->_out('startxref');
1297 $this->_out('%%EOF');
1301 function _beginpage($orientation) {
1303 $this->pages[$this->page]='';
1305 $this->x=$this->lMargin;
1306 $this->y=$this->tMargin;
1307 $this->FontFamily='';
1310 $orientation=$this->DefOrientation;
1313 $orientation=strtoupper($orientation{0});
1314 if ($orientation!=$this->DefOrientation)
1315 $this->OrientationChanges[$this->page]=true;
1317 if ($orientation!=$this->CurOrientation) {
1318 //Change orientation
1319 if ($orientation=='P') {
1320 $this->wPt=$this->fwPt;
1321 $this->hPt=$this->fhPt;
1327 $this->wPt=$this->fhPt;
1328 $this->hPt=$this->fwPt;
1332 $this->PageBreakTrigger=$this->h-$this->bMargin;
1333 $this->CurOrientation=$orientation;
1337 function _endpage() {
1338 //End of page contents
1342 function _newobj() {
1343 //Begin a new object
1345 $this->offsets[$this->n]=strlen($this->buffer);
1346 $this->_out($this->n.' 0 obj');
1349 function _dounderline($x,$y,$txt) {
1351 $up=$this->CurrentFont['up'];
1352 $ut=$this->CurrentFont['ut'];
1353 $w=$this->GetStringWidth($txt)+$this->ws*substr_count($txt,' ');
1354 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);
1357 function _parsegif ($file)
1359 //Function by Jérôme Fenal
1360 //GIF class in pure PHP from Yamasoft (http://www.yamasoft.com/php-gif.zip)
1361 require_once 'lib/gif.php';
1367 if (!$gif->loadFile($file, 0))
1368 $this->Error("GIF parser: unable to open file $file");
1370 if ($gif->m_img->m_gih->m_bLocalClr) {
1371 $nColors = $gif->m_img->m_gih->m_nTableSize;
1372 $pal = $gif->m_img->m_gih->m_colorTable->toString();
1373 if ($bgColor != -1) {
1374 $bgColor = $this->m_img->m_gih->m_colorTable->colorIndex($bgColor);
1376 $colspace='Indexed';
1377 } elseif ($gif->m_gfh->m_bGlobalClr) {
1378 $nColors = $gif->m_gfh->m_nTableSize;
1379 $pal = $gif->m_gfh->m_colorTable->toString();
1380 if ($bgColor != -1) {
1381 $bgColor = $gif->m_gfh->m_colorTable->colorIndex($bgColor);
1383 $colspace='Indexed';
1387 $colspace='DeviceGray';
1392 if ($gif->m_img->m_bTrans && ($nColors > 0)) {
1393 $trns=array($gif->m_img->m_nTrans);
1396 $data=$gif->m_img->m_data;
1397 $w=$gif->m_gfh->m_nWidth;
1398 $h=$gif->m_gfh->m_nHeight;
1400 if ($colspace=='Indexed' and empty($pal))
1401 $this->Error('Missing palette in '.$file);
1403 if ($this->compress) {
1404 $data = gzcompress($data);
1405 return array( 'w'=>$w, 'h'=>$h, 'cs'=>$colspace, 'bpc'=>8, 'f'=>'FlateDecode', 'pal'=>$pal, 'trns'=>$trns, 'data'=>$data);
1407 return array( 'w'=>$w, 'h'=>$h, 'cs'=>$colspace, 'bpc'=>8, 'pal'=>$pal, 'trns'=>$trns, 'data'=>$data);
1411 function _parsejpg($file) {
1412 //Extract info from a JPEG file
1413 $a = GetImageSize($file);
1415 $this->Error('Missing or incorrect image file: '.$file);
1417 $this->Error('Not a JPEG file: '.$file);
1418 if (!isset($a['channels']) or $a['channels']==3)
1419 $colspace='DeviceRGB';
1420 elseif ($a['channels']==4)
1421 $colspace='DeviceCMYK';
1423 $colspace='DeviceGray';
1424 $bpc=isset($a['bits']) ? $a['bits'] : 8;
1426 $f=fopen($file,'rb');
1429 $data.=fread($f,4096);
1431 return array('w'=>$a[0],'h'=>$a[1],'cs'=>$colspace,'bpc'=>$bpc,'f'=>'DCTDecode','data'=>$data);
1434 function _parsepng($file) {
1435 //Extract info from a PNG file
1436 $f=fopen($file,'rb');
1438 $this->Error('Can\'t open image file: '.$file);
1440 if (fread($f,8)!=chr(137).'PNG'.chr(13).chr(10).chr(26).chr(10))
1441 $this->Error('Not a PNG file: '.$file);
1444 if (fread($f,4)!='IHDR')
1445 $this->Error('Incorrect PNG file: '.$file);
1446 $w=$this->_freadint($f);
1447 $h=$this->_freadint($f);
1448 $bpc=ord(fread($f,1));
1450 $this->Error('16-bit depth not supported: '.$file);
1451 $ct=ord(fread($f,1));
1453 $colspace='DeviceGray';
1455 $colspace='DeviceRGB';
1457 $colspace='Indexed';
1459 $this->Error('Alpha channel not supported: '.$file);
1460 if (ord(fread($f,1))!=0)
1461 $this->Error('Unknown compression method: '.$file);
1462 if (ord(fread($f,1))!=0)
1463 $this->Error('Unknown filter method: '.$file);
1464 if (ord(fread($f,1))!=0)
1465 $this->Error('Interlacing not supported: '.$file);
1467 $parms='/DecodeParms <</Predictor 15 /Colors '.($ct==2 ? 3 : 1).' /BitsPerComponent '.$bpc.' /Columns '.$w.'>>';
1468 //Scan chunks looking for palette, transparency and image data
1473 $n=$this->_freadint($f);
1475 if ($type=='PLTE') {
1480 elseif ($type=='tRNS') {
1481 //Read transparency info
1484 $trns=array(ord(substr($t,1,1)));
1486 $trns=array(ord(substr($t,1,1)),ord(substr($t,3,1)),ord(substr($t,5,1)));
1488 $pos=strpos($t,chr(0));
1494 elseif ($type=='IDAT') {
1495 //Read image data block
1496 $data.=fread($f,$n);
1499 elseif ($type=='IEND')
1504 if ($colspace=='Indexed' and empty($pal))
1505 $this->Error('Missing palette in '.$file);
1507 return array('w'=>$w,'h'=>$h,'cs'=>$colspace,'bpc'=>$bpc,'f'=>'FlateDecode','parms'=>$parms,'pal'=>$pal,'trns'=>$trns,'data'=>$data);
1510 function _freadint($f) {
1511 //Read a 4-byte integer from file
1512 $i=ord(fread($f,1))<<24;
1513 $i+=ord(fread($f,1))<<16;
1514 $i+=ord(fread($f,1))<<8;
1515 $i+=ord(fread($f,1));
1519 function _textstring($s) {
1520 //Format a text string
1521 return '('.$this->_escape($s).')';
1524 function _escape($s) {
1525 //Add \ before \, ( and )
1526 return str_replace(')','\\)',str_replace('(','\\(',str_replace('\\','\\\\',$s)));
1529 function _putstream($s) {
1530 $this->_out('stream');
1532 $this->_out('endstream');
1536 //Add a line to the document
1537 if ($this->state == 2)
1538 $this->pages[$this->page].=$s."\n";
1540 $this->buffer.=$s."\n";
1546 //Handle special IE contype request
1547 if (isset($HTTP_SERVER_VARS['HTTP_USER_AGENT']) and
1548 $HTTP_SERVER_VARS['HTTP_USER_AGENT']=='contype')
1550 Header('Content-Type: application/pdf');
1557 // c-basic-offset: 4
1558 // c-hanging-comment-ender-p: nil
1559 // indent-tabs-mode: nil