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