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