Fix #8052 - fixing pdf
[pear] / FpdfAlt.php
diff --git a/FpdfAlt.php b/FpdfAlt.php
new file mode 100644 (file)
index 0000000..5886633
--- /dev/null
@@ -0,0 +1,1592 @@
+<?php\r
+/****************************************************************************\r
+* Software: FPDF                                                            *\r
+* Version:  1.51                                                            *\r
+* Date:     2002/08/03                                                      *\r
+* Author:   Olivier PLATHEY                                                 *\r
+* License:  Freeware                                                        *\r
+*                                                                           *\r
+* You may use and modify this software as you wish.                         *\r
+****************************************************************************/\r
+define('FPDF_VERSION','1.51');\r
+\r
+class FpdfAlt\r
+{\r
+//Private properties\r
+var $page;               //current page number\r
+var $n;                  //current object number\r
+var $offsets;            //array of object offsets\r
+var $buffer;             //buffer holding in-memory PDF\r
+var $pages;              //array containing pages\r
+var $state;              //current document state\r
+var $compress;           //compression flag\r
+var $DefOrientation;     //default orientation\r
+var $CurOrientation;     //current orientation\r
+var $OrientationChanges; //array indicating orientation changes\r
+var $fwPt,$fhPt;         //dimensions of page format in points\r
+var $fw,$fh;             //dimensions of page format in user unit\r
+var $wPt,$hPt;           //current dimensions of page in points\r
+var $k;                  //scale factor (number of points in user unit)\r
+var $w,$h;               //current dimensions of page in user unit\r
+var $lMargin;            //left margin\r
+var $tMargin;            //top margin\r
+var $rMargin;            //right margin\r
+var $bMargin;            //page break margin\r
+var $cMargin;            //cell margin\r
+var $x,$y;               //current position in user unit for cell positionning\r
+var $lasth;              //height of last cell printed\r
+var $LineWidth;          //line width in user unit\r
+var $CoreFonts;          //array of standard font names\r
+var $fonts;              //array of used fonts\r
+var $FontFiles;          //array of font files\r
+var $diffs;              //array of encoding differences\r
+var $images;             //array of used images\r
+var $PageLinks;          //array of links in pages\r
+var $links;              //array of internal links\r
+var $FontFamily;         //current font family\r
+var $FontStyle;          //current font style\r
+var $underline;          //underlining flag\r
+var $CurrentFont;        //current font info\r
+var $FontSizePt;         //current font size in points\r
+var $FontSize;           //current font size in user unit\r
+var $DrawColor;          //commands for drawing color\r
+var $FillColor;          //commands for filling color\r
+var $TextColor;          //commands for text color\r
+var $ColorFlag;          //indicates whether fill and text colors are different\r
+var $ws;                 //word spacing\r
+var $AutoPageBreak;      //automatic page breaking\r
+var $PageBreakTrigger;   //threshold used to trigger page breaks\r
+var $InFooter;           //flag set when processing footer\r
+var $ZoomMode;           //zoom display mode\r
+var $LayoutMode;         //layout display mode\r
+var $title;              //title\r
+var $subject;            //subject\r
+var $author;             //author\r
+var $keywords;           //keywords\r
+var $creator;            //creator\r
+var $AliasNbPages;       //alias for total number of pages\r
+\r
+var $CurPageFormat;\r
+var $PDFVersion;\r
+\r
+/****************************************************************************\r
+*                                                                           *\r
+*                              Public methods                               *\r
+*                                                                           *\r
+****************************************************************************/\r
+function __construct($orientation='P',$unit='mm',$format='A4')\r
+{\r
+       //Check for PHP locale-related bug\r
+       if(1.1==1)\r
+               $this->Error('Don\'t alter the locale before including class file');\r
+       //Initialization of properties\r
+       $this->page=0;\r
+       $this->n=2;\r
+       $this->buffer='';\r
+       $this->pages=array();\r
+       $this->OrientationChanges=array();\r
+       $this->state=0;\r
+       $this->fonts=array();\r
+       $this->FontFiles=array();\r
+       $this->diffs=array();\r
+       $this->images=array();\r
+       $this->links=array();\r
+       $this->InFooter=false;\r
+       $this->FontFamily='';\r
+       $this->FontStyle='';\r
+       $this->FontSizePt=12;\r
+       $this->underline=false;\r
+       $this->DrawColor='0 G';\r
+       $this->FillColor='0 g';\r
+       $this->TextColor='0 g';\r
+       $this->ColorFlag=false;\r
+       $this->ws=0;\r
+       //Standard fonts\r
+       $this->CoreFonts['courier']='Courier';\r
+       $this->CoreFonts['courierB']='Courier-Bold';\r
+       $this->CoreFonts['courierI']='Courier-Oblique';\r
+       $this->CoreFonts['courierBI']='Courier-BoldOblique';\r
+       $this->CoreFonts['helvetica']='Helvetica';\r
+       $this->CoreFonts['helveticaB']='Helvetica-Bold';\r
+       $this->CoreFonts['helveticaI']='Helvetica-Oblique';\r
+       $this->CoreFonts['helveticaBI']='Helvetica-BoldOblique';\r
+       $this->CoreFonts['times']='Times-Roman';\r
+       $this->CoreFonts['timesB']='Times-Bold';\r
+       $this->CoreFonts['timesI']='Times-Italic';\r
+       $this->CoreFonts['timesBI']='Times-BoldItalic';\r
+       $this->CoreFonts['symbol']='Symbol';\r
+       $this->CoreFonts['zapfdingbats']='ZapfDingbats';\r
+       //Scale factor\r
+       if($unit=='pt')\r
+               $this->k=1;\r
+       elseif($unit=='mm')\r
+               $this->k=72/25.4;\r
+       elseif($unit=='cm')\r
+               $this->k=72/2.54;\r
+       elseif($unit=='in')\r
+               $this->k=72;\r
+       else\r
+               $this->Error('Incorrect unit: '.$unit);\r
+       //Page format\r
+       if(is_string($format))\r
+       {\r
+               $format=strtolower($format);\r
+               if($format=='a3')\r
+                       $format=array(841.89,1190.55);\r
+               elseif($format=='a4')\r
+                       $format=array(595.28,841.89);\r
+               elseif($format=='a5')\r
+                       $format=array(420.94,595.28);\r
+               elseif($format=='letter')\r
+                       $format=array(612,792);\r
+               elseif($format=='legal')\r
+                       $format=array(612,1008);\r
+               else\r
+                       $this->Error('Unknown page format: '.$format);\r
+               $this->fwPt=$format[0];\r
+               $this->fhPt=$format[1];\r
+       }\r
+       else\r
+       {\r
+               $this->fwPt=$format[0]*$this->k;\r
+               $this->fhPt=$format[1]*$this->k;\r
+       }\r
+    $this->CurPageFormat = $format; //FC with 1.6...\r
+    \r
+    \r
+    \r
+       $this->fw=$this->fwPt/$this->k;\r
+       $this->fh=$this->fhPt/$this->k;\r
+       //Page orientation\r
+       $orientation=strtolower($orientation);\r
+       if($orientation=='p' or $orientation=='portrait')\r
+       {\r
+               $this->DefOrientation='P';\r
+               $this->wPt=$this->fwPt;\r
+               $this->hPt=$this->fhPt;\r
+       }\r
+       elseif($orientation=='l' or $orientation=='landscape')\r
+       {\r
+               $this->DefOrientation='L';\r
+               $this->wPt=$this->fhPt;\r
+               $this->hPt=$this->fwPt;\r
+       }\r
+       else\r
+               $this->Error('Incorrect orientation: '.$orientation);\r
+       $this->CurOrientation=$this->DefOrientation;\r
+       $this->w=$this->wPt/$this->k;\r
+       $this->h=$this->hPt/$this->k;\r
+       //Page margins (1 cm)\r
+       $margin=28.35/$this->k;\r
+       $this->SetMargins($margin,$margin);\r
+       //Interior cell margin (1 mm)\r
+       $this->cMargin=$margin/10;\r
+       //Line width (0.2 mm)\r
+       $this->LineWidth=.567/$this->k;\r
+       //Automatic page break\r
+       $this->SetAutoPageBreak(true,2*$margin);\r
+       //Full width display mode\r
+       $this->SetDisplayMode('fullwidth');\r
+       //Compression\r
+       $this->SetCompression(true);\r
+        \r
+        $this->PDFVersion = '1.4';\r
+}\r
+\r
+function SetMargins($left,$top,$right=-1)\r
+{\r
+       //Set left, top and right margins\r
+       $this->lMargin=$left;\r
+       $this->tMargin=$top;\r
+       if($right==-1)\r
+               $right=$left;\r
+       $this->rMargin=$right;\r
+}\r
+\r
+function SetLeftMargin($margin)\r
+{\r
+       //Set left margin\r
+       $this->lMargin=$margin;\r
+       if($this->page>0 and $this->x<$margin)\r
+               $this->x=$margin;\r
+}\r
+\r
+function SetTopMargin($margin)\r
+{\r
+       //Set top margin\r
+       $this->tMargin=$margin;\r
+}\r
+\r
+function SetRightMargin($margin)\r
+{\r
+       //Set right margin\r
+       $this->rMargin=$margin;\r
+}\r
+\r
+function SetAutoPageBreak($auto,$margin=0)\r
+{\r
+       //Set auto page break mode and triggering margin\r
+       $this->AutoPageBreak=$auto;\r
+       $this->bMargin=$margin;\r
+       $this->PageBreakTrigger=$this->h-$margin;\r
+}\r
+\r
+function SetDisplayMode($zoom,$layout='continuous')\r
+{\r
+       //Set display mode in viewer\r
+       if($zoom=='fullpage' or $zoom=='fullwidth' or $zoom=='real' or $zoom=='default' or !is_string($zoom))\r
+               $this->ZoomMode=$zoom;\r
+       elseif($zoom=='zoom')\r
+               $this->ZoomMode=$layout;\r
+       else\r
+               $this->Error('Incorrect zoom display mode: '.$zoom);\r
+       if($layout=='single' or $layout=='continuous' or $layout=='two' or $layout=='default')\r
+               $this->LayoutMode=$layout;\r
+       elseif($zoom!='zoom')\r
+               $this->Error('Incorrect layout display mode: '.$layout);\r
+}\r
+\r
+function SetCompression($compress)\r
+{\r
+       //Set page compression\r
+       if(function_exists('gzcompress'))\r
+               $this->compress=$compress;\r
+       else\r
+               $this->compress=false;\r
+}\r
+\r
+function SetTitle($title)\r
+{\r
+       //Title of document\r
+       $this->title=$title;\r
+}\r
+\r
+function SetSubject($subject)\r
+{\r
+       //Subject of document\r
+       $this->subject=$subject;\r
+}\r
+\r
+function SetAuthor($author)\r
+{\r
+       //Author of document\r
+       $this->author=$author;\r
+}\r
+\r
+function SetKeywords($keywords)\r
+{\r
+       //Keywords of document\r
+       $this->keywords=$keywords;\r
+}\r
+\r
+function SetCreator($creator)\r
+{\r
+       //Creator of document\r
+       $this->creator=$creator;\r
+}\r
+\r
+function AliasNbPages($alias='{nb}')\r
+{\r
+       //Define an alias for total number of pages\r
+       $this->AliasNbPages=$alias;\r
+}\r
+\r
+function Error($msg)\r
+{\r
+       //Fatal error\r
+       die('<B>FPDF error: </B>'.$msg);\r
+}\r
+\r
+function Open()\r
+{\r
+       //Begin document\r
+       $this->_begindoc();\r
+}\r
+\r
+function Close()\r
+{\r
+       //Terminate document\r
+       if($this->page==0)\r
+               $this->AddPage();\r
+       //Page footer\r
+       $this->InFooter=true;\r
+       $this->Footer();\r
+       $this->InFooter=false;\r
+       //Close page\r
+       $this->_endpage();\r
+       //Close document\r
+       $this->_enddoc();\r
+}\r
+\r
+function AddPage($orientation='')\r
+{\r
+       //Start a new page\r
+       $family=$this->FontFamily;\r
+       $style=$this->FontStyle.($this->underline ? 'U' : '');\r
+       $size=$this->FontSizePt;\r
+       $lw=$this->LineWidth;\r
+       $dc=$this->DrawColor;\r
+       $fc=$this->FillColor;\r
+       $tc=$this->TextColor;\r
+       $cf=$this->ColorFlag;\r
+       if($this->page>0)\r
+       {\r
+               //Page footer\r
+               $this->InFooter=true;\r
+               $this->Footer();\r
+               $this->InFooter=false;\r
+               //Close page\r
+               $this->_endpage();\r
+       }\r
+       //Start new page\r
+       $this->_beginpage($orientation);\r
+       //Set line cap style to square\r
+       $this->_out('2 J');\r
+       //Set line width\r
+       $this->LineWidth=$lw;\r
+       $this->_out(sprintf('%.2f w',$lw*$this->k));\r
+       //Set font\r
+       if($family)\r
+               $this->SetFont($family,$style,$size);\r
+       //Set colors\r
+       $this->DrawColor=$dc;\r
+       if($dc!='0 G')\r
+               $this->_out($dc);\r
+       $this->FillColor=$fc;\r
+       if($fc!='0 g')\r
+               $this->_out($fc);\r
+       $this->TextColor=$tc;\r
+       $this->ColorFlag=$cf;\r
+       //Page header\r
+       $this->Header();\r
+       //Restore line width\r
+       if($this->LineWidth!=$lw)\r
+       {\r
+               $this->LineWidth=$lw;\r
+               $this->_out(sprintf('%.2f w',$lw*$this->k));\r
+       }\r
+       //Restore font\r
+       if($family)\r
+               $this->SetFont($family,$style,$size);\r
+       //Restore colors\r
+       if($this->DrawColor!=$dc)\r
+       {\r
+               $this->DrawColor=$dc;\r
+               $this->_out($dc);\r
+       }\r
+       if($this->FillColor!=$fc)\r
+       {\r
+               $this->FillColor=$fc;\r
+               $this->_out($fc);\r
+       }\r
+       $this->TextColor=$tc;\r
+       $this->ColorFlag=$cf;\r
+}\r
+\r
+function Header()\r
+{\r
+       //To be implemented in your own inherited class\r
+}\r
+\r
+function Footer()\r
+{\r
+       //To be implemented in your own inherited class\r
+}\r
+\r
+function PageNo()\r
+{\r
+       //Get current page number\r
+       return $this->page;\r
+}\r
+\r
+function SetDrawColor($r,$g=-1,$b=-1)\r
+{\r
+       //Set color for all stroking operations\r
+       if(($r==0 and $g==0 and $b==0) or $g==-1)\r
+               $this->DrawColor=sprintf('%.3f G',$r/255);\r
+       else\r
+               $this->DrawColor=sprintf('%.3f %.3f %.3f RG',$r/255,$g/255,$b/255);\r
+       if($this->page>0)\r
+               $this->_out($this->DrawColor);\r
+}\r
+\r
+function SetFillColor($r,$g=-1,$b=-1)\r
+{\r
+       //Set color for all filling operations\r
+       if(($r==0 and $g==0 and $b==0) or $g==-1)\r
+               $this->FillColor=sprintf('%.3f g',$r/255);\r
+       else\r
+               $this->FillColor=sprintf('%.3f %.3f %.3f rg',$r/255,$g/255,$b/255);\r
+       $this->ColorFlag=($this->FillColor!=$this->TextColor);\r
+       if($this->page>0)\r
+               $this->_out($this->FillColor);\r
+}\r
+\r
+function SetTextColor($r,$g=-1,$b=-1)\r
+{\r
+       //Set color for text\r
+       if(($r==0 and $g==0 and $b==0) or $g==-1)\r
+               $this->TextColor=sprintf('%.3f g',$r/255);\r
+       else\r
+               $this->TextColor=sprintf('%.3f %.3f %.3f rg',$r/255,$g/255,$b/255);\r
+       $this->ColorFlag=($this->FillColor!=$this->TextColor);\r
+}\r
+\r
+function GetStringWidth($s)\r
+{\r
+       //Get width of a string in the current font\r
+       $s=(string)$s;\r
+       $cw=&$this->CurrentFont['cw'];\r
+       $w=0;\r
+       $l=strlen($s);\r
+       for($i=0;$i<$l;$i++)\r
+               $w+=$cw[$s[$i]];\r
+       return $w*$this->FontSize/1000;\r
+}\r
+\r
+function SetLineWidth($width)\r
+{\r
+       //Set line width\r
+       $this->LineWidth=$width;\r
+       if($this->page>0)\r
+               $this->_out(sprintf('%.2f w',$width*$this->k));\r
+}\r
+\r
+function Line($x1,$y1,$x2,$y2)\r
+{\r
+       //Draw a line\r
+    if (!is_array($x1)) {\r
+       $this->_out(\r
+            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
+        return;\r
+    }\r
+    $type= 'm';$out='';\r
+    for ($i=0;$i<count($x1);$i+=2) {\r
+               $out .= sprintf(' %.2f %.2f %s',$x1[$i]*$this->k,($this->h-$x1[$i+1])*$this->k,$type);\r
+        $type = 'l';\r
+    }          \r
+    $this->_out(ltrim($out) . ' f');\r
+\r
+\r
+}\r
+\r
+function Rect($x,$y,$w,$h,$style='')\r
+{\r
+       //Draw a rectangle\r
+       if($style=='F')\r
+               $op='f';\r
+       elseif($style=='FD' or $style=='DF')\r
+               $op='B';\r
+       else\r
+               $op='S';\r
+       $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
+}\r
+\r
+function AddFont($family,$style='',$file='')\r
+{\r
+       //Add a TrueType or Type1 font\r
+       $family=strtolower($family);\r
+       if($family=='arial')\r
+               $family='helvetica';\r
+       $style=strtoupper($style);\r
+       if($style=='IB')\r
+               $style='BI';\r
+       if(isset($this->fonts[$family.$style]))\r
+               $this->Error('Font already added: '.$family.' '.$style);\r
+       if($file=='')\r
+               $file=str_replace(' ','',$family).strtolower($style).'.php';\r
+        \r
+    if (file_exists(dirname(__FILE__) .'/FpdfAlt/'.$file)) {\r
+        include 'FpdfAlt/'.$file;\r
+    }\r
+       if(!isset($name)) {\r
+               $this->Error('Could not include font definition file');\r
+    }\r
+       $i=count($this->fonts)+1;\r
+       $this->fonts[$family.$style]=array('i'=>$i,'type'=>$type,'name'=>$name,'desc'=>$desc,'up'=>$up,'ut'=>$ut,'cw'=>$cw,'enc'=>$enc,'file'=>$file);\r
+       if($diff)\r
+       {\r
+               //Search existing encodings\r
+               $d=0;\r
+               $nb=count($this->diffs);\r
+               for($i=1;$i<=$nb;$i++)\r
+                       if($this->diffs[$i]==$diff)\r
+                       {\r
+                               $d=$i;\r
+                               break;\r
+                       }\r
+               if($d==0)\r
+               {\r
+                       $d=$nb+1;\r
+                       $this->diffs[$d]=$diff;\r
+               }\r
+               $this->fonts[$family.$style]['diff']=$d;\r
+       }\r
+       if($file)\r
+       {\r
+               if($type=='TrueType')\r
+                       $this->FontFiles[$file]=array('length1'=>$originalsize);\r
+               else\r
+                       $this->FontFiles[$file]=array('length1'=>$size1,'length2'=>$size2);\r
+       }\r
+}\r
+\r
+function SetFont($family,$style='',$size=0)\r
+{\r
+       //Select a font; size given in points\r
+       global $fpdf_charwidths;\r
+\r
+       $family=strtolower($family);\r
+       if($family=='')\r
+               $family=$this->FontFamily;\r
+       if($family=='arial')\r
+               $family='helvetica';\r
+       elseif($family=='symbol' or $family=='zapfdingbats')\r
+               $style='';\r
+       $style=strtoupper($style);\r
+       if(is_int(strpos($style,'U')))\r
+       {\r
+               $this->underline=true;\r
+               $style=str_replace('U','',$style);\r
+       }\r
+       else\r
+               $this->underline=false;\r
+       if($style=='IB')\r
+               $style='BI';\r
+       if($size==0)\r
+               $size=$this->FontSizePt;\r
+       //Test if font is already selected\r
+       if($this->FontFamily==$family and $this->FontStyle==$style and $this->FontSizePt==$size)\r
+               return;\r
+       //Test if used for the first time\r
+       $fontkey=$family.$style;\r
+       if(!isset($this->fonts[$fontkey]))\r
+       {\r
+               //Check if one of the standard fonts\r
+               if(isset($this->CoreFonts[$fontkey]))\r
+               {\r
+                       if(!isset($fpdf_charwidths[$fontkey]))\r
+                       {\r
+                               //Load metric file\r
+                               $file=$family;\r
+                               if($family=='times' or $family=='helvetica')\r
+                                       $file.=strtolower($style);\r
+                               $file.='.php';\r
+                               if (file_exists(dirname(__FILE__) .'/FpdfAlt/'.$file)) {\r
+                    include 'FpdfAlt/'.$file;\r
+                }\r
+                               if(!isset($fpdf_charwidths[$fontkey]))\r
+                                       $this->Error('Could not include font metric file');\r
+                       }\r
+                       $i=count($this->fonts)+1;\r
+                       $this->fonts[$fontkey]=array('i'=>$i,'type'=>'core','name'=>$this->CoreFonts[$fontkey],'up'=>-100,'ut'=>50,'cw'=>$fpdf_charwidths[$fontkey]);\r
+               }\r
+               else\r
+                       $this->Error('Undefined font: '.$family.' '.$style);\r
+       }\r
+       //Select it\r
+       $this->FontFamily=$family;\r
+       $this->FontStyle=$style;\r
+       $this->FontSizePt=$size;\r
+       $this->FontSize=$size/$this->k;\r
+       $this->CurrentFont=&$this->fonts[$fontkey];\r
+       if($this->page>0)\r
+               $this->_out(sprintf('BT /F%d %.2f Tf ET',$this->CurrentFont['i'],$this->FontSizePt));\r
+}\r
+\r
+function SetFontSize($size)\r
+{\r
+       //Set font size in points\r
+       if($this->FontSizePt==$size)\r
+               return;\r
+       $this->FontSizePt=$size;\r
+       $this->FontSize=$size/$this->k;\r
+       if($this->page>0)\r
+               $this->_out(sprintf('BT /F%d %.2f Tf ET',$this->CurrentFont['i'],$this->FontSizePt));\r
+}\r
+\r
+function AddLink()\r
+{\r
+       //Create a new internal link\r
+       $n=count($this->links)+1;\r
+       $this->links[$n]=array(0,0);\r
+       return $n;\r
+}\r
+\r
+function SetLink($link,$y=0,$page=-1)\r
+{\r
+       //Set destination of internal link\r
+       if($y==-1)\r
+               $y=$this->y;\r
+       if($page==-1)\r
+               $page=$this->page;\r
+       $this->links[$link]=array($page,$y);\r
+}\r
+\r
+function Link($x,$y,$w,$h,$link)\r
+{\r
+       //Put a link on the page\r
+       $this->PageLinks[$this->page][]=array($x*$this->k,$this->hPt-$y*$this->k,$w*$this->k,$h*$this->k,$link);\r
+}\r
+\r
+function Text($x,$y,$txt)\r
+{\r
+       //Output a string\r
+       $txt=str_replace(')','\\)',str_replace('(','\\(',str_replace('\\','\\\\',$txt)));\r
+       $s=sprintf('BT %.2f %.2f Td (%s) Tj ET',$x*$this->k,($this->h-$y)*$this->k,$txt);\r
+       if($this->underline and $txt!='')\r
+               $s.=' '.$this->_dounderline($x,$y,$txt);\r
+       if($this->ColorFlag)\r
+               $s='q '.$this->TextColor.' '.$s.' Q';\r
+       $this->_out($s);\r
+}\r
+\r
+function AcceptPageBreak()\r
+{\r
+       //Accept automatic page break or not\r
+       return $this->AutoPageBreak;\r
+}\r
+\r
+function Cell($w,$h=0,$txt='',$border=0,$ln=0,$align='',$fill=0,$link='')\r
+{\r
+       //Output a cell\r
+       $k=$this->k;\r
+       if($this->y+$h>$this->PageBreakTrigger and !$this->InFooter and $this->AcceptPageBreak())\r
+       {\r
+               $x=$this->x;\r
+               $ws=$this->ws;\r
+               if($ws>0)\r
+               {\r
+                       $this->ws=0;\r
+                       $this->_out('0 Tw');\r
+               }\r
+               $this->AddPage($this->CurOrientation);\r
+               $this->x=$x;\r
+               if($ws>0)\r
+               {\r
+                       $this->ws=$ws;\r
+                       $this->_out(sprintf('%.3f Tw',$ws*$k));\r
+               }\r
+       }\r
+       if($w==0)\r
+               $w=$this->w-$this->rMargin-$this->x;\r
+       $s='';\r
+       if($fill==1 or $border==1)\r
+       {\r
+               if($fill==1)\r
+                       $op=($border==1) ? 'B' : 'f';\r
+               else\r
+                       $op='S';\r
+               $s=sprintf('%.2f %.2f %.2f %.2f re %s ',$this->x*$k,($this->h-$this->y)*$k,$w*$k,-$h*$k,$op);\r
+       }\r
+       if(is_string($border))\r
+       {\r
+               $x=$this->x;\r
+               $y=$this->y;\r
+               if(is_int(strpos($border,'L')))\r
+                       $s.=sprintf('%.2f %.2f m %.2f %.2f l S ',$x*$k,($this->h-$y)*$k,$x*$k,($this->h-($y+$h))*$k);\r
+               if(is_int(strpos($border,'T')))\r
+                       $s.=sprintf('%.2f %.2f m %.2f %.2f l S ',$x*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-$y)*$k);\r
+               if(is_int(strpos($border,'R')))\r
+                       $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
+               if(is_int(strpos($border,'B')))\r
+                       $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
+       }\r
+       if($txt!='')\r
+       {\r
+               if($align=='R')\r
+                       $dx=$w-$this->cMargin-$this->GetStringWidth($txt);\r
+               elseif($align=='C')\r
+                       $dx=($w-$this->GetStringWidth($txt))/2;\r
+               else\r
+                       $dx=$this->cMargin;\r
+               $txt=str_replace(')','\\)',str_replace('(','\\(',str_replace('\\','\\\\',$txt)));\r
+               if($this->ColorFlag)\r
+                       $s.='q '.$this->TextColor.' ';\r
+               $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
+               if($this->underline)\r
+                       $s.=' '.$this->_dounderline($this->x+$dx,$this->y+.5*$h+.3*$this->FontSize,$txt);\r
+               if($this->ColorFlag)\r
+                       $s.=' Q';\r
+               if($link)\r
+                       $this->Link($this->x+$dx,$this->y+.5*$h-.5*$this->FontSize,$this->GetStringWidth($txt),$this->FontSize,$link);\r
+       }\r
+       if($s)\r
+               $this->_out($s);\r
+       $this->lasth=$h;\r
+       if($ln>0)\r
+       {\r
+               //Go to next line\r
+               $this->y+=$h;\r
+               if($ln==1)\r
+                       $this->x=$this->lMargin;\r
+       }\r
+       else\r
+               $this->x+=$w;\r
+}\r
+\r
+function MultiCell($w,$h,$txt,$border=0,$align='J',$fill=0)\r
+{\r
+       //Output text with automatic or explicit line breaks\r
+       $cw=&$this->CurrentFont['cw'];\r
+       if($w==0)\r
+               $w=$this->w-$this->rMargin-$this->x;\r
+       $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;\r
+       $s=str_replace("\r",'',$txt);\r
+       $nb=strlen($s);\r
+       if($nb>0 and $s[$nb-1]=="\n")\r
+               $nb--;\r
+       $b=0;\r
+       if($border)\r
+       {\r
+               if($border==1)\r
+               {\r
+                       $border='LTRB';\r
+                       $b='LRT';\r
+                       $b2='LR';\r
+               }\r
+               else\r
+               {\r
+                       $b2='';\r
+                       if(is_int(strpos($border,'L')))\r
+                               $b2.='L';\r
+                       if(is_int(strpos($border,'R')))\r
+                               $b2.='R';\r
+                       $b=is_int(strpos($border,'T')) ? $b2.'T' : $b2;\r
+               }\r
+       }\r
+       $sep=-1;\r
+       $i=0;\r
+       $j=0;\r
+       $l=0;\r
+       $ns=0;\r
+       $nl=1;\r
+       while($i<$nb)\r
+       {\r
+               //Get next character\r
+               $c=$s[$i];\r
+               if($c=="\n")\r
+               {\r
+                       //Explicit line break\r
+                       if($this->ws>0)\r
+                       {\r
+                               $this->ws=0;\r
+                               $this->_out('0 Tw');\r
+                       }\r
+                       $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);\r
+                       $i++;\r
+                       $sep=-1;\r
+                       $j=$i;\r
+                       $l=0;\r
+                       $ns=0;\r
+                       $nl++;\r
+                       if($border and $nl==2)\r
+                               $b=$b2;\r
+                       continue;\r
+               }\r
+               if($c==' ')\r
+               {\r
+                       $sep=$i;\r
+                       $ls=$l;\r
+                       $ns++;\r
+               }\r
+               $l+=$cw[$c];\r
+               if($l>$wmax)\r
+               {\r
+                       //Automatic line break\r
+                       if($sep==-1)\r
+                       {\r
+                               if($i==$j)\r
+                                       $i++;\r
+                               if($this->ws>0)\r
+                               {\r
+                                       $this->ws=0;\r
+                                       $this->_out('0 Tw');\r
+                               }\r
+                               $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);\r
+                       }\r
+                       else\r
+                       {\r
+                               if($align=='J')\r
+                               {\r
+                                       $this->ws=($ns>1) ? ($wmax-$ls)/1000*$this->FontSize/($ns-1) : 0;\r
+                                       $this->_out(sprintf('%.3f Tw',$this->ws*$this->k));\r
+                               }\r
+                               $this->Cell($w,$h,substr($s,$j,$sep-$j),$b,2,$align,$fill);\r
+                               $i=$sep+1;\r
+                       }\r
+                       $sep=-1;\r
+                       $j=$i;\r
+                       $l=0;\r
+                       $ns=0;\r
+                       $nl++;\r
+                       if($border and $nl==2)\r
+                               $b=$b2;\r
+               }\r
+               else\r
+                       $i++;\r
+       }\r
+       //Last chunk\r
+       if($this->ws>0)\r
+       {\r
+               $this->ws=0;\r
+               $this->_out('0 Tw');\r
+       }\r
+       if($border and is_int(strpos($border,'B')))\r
+               $b.='B';\r
+       $this->Cell($w,$h,substr($s,$j,$i),$b,2,$align,$fill);\r
+       $this->x=$this->lMargin;\r
+}\r
+\r
+function Write($h,$txt,$link='')\r
+{\r
+       //Output text in flowing mode\r
+       $cw=&$this->CurrentFont['cw'];\r
+       $w=$this->w-$this->rMargin-$this->x;\r
+       $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;\r
+       $s=str_replace("\r",'',$txt);\r
+       $nb=strlen($s);\r
+       $sep=-1;\r
+       $i=0;\r
+       $j=0;\r
+       $l=0;\r
+       $nl=1;\r
+       while($i<$nb)\r
+       {\r
+               //Get next character\r
+               $c=$s[$i];\r
+               if($c=="\n")\r
+               {\r
+                       //Explicit line break\r
+                       $this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link);\r
+                       $i++;\r
+                       $sep=-1;\r
+                       $j=$i;\r
+                       $l=0;\r
+                       if($nl==1)\r
+                       {\r
+                               $this->x=$this->lMargin;\r
+                               $w=$this->w-$this->rMargin-$this->x;\r
+                               $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;\r
+                       }\r
+                       $nl++;\r
+                       continue;\r
+               }\r
+               if($c==' ')\r
+               {\r
+                       $sep=$i;\r
+                       $ls=$l;\r
+               }\r
+               $l+=$cw[$c];\r
+               if($l>$wmax)\r
+               {\r
+                       //Automatic line break\r
+                       if($sep==-1)\r
+                       {\r
+                               if($this->x>$this->lMargin)\r
+                               {\r
+                                       //Move to next line\r
+                                       $this->x=$this->lMargin;\r
+                                       $this->y+=$h;\r
+                                       $w=$this->w-$this->rMargin-$this->x;\r
+                                       $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;\r
+                                       $i++;\r
+                                       $nl++;\r
+                                       continue;\r
+                               }\r
+                               if($i==$j)\r
+                                       $i++;\r
+                               $this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link);\r
+                       }\r
+                       else\r
+                       {\r
+                               $this->Cell($w,$h,substr($s,$j,$sep-$j),0,2,'',0,$link);\r
+                               $i=$sep+1;\r
+                       }\r
+                       $sep=-1;\r
+                       $j=$i;\r
+                       $l=0;\r
+                       if($nl==1)\r
+                       {\r
+                               $this->x=$this->lMargin;\r
+                               $w=$this->w-$this->rMargin-$this->x;\r
+                               $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;\r
+                       }\r
+                       $nl++;\r
+               }\r
+               else\r
+                       $i++;\r
+       }\r
+       //Last chunk\r
+       if($i!=$j)\r
+               $this->Cell($l/1000*$this->FontSize,$h,substr($s,$j,$i),0,0,'',0,$link);\r
+}\r
+\r
+function Image($file,$x,$y,$w,$h=0,$type='',$link='')\r
+{\r
+       //Put an image on the page\r
+       if(!isset($this->images[$file]))\r
+       {\r
+               //First use of image, get info\r
+               if($type=='')\r
+               {\r
+                       $pos=strrpos($file,'.');\r
+                       if(!$pos)\r
+                               $this->Error('Image file has no extension and no type was specified: '.$file);\r
+                       $type=substr($file,$pos+1);\r
+               }\r
+               $type=strtolower($type);\r
+               //$mqr=get_magic_quotes_runtime();\r
+//             set_magic_quotes_runtime(0);\r
+               if($type=='jpg' or $type=='jpeg')\r
+                       $info=$this->_parsejpg($file);\r
+               elseif($type=='png')\r
+                       $info=$this->_parsepng($file);\r
+               else\r
+                       $this->Error('Unsupported image file type: '.$type);\r
+               //set_magic_quotes_runtime($mqr);\r
+               $info['i']=count($this->images)+1;\r
+               $this->images[$file]=$info;\r
+       }\r
+       else\r
+               $info=$this->images[$file];\r
+       //Automatic width or height calculation\r
+       if($w==0)\r
+               $w=$h*$info['w']/$info['h'];\r
+       if($h==0)\r
+               $h=$w*$info['h']/$info['w'];\r
+       $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
+       if($link)\r
+               $this->Link($x,$y,$w,$h,$link);\r
+}\r
+\r
+function Ln($h='')\r
+{\r
+       //Line feed; default value is last cell height\r
+       $this->x=$this->lMargin;\r
+       if(is_string($h))\r
+               $this->y+=$this->lasth;\r
+       else\r
+               $this->y+=$h;\r
+}\r
+\r
+function GetX()\r
+{\r
+       //Get x position\r
+       return $this->x;\r
+}\r
+\r
+function SetX($x)\r
+{\r
+       //Set x position\r
+       if($x>=0)\r
+               $this->x=$x;\r
+       else\r
+               $this->x=$this->w+$x;\r
+}\r
+\r
+function GetY()\r
+{\r
+       //Get y position\r
+       return $this->y;\r
+}\r
+\r
+function SetY($y)\r
+{\r
+       //Set y position and reset x\r
+       $this->x=$this->lMargin;\r
+       if($y>=0)\r
+               $this->y=$y;\r
+       else\r
+               $this->y=$this->h+$y;\r
+}\r
+\r
+function SetXY($x,$y)\r
+{\r
+       //Set x and y positions\r
+       $this->SetY($y);\r
+       $this->SetX($x);\r
+}\r
+\r
+function Output($file='',$download=false)\r
+{\r
+       //Output PDF to file or browser\r
+       global $HTTP_ENV_VARS;\r
+\r
+       if($this->state<3)\r
+               $this->Close();\r
+       if($file=='')\r
+       {\r
+               //Send to browser\r
+               Header('Content-Type: application/pdf');\r
+               if(headers_sent())\r
+                       $this->Error('Some data has already been output to browser, can\'t send PDF file');\r
+               Header('Content-Length: '.strlen($this->buffer));\r
+               Header('Content-disposition: inline; filename=doc.pdf');\r
+               echo $this->buffer;\r
+       }\r
+       else\r
+       {\r
+               if($download)\r
+               {\r
+                       //Download file\r
+                       if(isset($HTTP_ENV_VARS['HTTP_USER_AGENT']) and strpos($HTTP_ENV_VARS['HTTP_USER_AGENT'],'MSIE 5.5'))\r
+                               Header('Content-Type: application/dummy');\r
+                       else\r
+                               Header('Content-Type: application/octet-stream');\r
+                       if(headers_sent())\r
+                               $this->Error('Some data has already been output to browser, can\'t send PDF file');\r
+                       Header('Content-Length: '.strlen($this->buffer));\r
+                       Header('Content-disposition: attachment; filename='.$file);\r
+                       echo $this->buffer;\r
+               }\r
+               else\r
+               {\r
+                       //Save file locally\r
+                       $f=fopen($file,'wb');\r
+                       if(!$f)\r
+                               $this->Error('Unable to create output file: '.$file);\r
+                       fwrite($f,$this->buffer,strlen($this->buffer));\r
+                       fclose($f);\r
+               }\r
+       }\r
+}\r
+\r
+/****************************************************************************\r
+*                                                                           *\r
+*                              Private methods                              *\r
+*                                                                           *\r
+****************************************************************************/\r
+function _begindoc()\r
+{\r
+       //Start document\r
+       $this->state=1;\r
+       $this->_out('%PDF-1.3');\r
+}\r
+\r
+function _putpages()\r
+{\r
+       $nb=$this->page;\r
+       if(!empty($this->AliasNbPages))\r
+       {\r
+               //Replace number of pages\r
+               for($n=1;$n<=$nb;$n++)\r
+                       $this->pages[$n]=str_replace($this->AliasNbPages,$nb,$this->pages[$n]);\r
+       }\r
+       if($this->DefOrientation=='P')\r
+       {\r
+               $wPt=$this->fwPt;\r
+               $hPt=$this->fhPt;\r
+       }\r
+       else\r
+       {\r
+               $wPt=$this->fhPt;\r
+               $hPt=$this->fwPt;\r
+       }\r
+       $filter=($this->compress) ? '/Filter /FlateDecode ' : '';\r
+       for($n=1;$n<=$nb;$n++)\r
+       {\r
+               //Page\r
+               $this->_newobj();\r
+               $this->_out('<</Type /Page');\r
+               $this->_out('/Parent 1 0 R');\r
+               if(isset($this->OrientationChanges[$n]))\r
+                       $this->_out(sprintf('/MediaBox [0 0 %.2f %.2f]',$hPt,$wPt));\r
+               $this->_out('/Resources 2 0 R');\r
+               if(isset($this->PageLinks[$n]))\r
+               {\r
+                       //Links\r
+                       $annots='/Annots [';\r
+                       foreach($this->PageLinks[$n] as $pl)\r
+                       {\r
+                               $rect=sprintf('%.2f %.2f %.2f %.2f',$pl[0],$pl[1],$pl[0]+$pl[2],$pl[1]-$pl[3]);\r
+                               $annots.='<</Type /Annot /Subtype /Link /Rect ['.$rect.'] /Border [0 0 0] ';\r
+                               if(is_string($pl[4]))\r
+                                       $annots.='/A <</S /URI /URI '.$this->_textstring($pl[4]).'>>>>';\r
+                               else\r
+                               {\r
+                                       $l=$this->links[$pl[4]];\r
+                                       $h=isset($this->OrientationChanges[$l[0]]) ? $wPt : $hPt;\r
+                                       $annots.=sprintf('/Dest [%d 0 R /XYZ 0 %.2f null]>>',1+2*$l[0],$h-$l[1]*$this->k);\r
+                               }\r
+                       }\r
+                       $this->_out($annots.']');\r
+               }\r
+                $this->_out('/Group <</Type /Group /S /Transparency /CS /DeviceRGB>>');\r
+               $this->_out('/Contents '.($this->n+1).' 0 R>>');\r
+               $this->_out('endobj');\r
+               //Page content\r
+               $p=($this->compress) ? gzcompress($this->pages[$n]) : $this->pages[$n];\r
+               $this->_newobj();\r
+               $this->_out('<<'.$filter.'/Length '.strlen($p).'>>');\r
+               $this->_putstream($p);\r
+               $this->_out('endobj');\r
+       }\r
+       //Pages root\r
+       $this->offsets[1]=strlen($this->buffer);\r
+       $this->_out('1 0 obj');\r
+       $this->_out('<</Type /Pages');\r
+       $kids='/Kids [';\r
+       for($i=0;$i<$nb;$i++)\r
+               $kids.=(3+2*$i).' 0 R ';\r
+       $this->_out($kids.']');\r
+       $this->_out('/Count '.$nb);\r
+       $this->_out(sprintf('/MediaBox [0 0 %.2f %.2f]',$wPt,$hPt));\r
+       $this->_out('>>');\r
+       $this->_out('endobj');\r
+}\r
+\r
+function _putfonts()\r
+{\r
+       $nf=$this->n;\r
+       foreach($this->diffs as $diff)\r
+       {\r
+               //Encodings\r
+               $this->_newobj();\r
+               $this->_out('<</Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences ['.$diff.']>>');\r
+               $this->_out('endobj');\r
+       }\r
+       $mqr=get_magic_quotes_runtime();\r
+       //set_magic_quotes_runtime(0);\r
+       foreach($this->FontFiles as $file=>$info)\r
+       {\r
+               //Font file embedding\r
+               $this->_newobj();\r
+               $this->FontFiles[$file]['n']=$this->n;\r
+               if(defined('FPDF_FONTPATH'))\r
+                       $file=FPDF_FONTPATH.$file;\r
+               $size=filesize($file);\r
+               if(!$size)\r
+                       $this->Error('Font file not found');\r
+               $this->_out('<</Length '.$size);\r
+               if(substr($file,-2)=='.z')\r
+                       $this->_out('/Filter /FlateDecode');\r
+               $this->_out('/Length1 '.$info['length1']);\r
+               if(isset($info['length2']))\r
+                       $this->_out('/Length2 '.$info['length2'].' /Length3 0');\r
+               $this->_out('>>');\r
+               $f=fopen($file,'rb');\r
+               $this->_putstream(fread($f,$size));\r
+               fclose($f);\r
+               $this->_out('endobj');\r
+       }\r
+       //set_magic_quotes_runtime($mqr);\r
+       foreach($this->fonts as $k=>$font)\r
+       {\r
+               //Font objects\r
+               $this->_newobj();\r
+               $this->fonts[$k]['n']=$this->n;\r
+               $name=$font['name'];\r
+               $this->_out('<</Type /Font');\r
+               $this->_out('/BaseFont /'.$name);\r
+               if($font['type']=='core')\r
+               {\r
+                       //Standard font\r
+                       $this->_out('/Subtype /Type1');\r
+                       if($name!='Symbol' and $name!='ZapfDingbats')\r
+                               $this->_out('/Encoding /WinAnsiEncoding');\r
+               }\r
+               else\r
+               {\r
+                       //Additional font\r
+                       $this->_out('/Subtype /'.$font['type']);\r
+                       $this->_out('/FirstChar 32');\r
+                       $this->_out('/LastChar 255');\r
+                       $this->_out('/Widths '.($this->n+1).' 0 R');\r
+                       $this->_out('/FontDescriptor '.($this->n+2).' 0 R');\r
+                       if($font['enc'])\r
+                       {\r
+                               if(isset($font['diff']))\r
+                                       $this->_out('/Encoding '.($nf+$font['diff']).' 0 R');\r
+                               else\r
+                                       $this->_out('/Encoding /WinAnsiEncoding');\r
+                       }\r
+               }\r
+               $this->_out('>>');\r
+               $this->_out('endobj');\r
+               if($font['type']!='core')\r
+               {\r
+                       //Widths\r
+                       $this->_newobj();\r
+                       $cw=&$font['cw'];\r
+                       $s='[';\r
+                       for($i=32;$i<=255;$i++)\r
+                               $s.=$cw[chr($i)].' ';\r
+                       $this->_out($s.']');\r
+                       $this->_out('endobj');\r
+                       //Descriptor\r
+                       $this->_newobj();\r
+                       $s='<</Type /FontDescriptor /FontName /'.$name;\r
+                       foreach($font['desc'] as $k=>$v)\r
+                               $s.=' /'.$k.' '.$v;\r
+                       $file=$font['file'];\r
+                       if($file)\r
+                               $s.=' /FontFile'.($font['type']=='Type1' ? '' : '2').' '.$this->FontFiles[$file]['n'].' 0 R';\r
+                       $this->_out($s.'>>');\r
+                       $this->_out('endobj');\r
+               }\r
+       }\r
+}\r
+\r
+function _putimages()\r
+{\r
+       $filter=($this->compress) ? '/Filter /FlateDecode ' : '';\r
+       foreach($this->images as $file=>$info)\r
+       {\r
+               $this->_newobj();\r
+               $this->images[$file]['n']=$this->n;\r
+               $this->_out('<</Type /XObject');\r
+               $this->_out('/Subtype /Image');\r
+               $this->_out('/Width '.$info['w']);\r
+               $this->_out('/Height '.$info['h']);\r
+               if($info['cs']=='Indexed')\r
+                       $this->_out('/ColorSpace [/Indexed /DeviceRGB '.(strlen($info['pal'])/3-1).' '.($this->n+1).' 0 R]');\r
+               else\r
+               {\r
+                       $this->_out('/ColorSpace /'.$info['cs']);\r
+                       if($info['cs']=='DeviceCMYK')\r
+                               $this->_out('/Decode [1 0 1 0 1 0 1 0]');\r
+               }\r
+               $this->_out('/BitsPerComponent '.$info['bpc']);\r
+               $this->_out('/Filter /'.$info['f']);\r
+               if(isset($info['parms']))\r
+                       $this->_out($info['parms']);\r
+               if(isset($info['trns']) and is_array($info['trns']))\r
+               {\r
+                       $trns='';\r
+                       for($i=0;$i<count($info['trns']);$i++)\r
+                               $trns.=$info['trns'][$i].' '.$info['trns'][$i].' ';\r
+                       $this->_out('/Mask ['.$trns.']');\r
+               }\r
+               $this->_out('/Length '.strlen($info['data']).'>>');\r
+               $this->_putstream($info['data']);\r
+               $this->_out('endobj');\r
+               //Palette\r
+               if($info['cs']=='Indexed')\r
+               {\r
+                       $this->_newobj();\r
+                       $pal=($this->compress) ? gzcompress($info['pal']) : $info['pal'];\r
+                       $this->_out('<<'.$filter.'/Length '.strlen($pal).'>>');\r
+                       $this->_putstream($pal);\r
+                       $this->_out('endobj');\r
+               }\r
+       }\r
+}\r
+\r
+function _putresources()\r
+{\r
+       $this->_putfonts();\r
+       $this->_putimages();\r
+       //Resource dictionary\r
+       $this->offsets[2]=strlen($this->buffer);\r
+       $this->_out('2 0 obj');\r
+       $this->_out('<</ProcSet [/PDF /Text /ImageB /ImageC /ImageI]');\r
+       $this->_out('/Font <<');\r
+       foreach($this->fonts as $font)\r
+               $this->_out('/F'.$font['i'].' '.$font['n'].' 0 R');\r
+       $this->_out('>>');\r
+       if(count($this->images))\r
+       {\r
+               $this->_out('/XObject <<');\r
+               foreach($this->images as $image)\r
+                       $this->_out('/I'.$image['i'].' '.$image['n'].' 0 R');\r
+               $this->_out('>>');\r
+       }\r
+       $this->_out('>>');\r
+       $this->_out('endobj');\r
+}\r
+\r
+function _putinfo()\r
+{\r
+       $this->_out('/Producer '.$this->_textstring('FPDF '.FPDF_VERSION));\r
+       if(!empty($this->title))\r
+               $this->_out('/Title '.$this->_textstring($this->title));\r
+       if(!empty($this->subject))\r
+               $this->_out('/Subject '.$this->_textstring($this->subject));\r
+       if(!empty($this->author))\r
+               $this->_out('/Author '.$this->_textstring($this->author));\r
+       if(!empty($this->keywords))\r
+               $this->_out('/Keywords '.$this->_textstring($this->keywords));\r
+       if(!empty($this->creator))\r
+               $this->_out('/Creator '.$this->_textstring($this->creator));\r
+       $this->_out('/CreationDate '.$this->_textstring('D:'.date('YmdHis')));\r
+}\r
+\r
+function _putcatalog()\r
+{\r
+       $this->_out('/Type /Catalog');\r
+       $this->_out('/Pages 1 0 R');\r
+       if($this->ZoomMode=='fullpage')\r
+               $this->_out('/OpenAction [3 0 R /Fit]');\r
+       elseif($this->ZoomMode=='fullwidth')\r
+               $this->_out('/OpenAction [3 0 R /FitH null]');\r
+       elseif($this->ZoomMode=='real')\r
+               $this->_out('/OpenAction [3 0 R /XYZ null null 1]');\r
+       elseif(!is_string($this->ZoomMode))\r
+               $this->_out('/OpenAction [3 0 R /XYZ null null '.($this->ZoomMode/100).']');\r
+       if($this->LayoutMode=='single')\r
+               $this->_out('/PageLayout /SinglePage');\r
+       elseif($this->LayoutMode=='continuous')\r
+               $this->_out('/PageLayout /OneColumn');\r
+       elseif($this->LayoutMode=='two')\r
+               $this->_out('/PageLayout /TwoColumnLeft');\r
+}\r
+\r
+function _putheader()\r
+{\r
+       $this->_out('%PDF-'.$this->PDFVersion);\r
+}\r
+\r
+\r
+function _puttrailer()\r
+{\r
+       $this->_out('/Size '.($this->n+1));\r
+       $this->_out('/Root '.$this->n.' 0 R');\r
+       $this->_out('/Info '.($this->n-1).' 0 R');\r
+}\r
+\r
+function _enddoc()\r
+{\r
+        $this->_putheader();   \r
+        $this->_putpages();\r
+       $this->_putresources();\r
+       //Info\r
+       $this->_newobj();\r
+       $this->_out('<<');\r
+       $this->_putinfo();\r
+       $this->_out('>>');\r
+       $this->_out('endobj');\r
+       //Catalog\r
+       $this->_newobj();\r
+       $this->_out('<<');\r
+       $this->_putcatalog();\r
+       $this->_out('>>');\r
+       $this->_out('endobj');\r
+       //Cross-ref\r
+       $o=strlen($this->buffer);\r
+       $this->_out('xref');\r
+       $this->_out('0 '.($this->n+1));\r
+       $this->_out('0000000000 65535 f ');\r
+       for($i=1;$i<=$this->n;$i++)\r
+               $this->_out(sprintf('%010d 00000 n ',$this->offsets[$i]));\r
+       //Trailer\r
+       $this->_out('trailer');\r
+       $this->_out('<<');\r
+       $this->_puttrailer();\r
+       $this->_out('>>');\r
+       $this->_out('startxref');\r
+       $this->_out($o);\r
+       $this->_out('%%EOF');\r
+       $this->state=3;\r
+}\r
+\r
+function _beginpage($orientation)\r
+{\r
+       $this->page++;\r
+       $this->pages[$this->page]='';\r
+       $this->state=2;\r
+       $this->x=$this->lMargin;\r
+       $this->y=$this->tMargin;\r
+       $this->lasth=0;\r
+       $this->FontFamily='';\r
+       //Page orientation\r
+       if(!$orientation)\r
+               $orientation=$this->DefOrientation;\r
+       else\r
+       {\r
+               $orientation=strtoupper($orientation[0]);\r
+               if($orientation!=$this->DefOrientation)\r
+                       $this->OrientationChanges[$this->page]=true;\r
+       }\r
+       if($orientation!=$this->CurOrientation)\r
+       {\r
+               //Change orientation\r
+               if($orientation=='P')\r
+               {\r
+                       $this->wPt=$this->fwPt;\r
+                       $this->hPt=$this->fhPt;\r
+                       $this->w=$this->fw;\r
+                       $this->h=$this->fh;\r
+               }\r
+               else\r
+               {\r
+                       $this->wPt=$this->fhPt;\r
+                       $this->hPt=$this->fwPt;\r
+                       $this->w=$this->fh;\r
+                       $this->h=$this->fw;\r
+               }\r
+               $this->PageBreakTrigger=$this->h-$this->bMargin;\r
+               $this->CurOrientation=$orientation;\r
+       }\r
+}\r
+\r
+function _endpage()\r
+{\r
+       //End of page contents\r
+       $this->state=1;\r
+}\r
+\r
+function _newobj()\r
+{\r
+       //Begin a new object\r
+       $this->n++;\r
+       $this->offsets[$this->n]=strlen($this->buffer);\r
+       $this->_out($this->n.' 0 obj');\r
+}\r
+\r
+function _dounderline($x,$y,$txt)\r
+{\r
+       //Underline text\r
+       $up=$this->CurrentFont['up'];\r
+       $ut=$this->CurrentFont['ut'];\r
+       $w=$this->GetStringWidth($txt)+$this->ws*substr_count($txt,' ');\r
+       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
+}\r
+\r
+function _parsejpg($file)\r
+{\r
+       //Extract info from a JPEG file\r
+       $a=GetImageSize($file);\r
+       if(!$a)\r
+               $this->Error('Missing or incorrect image file: '.$file);\r
+       if($a[2]!=2)\r
+               $this->Error('Not a JPEG file: '.$file);\r
+       if(!isset($a['channels']) or $a['channels']==3)\r
+               $colspace='DeviceRGB';\r
+       elseif($a['channels']==4)\r
+               $colspace='DeviceCMYK';\r
+       else\r
+               $colspace='DeviceGray';\r
+       $bpc=isset($a['bits']) ? $a['bits'] : 8;\r
+       //Read whole file\r
+       $f=fopen($file,'rb');\r
+       $data=fread($f,filesize($file));\r
+       fclose($f);\r
+       return array('w'=>$a[0],'h'=>$a[1],'cs'=>$colspace,'bpc'=>$bpc,'f'=>'DCTDecode','data'=>$data);\r
+}\r
+\r
+function _parsepng($file)\r
+{\r
+       //Extract info from a PNG file\r
+       $f=fopen($file,'rb');\r
+       if(!$f)\r
+               $this->Error('Can\'t open image file: '.$file);\r
+       //Check signature\r
+       if(fread($f,8)!=chr(137).'PNG'.chr(13).chr(10).chr(26).chr(10))\r
+               $this->Error('Not a PNG file: '.$file);\r
+       //Read header chunk\r
+       fread($f,4);\r
+       if(fread($f,4)!='IHDR')\r
+               $this->Error('Incorrect PNG file: '.$file);\r
+       $w=$this->_freadint($f);\r
+       $h=$this->_freadint($f);\r
+       $bpc=ord(fread($f,1));\r
+       if($bpc>8)\r
+               $this->Error('16-bit depth not supported: '.$file);\r
+       $ct=ord(fread($f,1));\r
+       if($ct==0)\r
+               $colspace='DeviceGray';\r
+       elseif($ct==2)\r
+               $colspace='DeviceRGB';\r
+       elseif($ct==3)\r
+               $colspace='Indexed';\r
+       else\r
+               $this->Error("Alpha channel not supported: $ct ".$file);\r
+       if(ord(fread($f,1))!=0)\r
+               $this->Error('Unknown compression method: '.$file);\r
+       if(ord(fread($f,1))!=0)\r
+               $this->Error('Unknown filter method: '.$file);\r
+       if(ord(fread($f,1))!=0)\r
+               $this->Error('Interlacing not supported: '.$file);\r
+       fread($f,4);\r
+       $parms='/DecodeParms <</Predictor 15 /Colors '.($ct==2 ? 3 : 1).' /BitsPerComponent '.$bpc.' /Columns '.$w.'>>';\r
+       //Scan chunks looking for palette, transparency and image data\r
+       $pal='';\r
+       $trns='';\r
+       $data='';\r
+       do\r
+       {\r
+               $n=$this->_freadint($f);\r
+               $type=fread($f,4);\r
+               if($type=='PLTE')\r
+               {\r
+                       //Read palette\r
+                       $pal=fread($f,$n);\r
+                       fread($f,4);\r
+               }\r
+               elseif($type=='tRNS')\r
+               {\r
+                       //Read transparency info\r
+                       $t=fread($f,$n);\r
+                       if($ct==0)\r
+                               $trns=array(ord(substr($t,1,1)));\r
+                       elseif($ct==2)\r
+                               $trns=array(ord(substr($t,1,1)),ord(substr($t,3,1)),ord(substr($t,5,1)));\r
+                       else\r
+                       {\r
+                               $pos=strpos($t,chr(0));\r
+                               if(is_int($pos))\r
+                                       $trns=array($pos);\r
+                       }\r
+                       fread($f,4);\r
+               }\r
+               elseif($type=='IDAT')\r
+               {\r
+                       //Read image data block\r
+                       $data.=fread($f,$n);\r
+                       fread($f,4);\r
+               }\r
+               elseif($type=='IEND')\r
+                       break;\r
+               else\r
+                       fread($f,$n+4);\r
+       }\r
+       while($n);\r
+       if($colspace=='Indexed' and empty($pal))\r
+               $this->Error('Missing palette in '.$file);\r
+       fclose($f);\r
+       return array('w'=>$w,'h'=>$h,'cs'=>$colspace,'bpc'=>$bpc,'f'=>'FlateDecode','parms'=>$parms,'pal'=>$pal,'trns'=>$trns,'data'=>$data);\r
+}\r
+\r
+function _freadint($f)\r
+{\r
+       //Read a 4-byte integer from file\r
+       $i=ord(fread($f,1))<<24;\r
+       $i+=ord(fread($f,1))<<16;\r
+       $i+=ord(fread($f,1))<<8;\r
+       $i+=ord(fread($f,1));\r
+       return $i;\r
+}\r
+\r
+function _textstring($s)\r
+{\r
+       //Format a text string\r
+       return '('.$this->_escape($s).')';\r
+}\r
+\r
+function _escape($s)\r
+{\r
+       //Add \ before \, ( and )\r
+       return str_replace(')','\\)',str_replace('(','\\(',str_replace('\\','\\\\',$s)));\r
+}\r
+\r
+function _putstream($s)\r
+{\r
+       $this->_out('stream');\r
+       $this->_out($s);\r
+       $this->_out('endstream');\r
+}\r
+\r
+function _out($s)\r
+{\r
+       //Add a line to the document\r
+       if($this->state==2)\r
+               $this->pages[$this->page].=$s."\n";\r
+       else\r
+               $this->buffer.=$s."\n";\r
+}\r
+//End of class\r
+}\r
+\r
+//Handle silly IE contype request\r
+  \r