2 /*******************************************************************************
3 * tFPDF (based on FPDF 1.6) *
7 * Author: Ian Back <ianb@bpm1.com> *
9 *******************************************************************************/
10 define('tFPDF_VERSION','1.02');
16 var $extraFontSubsets = 0;
19 var $page; //current page number
20 var $n; //current object number
21 var $offsets; //array of object offsets
22 var $buffer; //buffer holding in-memory PDF
23 var $pages; //array containing pages
24 var $state; //current document state
25 var $compress; //compression flag
26 var $k; //scale factor (number of points in user unit)
27 var $DefOrientation; //default orientation
28 var $CurOrientation; //current orientation
29 var $PageFormats; //available page formats
30 var $DefPageFormat; //default page format
31 var $CurPageFormat; //current page format
32 var $PageSizes; //array storing non-default page sizes
33 var $wPt,$hPt; //dimensions of current page in points
34 var $w,$h; //dimensions of current page in user unit
35 var $lMargin; //left margin
36 var $tMargin; //top margin
37 var $rMargin; //right margin
38 var $bMargin; //page break margin
39 var $cMargin; //cell margin
40 var $x,$y; //current position in user unit
41 var $lasth; //height of last printed cell
42 var $LineWidth; //line width in user unit
43 var $CoreFonts; //array of standard font names
44 var $fonts; //array of used fonts
45 var $FontFiles; //array of font files
46 var $diffs; //array of encoding differences
47 var $FontFamily; //current font family
48 var $FontStyle; //current font style
49 var $underline; //underlining flag
50 var $CurrentFont; //current font info
51 var $FontSizePt; //current font size in points
52 var $FontSize; //current font size in user unit
53 var $DrawColor; //commands for drawing color
54 var $FillColor; //commands for filling color
55 var $TextColor; //commands for text color
56 var $ColorFlag; //indicates whether fill and text colors are different
57 var $ws; //word spacing
58 var $images; //array of used images
59 var $PageLinks; //array of links in pages
60 var $links; //array of internal links
61 var $AutoPageBreak; //automatic page breaking
62 var $PageBreakTrigger; //threshold used to trigger page breaks
63 var $InHeader; //flag set when processing header
64 var $InFooter; //flag set when processing footer
65 var $ZoomMode; //zoom display mode
66 var $LayoutMode; //layout display mode
68 var $subject; //subject
70 var $keywords; //keywords
71 var $creator; //creator
72 var $AliasNbPages; //alias for total number of pages
73 var $PDFVersion; //PDF version number
75 /*******************************************************************************
79 *******************************************************************************/
80 function __construct($orientation='P', $unit='mm', $format='A4')
84 //Initialization of properties
89 $this->PageSizes=array();
92 $this->FontFiles=array();
94 $this->images=array();
96 $this->InHeader=false;
97 $this->InFooter=false;
101 $this->FontSizePt=12;
102 $this->underline=false;
103 $this->DrawColor='0 G';
104 $this->FillColor='0 g';
105 $this->TextColor='0 g';
106 $this->ColorFlag=false;
109 $this->CoreFonts=array('courier'=>'Courier', 'courierB'=>'Courier-Bold', 'courierI'=>'Courier-Oblique', 'courierBI'=>'Courier-BoldOblique',
110 'helvetica'=>'Helvetica', 'helveticaB'=>'Helvetica-Bold', 'helveticaI'=>'Helvetica-Oblique', 'helveticaBI'=>'Helvetica-BoldOblique',
111 'times'=>'Times-Roman', 'timesB'=>'Times-Bold', 'timesI'=>'Times-Italic', 'timesBI'=>'Times-BoldItalic',
112 'symbol'=>'Symbol', 'zapfdingbats'=>'ZapfDingbats');
123 $this->Error('Incorrect unit: '.$unit);
126 $this->PageFormats=array('a3'=>array(841.89,1190.55), 'a4'=>array(595.28,841.89), 'a5'=>array(420.94,595.28),
127 'letter'=>array(612,792), 'legal'=>array(612,1008));
128 if(is_string($format))
129 $format=$this->_getpageformat($format);
130 $this->DefPageFormat=$format;
131 $this->CurPageFormat=$format;
133 $orientation=strtolower($orientation);
134 if($orientation=='p' || $orientation=='portrait')
136 $this->DefOrientation='P';
137 $this->w=$this->DefPageFormat[0];
138 $this->h=$this->DefPageFormat[1];
140 elseif($orientation=='l' || $orientation=='landscape')
142 $this->DefOrientation='L';
143 $this->w=$this->DefPageFormat[1];
144 $this->h=$this->DefPageFormat[0];
147 $this->Error('Incorrect orientation: '.$orientation);
148 $this->CurOrientation=$this->DefOrientation;
149 $this->wPt=$this->w*$this->k;
150 $this->hPt=$this->h*$this->k;
151 //Page margins (1 cm)
152 $margin=28.35/$this->k;
153 $this->SetMargins($margin,$margin);
154 //Interior cell margin (1 mm)
155 $this->cMargin=$margin/10;
156 //Line width (0.2 mm)
157 $this->LineWidth=.567/$this->k;
158 //Automatic page break
159 $this->SetAutoPageBreak(true,2*$margin);
160 //Full width display mode
161 $this->SetDisplayMode('fullwidth');
163 $this->SetCompression(true);
164 //Set default PDF version number
165 $this->PDFVersion='1.3';
168 function SetMargins($left, $top, $right=null)
170 //Set left, top and right margins
171 $this->lMargin=$left;
175 $this->rMargin=$right;
178 function SetLeftMargin($margin)
181 $this->lMargin=$margin;
182 if($this->page>0 && $this->x<$margin)
186 function SetTopMargin($margin)
189 $this->tMargin=$margin;
192 function SetRightMargin($margin)
195 $this->rMargin=$margin;
198 function SetAutoPageBreak($auto, $margin=0)
200 //Set auto page break mode and triggering margin
201 $this->AutoPageBreak=$auto;
202 $this->bMargin=$margin;
203 $this->PageBreakTrigger=$this->h-$margin;
206 function SetDisplayMode($zoom, $layout='continuous')
208 //Set display mode in viewer
209 if($zoom=='fullpage' || $zoom=='fullwidth' || $zoom=='real' || $zoom=='default' || !is_string($zoom))
210 $this->ZoomMode=$zoom;
212 $this->Error('Incorrect zoom display mode: '.$zoom);
213 if($layout=='single' || $layout=='continuous' || $layout=='two' || $layout=='default')
214 $this->LayoutMode=$layout;
216 $this->Error('Incorrect layout display mode: '.$layout);
219 function SetCompression($compress)
221 //Set page compression
222 if(function_exists('gzcompress'))
223 $this->compress=$compress;
225 $this->compress=false;
228 function SetTitle($title, $isUTF8=false)
232 $title=$this->_UTF8toUTF16($title);
236 function SetSubject($subject, $isUTF8=false)
238 //Subject of document
240 $subject=$this->_UTF8toUTF16($subject);
241 $this->subject=$subject;
244 function SetAuthor($author, $isUTF8=false)
248 $author=$this->_UTF8toUTF16($author);
249 $this->author=$author;
252 function SetKeywords($keywords, $isUTF8=false)
254 //Keywords of document
256 $keywords=$this->_UTF8toUTF16($keywords);
257 $this->keywords=$keywords;
260 function SetCreator($creator, $isUTF8=false)
262 //Creator of document
264 $creator=$this->_UTF8toUTF16($creator);
265 $this->creator=$creator;
268 function AliasNbPages($alias='{nb}')
270 //Define an alias for total number of pages
271 $this->AliasNbPages=$alias;
278 die('<b>tFPDF error:</b> '.$msg);
295 $this->InFooter=true;
297 $this->InFooter=false;
304 function AddPage($orientation='', $format='')
309 $family=$this->FontFamily;
310 $style=$this->FontStyle.($this->underline ? 'U' : '');
311 $size=$this->FontSizePt;
312 $lw=$this->LineWidth;
313 $dc=$this->DrawColor;
314 $fc=$this->FillColor;
315 $tc=$this->TextColor;
316 $cf=$this->ColorFlag;
320 $this->InFooter=true;
322 $this->InFooter=false;
327 $this->_beginpage($orientation,$format);
328 //Set line cap style to square
331 $this->LineWidth=$lw;
332 $this->_out(sprintf('%.2F w',$lw*$this->k));
335 $this->SetFont($family,$style,$size);
337 $this->DrawColor=$dc;
340 $this->FillColor=$fc;
343 $this->TextColor=$tc;
344 $this->ColorFlag=$cf;
346 $this->InHeader=true;
348 $this->InHeader=false;
350 if($this->LineWidth!=$lw)
352 $this->LineWidth=$lw;
353 $this->_out(sprintf('%.2F w',$lw*$this->k));
357 $this->SetFont($family,$style,$size);
359 if($this->DrawColor!=$dc)
361 $this->DrawColor=$dc;
364 if($this->FillColor!=$fc)
366 $this->FillColor=$fc;
369 $this->TextColor=$tc;
370 $this->ColorFlag=$cf;
375 //To be implemented in your own inherited class
380 //To be implemented in your own inherited class
385 //Get current page number
389 function SetDrawColor($r, $g=null, $b=null)
391 //Set color for all stroking operations
392 if(($r==0 && $g==0 && $b==0) || $g===null)
393 $this->DrawColor=sprintf('%.3F G',$r/255);
395 $this->DrawColor=sprintf('%.3F %.3F %.3F RG',$r/255,$g/255,$b/255);
397 $this->_out($this->DrawColor);
400 function SetFillColor($r, $g=null, $b=null)
402 //Set color for all filling operations
403 if(($r==0 && $g==0 && $b==0) || $g===null)
404 $this->FillColor=sprintf('%.3F g',$r/255);
406 $this->FillColor=sprintf('%.3F %.3F %.3F rg',$r/255,$g/255,$b/255);
407 $this->ColorFlag=($this->FillColor!=$this->TextColor);
409 $this->_out($this->FillColor);
412 function SetTextColor($r, $g=null, $b=null)
415 if(($r==0 && $g==0 && $b==0) || $g===null)
416 $this->TextColor=sprintf('%.3F g',$r/255);
418 $this->TextColor=sprintf('%.3F %.3F %.3F rg',$r/255,$g/255,$b/255);
419 $this->ColorFlag=($this->FillColor!=$this->TextColor);
422 function GetStringWidth($s)
424 //Get width of a string in the current font
426 $cw=&$this->CurrentFont['cw'];
428 if ($this->unifontSubset) {
429 $unicode = $this->UTF8StringToArray($s);
430 foreach($unicode as $char) {
431 if (isset($cw[$char])) { $w+=$cw[$char]; }
432 else if($char>0 && $char<128 && isset($cw[chr($char)])) { $w+=$cw[chr($char)]; }
433 else if(isset($this->CurrentFont['desc']['MissingWidth'])) { $w += $this->CurrentFont['desc']['MissingWidth']; }
434 else if(isset($this->CurrentFont['MissingWidth'])) { $w += $this->CurrentFont['MissingWidth']; }
443 return $w*$this->FontSize/1000;
446 function SetLineWidth($width)
449 $this->LineWidth=$width;
451 $this->_out(sprintf('%.2F w',$width*$this->k));
454 function Line($x1, $y1, $x2, $y2)
457 $this->_out(sprintf('%.2F %.2F m %.2F %.2F l S',$x1*$this->k,($this->h-$y1)*$this->k,$x2*$this->k,($this->h-$y2)*$this->k));
460 function Rect($x, $y, $w, $h, $style='')
465 elseif($style=='FD' || $style=='DF')
469 $this->_out(sprintf('%.2F %.2F %.2F %.2F re %s',$x*$this->k,($this->h-$y)*$this->k,$w*$this->k,-$h*$this->k,$op));
473 function AddFont($family, $style='', $file='', $uni=false)
475 //Add a TrueType or Type1 font
476 $family=strtolower($family);
479 $style=strtoupper($style);
484 $file=str_replace(' ','',$family).strtolower($style).'.ttf';
487 $file=str_replace(' ','',$family).strtolower($style).'.php';
490 $fontkey=$family.$style;
491 if(isset($this->fonts[$fontkey]))
497 $ttfilename = dirname(__FILE__). '/font/unifont/'.$file ;
500 $filename =str_replace(' ','',$filename );
501 $filename =str_replace('-','',$filename );
502 $unifilename = dirname(__FILE__).'/font/unifont/'.strtolower(substr($filename ,0,(strpos($filename ,'.'))));
505 if (file_exists($unifilename.'.mtx.php')) {
506 include($unifilename.'.mtx.php');
508 if (!isset($type) || $type != "TrueTypesubset") {
509 require_once(dirname(__FILE__).'/font/unifont/ttfonts.php');
510 $ttf = new TTFontFile();
511 $ttf->getMetrics($ttfilename);
512 $cw = $ttf->charWidths;
513 $type = "TrueTypesubset";
514 $name = preg_replace('/ /','',$ttf->fullName);
515 $desc= array('Ascent'=>round($ttf->ascent),
516 'Descent'=>round($ttf->descent),
517 'CapHeight'=>round($ttf->capHeight),
518 'Flags'=>$ttf->flags,
519 'FontBBox'=>'['.round($ttf->bbox[0])." ".round($ttf->bbox[1])." ".round($ttf->bbox[2])." ".round($ttf->bbox[3]).']',
520 'ItalicAngle'=>$ttf->italicAngle,
521 'StemV'=>round($ttf->stemV),
522 'MissingWidth'=>round($ttf->defaultWidth));
523 $up = round($ttf->underlinePosition);
524 $ut = round($ttf->underlineThickness);
525 //Generate metrics .php file
527 $s.='$type=\''.$type."';\n";
528 $s.='$name=\''.$name."';\n";
529 $s.='$desc='.var_export($desc,true).";\n";
530 $s.='$up='.$up.";\n";
531 $s.='$ut='.$ut.";\n";
532 $s.='$cw='.var_export($cw,true).";\n";
534 if (is_writable($this->_getfontpath().'unifont')) {
535 $fh = fopen($unifilename.'.mtx.php',"w");
536 fwrite($fh,$s,strlen($s));
542 $this->Error('Problem with the font definition file');
544 $i = count($this->fonts)+$this->extraFontSubsets+1;
545 $sbarr = range(0,127);
546 $this->fonts[$fontkey] = array('i'=>$i, 'type'=>$type, 'name'=>$name, 'desc'=>$desc, 'up'=>$up, 'ut'=>$ut, 'cw'=>$cw, 'enc'=>$enc, 'file'=>$ttfilename, 'subsets'=>array(0=>$sbarr), 'subsetfontids'=>array($i), 'used'=>false);
550 include($this->_getfontpath().$file);
552 $this->Error('Could not include font definition file');
553 $i=count($this->fonts)+$this->extraFontSubsets+1;
554 $this->fonts[$fontkey]=array('i'=>$i, 'type'=>$type, 'name'=>$name, 'desc'=>$desc, 'up'=>$up, 'ut'=>$ut, 'cw'=>$cw, 'enc'=>$enc, 'file'=>$file);
559 //Search existing encodings
561 $nb=count($this->diffs);
562 for($i=1;$i<=$nb;$i++)
564 if($this->diffs[$i]==$diff)
573 $this->diffs[$d]=$diff;
575 $this->fonts[$fontkey]['diff']=$d;
579 if($type=='TrueType')
580 $this->FontFiles[$file]=array('length1'=>$originalsize);
581 else if ($uni && $type == "TrueTypesubset")
582 $this->FontFiles[$file]=array('type'=>"TrueTypesubset");
584 $this->FontFiles[$file]=array('length1'=>$size1, 'length2'=>$size2);
588 function SetFont($family, $style='', $size=0)
590 //Select a font; size given in points
591 global $fpdf_charwidths;
593 $family=strtolower($family);
595 $family=$this->FontFamily;
598 elseif($family=='symbol' || $family=='zapfdingbats')
600 $style=strtoupper($style);
601 if(strpos($style,'U')!==false)
603 $this->underline=true;
604 $style=str_replace('U','',$style);
607 $this->underline=false;
611 $size=$this->FontSizePt;
612 //Test if font is already selected
613 if($this->FontFamily==$family && $this->FontStyle==$style && $this->FontSizePt==$size)
615 //Test if used for the first time
616 $fontkey=$family.$style;
617 if(!isset($this->fonts[$fontkey]) && isset($this->fonts[$family])) {
621 if(!isset($this->fonts[$fontkey]))
623 //Check if one of the standard fonts
624 if(isset($this->CoreFonts[$fontkey]))
626 if(!isset($fpdf_charwidths[$fontkey]))
630 if($family=='times' || $family=='helvetica')
631 $file.=strtolower($style);
632 //var_dump($file); var_dump($this->_getfontpath());
633 include($this->_getfontpath().$file.'.php');
634 if(!isset($fpdf_charwidths[$fontkey]))
635 $this->Error('Could not include font metric file');
637 $i=count($this->fonts)+1+$this->extraFontSubsets;
638 $name=$this->CoreFonts[$fontkey];
639 $cw=$fpdf_charwidths[$fontkey];
640 $this->fonts[$fontkey]=array('i'=>$i, 'type'=>'core', 'name'=>$name, 'up'=>-100, 'ut'=>50, 'cw'=>$cw);
644 $this->Error('Undefined font qq: '.$family.' '.$style);
648 $this->FontFamily=$family;
649 $this->FontStyle=$style;
650 $this->FontSizePt=$size;
652 $this->FontSize=floatval(str_replace("px","",$size))/$this->k;
653 $this->CurrentFont=&$this->fonts[$fontkey];
654 if ($this->fonts[$fontkey]['type']=='TrueTypesubset') { $this->unifontSubset = true; }
655 else { $this->unifontSubset = false; }
657 $this->_out(sprintf('BT /F%d %.2F Tf ET',$this->CurrentFont['i'],$this->FontSizePt));
660 function SetFontSize($size)
662 //Set font size in points
663 if($this->FontSizePt==$size)
665 $this->FontSizePt=$size;
666 $this->FontSize=$size/$this->k;
668 $this->_out(sprintf('BT /F%d %.2F Tf ET',$this->CurrentFont['i'],$this->FontSizePt));
673 //Create a new internal link
674 $n=count($this->links)+1;
675 $this->links[$n]=array(0, 0);
679 function SetLink($link, $y=0, $page=-1)
681 //Set destination of internal link
686 $this->links[$link]=array($page, $y);
689 function Link($x, $y, $w, $h, $link)
691 //Put a link on the page
692 $this->PageLinks[$this->page][]=array($x*$this->k, $this->hPt-$y*$this->k, $w*$this->k, $h*$this->k, $link);
695 function Text($x, $y, $txt)
698 if ($this->unifontSubset)
699 $txt2 = $this->UTF8toSubset($txt);
701 $txt2='('.$this->_escape($txt).')';
702 $s=sprintf('BT %.2F %.2F Td %s Tj ET',$x*$this->k,($this->h-$y)*$this->k,$txt2);
703 if($this->underline && $txt!='')
704 $s.=' '.$this->_dounderline($x,$y,$txt);
706 $s='q '.$this->TextColor.' '.$s.' Q';
710 function AcceptPageBreak()
712 //Accept automatic page break or not
713 return $this->AutoPageBreak;
716 function Cell($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link='')
720 if($this->y+$h>$this->PageBreakTrigger && !$this->InHeader && !$this->InFooter && $this->AcceptPageBreak())
722 //Automatic page break
730 $this->AddPage($this->CurOrientation,$this->CurPageFormat);
735 $this->_out(sprintf('%.3F Tw',$ws*$k));
739 $w=$this->w-$this->rMargin-$this->x;
741 if($fill || $border==1)
744 $op=($border==1) ? 'B' : 'f';
747 $s=sprintf('%.2F %.2F %.2F %.2F re %s ',$this->x*$k,($this->h-$this->y)*$k,$w*$k,-$h*$k,$op);
749 if(is_string($border))
753 if(strpos($border,'L')!==false)
754 $s.=sprintf('%.2F %.2F m %.2F %.2F l S ',$x*$k,($this->h-$y)*$k,$x*$k,($this->h-($y+$h))*$k);
755 if(strpos($border,'T')!==false)
756 $s.=sprintf('%.2F %.2F m %.2F %.2F l S ',$x*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-$y)*$k);
757 if(strpos($border,'R')!==false)
758 $s.=sprintf('%.2F %.2F m %.2F %.2F l S ',($x+$w)*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
759 if(strpos($border,'B')!==false)
760 $s.=sprintf('%.2F %.2F m %.2F %.2F l S ',$x*$k,($this->h-($y+$h))*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
765 $dx=$w-$this->cMargin-$this->GetStringWidth($txt);
767 $dx=($w-$this->GetStringWidth($txt))/2;
771 $s.='q '.$this->TextColor.' ';
772 if ($this->unifontSubset)
773 $txt2 = $this->UTF8toSubset($txt);
775 $txt2='('.str_replace(')','\\)',str_replace('(','\\(',str_replace('\\','\\\\',$txt))).')';
776 $s.=sprintf('BT %.2F %.2F Td %s Tj ET',($this->x+$dx)*$k,($this->h-($this->y+.5*$h+.3*$this->FontSize))*$k,$txt2);
778 $s.=' '.$this->_dounderline($this->x+$dx,$this->y+.5*$h+.3*$this->FontSize,$txt);
782 $this->Link($this->x+$dx,$this->y+.5*$h-.5*$this->FontSize,$this->GetStringWidth($txt),$this->FontSize,$link);
792 $this->x=$this->lMargin;
798 function MultiCell($w, $h, $txt, $border=0, $align='J', $fill=false)
800 //Output text with automatic or explicit line breaks
801 $cw=&$this->CurrentFont['cw'];
803 $w=$this->w-$this->rMargin-$this->x;
804 $wmax=($w-2*$this->cMargin);
805 $s=str_replace("\r",'',$txt);
806 if ($this->unifontSubset) {
807 $nb=mb_strlen($s, 'utf-8');
808 while($nb>0 && mb_substr($s,$nb-1,1,'utf-8')=="\n") $nb--;
812 if($nb>0 && $s[$nb-1]=="\n")
828 if(strpos($border,'L')!==false)
830 if(strpos($border,'R')!==false)
832 $b=(strpos($border,'T')!==false) ? $b2.'T' : $b2;
844 if ($this->unifontSubset) {
845 $c = mb_substr($s,$i,1,'UTF-8');
851 //Explicit line break
857 if ($this->unifontSubset) {
858 $this->Cell($w,$h,mb_substr($s,$j,$i-$j,'UTF-8'),$b,2,$align,$fill);
861 $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
869 if($border && $nl==2)
880 if ($this->unifontSubset) { $l += $this->GetStringWidth($c); }
881 else { $l += $cw[$c]*$this->FontSize/1000; }
885 //Automatic line break
895 if ($this->unifontSubset) {
896 $this->Cell($w,$h,mb_substr($s,$j,$i-$j,'UTF-8'),$b,2,$align,$fill);
899 $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
906 $this->ws=($ns>1) ? ($wmax-$ls)/($ns-1) : 0;
907 $this->_out(sprintf('%.3F Tw',$this->ws*$this->k));
909 if ($this->unifontSubset) {
910 $this->Cell($w,$h,mb_substr($s,$j,$sep-$j,'UTF-8'),$b,2,$align,$fill);
913 $this->Cell($w,$h,substr($s,$j,$sep-$j),$b,2,$align,$fill);
922 if($border && $nl==2)
934 if($border && strpos($border,'B')!==false)
936 if ($this->unifontSubset) {
937 $this->Cell($w,$h,mb_substr($s,$j,$i-$j,'UTF-8'),$b,2,$align,$fill);
940 $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
942 $this->x=$this->lMargin;
945 function Write($h, $txt, $link='')
947 //Output text in flowing mode
948 $cw=&$this->CurrentFont['cw'];
949 $w=$this->w-$this->rMargin-$this->x;
951 $wmax=($w-2*$this->cMargin);
952 $s=str_replace("\r",'',$txt);
953 if ($this->unifontSubset) {
954 $nb=mb_strlen($s, 'UTF-8');
955 if($nb==1 && $s==" ") {
956 $this->x += $this->GetStringWidth($s);
973 if ($this->unifontSubset) {
974 $c = mb_substr($s,$i,1,'UTF-8');
980 //Explicit line break
981 if ($this->unifontSubset) {
982 $this->Cell($w,$h,mb_substr($s,$j,$i-$j,'UTF-8'),0,2,'',0,$link);
985 $this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link);
993 $this->x=$this->lMargin;
994 $w=$this->w-$this->rMargin-$this->x;
995 $wmax=($w-2*$this->cMargin);
1003 if ($this->unifontSubset) { $l += $this->GetStringWidth($c); }
1004 else { $l += $cw[$c]*$this->FontSize/1000; }
1008 //Automatic line break
1011 if($this->x>$this->lMargin)
1014 $this->x=$this->lMargin;
1016 $w=$this->w-$this->rMargin-$this->x;
1017 $wmax=($w-2*$this->cMargin);
1024 if ($this->unifontSubset) {
1025 $this->Cell($w,$h,mb_substr($s,$j,$i-$j,'UTF-8'),0,2,'',0,$link);
1028 $this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link);
1033 if ($this->unifontSubset) {
1034 $this->Cell($w,$h,mb_substr($s,$j,$sep-$j,'UTF-8'),0,2,'',0,$link);
1037 $this->Cell($w,$h,substr($s,$j,$sep-$j),0,2,'',0,$link);
1046 $this->x=$this->lMargin;
1047 $w=$this->w-$this->rMargin-$this->x;
1048 $wmax=($w-2*$this->cMargin);
1057 if ($this->unifontSubset) {
1058 $this->Cell($l,$h,mb_substr($s,$j,$i-$j,'UTF-8'),0,0,'',0,$link);
1061 $this->Cell($l,$h,substr($s,$j),0,0,'',0,$link);
1066 function Ln($h=null)
1068 //Line feed; default value is last cell height
1069 $this->x=$this->lMargin;
1071 $this->y+=$this->lasth;
1076 function Image($file, $x=null, $y=null, $w=0, $h=0, $type='', $link='')
1078 //Put an image on the page
1079 if(!isset($this->images[$file]))
1081 //First use of this image, get info
1084 $pos=strrpos($file,'.');
1086 $this->Error('Image file has no extension and no type was specified: '.$file);
1087 $type=substr($file,$pos+1);
1089 $type=strtolower($type);
1092 $mtd='_parse'.$type;
1093 if(!method_exists($this,$mtd))
1094 $this->Error('Unsupported image type: '.$type);
1095 $info=$this->$mtd($file);
1096 $info['i']=count($this->images)+1;
1097 $this->images[$file]=$info;
1100 $info=$this->images[$file];
1101 //Automatic width and height calculation if needed
1104 //Put image at 72 dpi
1105 $w=$info['w']/$this->k;
1106 $h=$info['h']/$this->k;
1109 $w=$h*$info['w']/$info['h'];
1111 $h=$w*$info['h']/$info['w'];
1115 if($this->y+$h>$this->PageBreakTrigger && !$this->InHeader && !$this->InFooter && $this->AcceptPageBreak())
1117 //Automatic page break
1119 $this->AddPage($this->CurOrientation,$this->CurPageFormat);
1127 $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']));
1129 $this->Link($x,$y,$w,$h,$link);
1144 $this->x=$this->w+$x;
1155 //Set y position and reset x
1156 $this->x=$this->lMargin;
1160 $this->y=$this->h+$y;
1163 function SetXY($x, $y)
1165 //Set x and y positions
1170 function Output($name='', $dest='')
1172 //Output PDF to some destination
1175 $dest=strtoupper($dest);
1189 //Send to standard output
1191 $this->Error('Some data has already been output, can\'t send PDF file');
1192 if(php_sapi_name()!='cli')
1194 //We send to a browser
1195 header('Content-Type: application/pdf');
1197 $this->Error('Some data has already been output, can\'t send PDF file');
1198 header('Content-Length: '.strlen($this->buffer));
1199 header('Content-Disposition: inline; filename="'.$name.'"');
1200 header('Cache-Control: private, max-age=0, must-revalidate');
1201 header('Pragma: public');
1202 ini_set('zlib.output_compression','0');
1209 $this->Error('Some data has already been output, can\'t send PDF file');
1210 header('Content-Type: application/x-download');
1212 $this->Error('Some data has already been output, can\'t send PDF file');
1213 header('Content-Length: '.strlen($this->buffer));
1214 header('Content-Disposition: attachment; filename="'.$name.'"');
1215 header('Cache-Control: private, max-age=0, must-revalidate');
1216 header('Pragma: public');
1217 ini_set('zlib.output_compression','0');
1221 //Save to local file
1222 $f=fopen($name,'wb');
1224 $this->Error('Unable to create output file: '.$name);
1225 fwrite($f,$this->buffer,strlen($this->buffer));
1229 //Return as a string
1230 return $this->buffer;
1232 $this->Error('Incorrect output destination: '.$dest);
1237 /*******************************************************************************
1239 * Protected methods *
1241 *******************************************************************************/
1242 function _dochecks()
1244 //Check availability of %F
1245 if(sprintf('%.1F',1.0)!='1.0')
1246 $this->Error('This version of PHP is not supported');
1247 //Check availability of mbstring
1248 if(!function_exists('mb_strlen'))
1249 $this->Error('mbstring extension is not available');
1250 //Check mbstring overloading
1251 if(ini_get('mbstring.func_overload') & 2)
1252 $this->Error('mbstring overloading must be disabled');
1253 //Disable runtime magic quotes
1257 function _getpageformat($format)
1259 $format=strtolower($format);
1260 if(!isset($this->PageFormats[$format]))
1261 $this->Error('Unknown page format: '.$format);
1262 $a=$this->PageFormats[$format];
1263 return array($a[0]/$this->k, $a[1]/$this->k);
1266 function _getfontpath()
1268 if(!defined('FPDF_FONTPATH') && is_dir(dirname(__FILE__).'/font'))
1269 define('FPDF_FONTPATH',dirname(__FILE__).'/font/');
1270 return defined('FPDF_FONTPATH') ? FPDF_FONTPATH : '';
1273 function _beginpage($orientation, $format)
1276 $this->pages[$this->page]='';
1278 $this->x=$this->lMargin;
1279 $this->y=$this->tMargin;
1280 $this->FontFamily='';
1282 if($orientation=='')
1283 $orientation=$this->DefOrientation;
1285 $orientation=strtoupper($orientation[0]);
1287 $format=$this->DefPageFormat;
1290 if(is_string($format))
1291 $format=$this->_getpageformat($format);
1293 if($orientation!=$this->CurOrientation || $format[0]!=$this->CurPageFormat[0] || $format[1]!=$this->CurPageFormat[1])
1296 if($orientation=='P')
1298 $this->w=$format[0];
1299 $this->h=$format[1];
1303 $this->w=$format[1];
1304 $this->h=$format[0];
1306 $this->wPt=$this->w*$this->k;
1307 $this->hPt=$this->h*$this->k;
1308 $this->PageBreakTrigger=$this->h-$this->bMargin;
1309 $this->CurOrientation=$orientation;
1310 $this->CurPageFormat=$format;
1312 if($orientation!=$this->DefOrientation || $format[0]!=$this->DefPageFormat[0] || $format[1]!=$this->DefPageFormat[1])
1313 $this->PageSizes[$this->page]=array($this->wPt, $this->hPt);
1321 function _escape($s)
1323 //Escape special characters in strings
1324 $s=str_replace('\\','\\\\',$s);
1325 $s=str_replace('(','\\(',$s);
1326 $s=str_replace(')','\\)',$s);
1327 $s=str_replace("\r",'\\r',$s);
1331 function _textstring($s)
1333 //Format a text string
1334 return '('.$this->_escape($s).')';
1337 function _UTF8toUTF16($s)
1339 //Convert UTF-8 to UTF-16BE with BOM
1351 $res.=chr((($c1 & 0x0F)<<4) + (($c2 & 0x3C)>>2));
1352 $res.=chr((($c2 & 0x03)<<6) + ($c3 & 0x3F));
1358 $res.=chr(($c1 & 0x1C)>>2);
1359 $res.=chr((($c1 & 0x03)<<6) + ($c2 & 0x3F));
1363 //Single-byte character
1364 $res.="\0".chr($c1);
1370 function _dounderline($x, $y, $txt)
1373 $up=$this->CurrentFont['up'];
1374 $ut=$this->CurrentFont['ut'];
1375 $w=$this->GetStringWidth($txt)+$this->ws*substr_count($txt,' ');
1376 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);
1379 function _parsejpg($file)
1381 //Extract info from a JPEG file
1382 $a=GetImageSize($file);
1384 $this->Error('Missing or incorrect image file: '.$file);
1386 $this->Error('Not a JPEG file: '.$file);
1387 if(!isset($a['channels']) || $a['channels']==3)
1388 $colspace='DeviceRGB';
1389 elseif($a['channels']==4)
1390 $colspace='DeviceCMYK';
1392 $colspace='DeviceGray';
1393 $bpc=isset($a['bits']) ? $a['bits'] : 8;
1395 $f=fopen($file,'rb');
1398 $data.=fread($f,8192);
1400 return array('w'=>$a[0], 'h'=>$a[1], 'cs'=>$colspace, 'bpc'=>$bpc, 'f'=>'DCTDecode', 'data'=>$data);
1403 function _parsepng($file)
1405 //Extract info from a PNG file
1406 $f=fopen($file,'rb');
1408 $this->Error('Can\'t open image file: '.$file);
1410 if($this->_readstream($f,8)!=chr(137).'PNG'.chr(13).chr(10).chr(26).chr(10))
1411 $this->Error('Not a PNG file: '.$file);
1413 $this->_readstream($f,4);
1414 if($this->_readstream($f,4)!='IHDR')
1415 $this->Error('Incorrect PNG file: '.$file);
1416 $w=$this->_readint($f);
1417 $h=$this->_readint($f);
1418 $bpc=ord($this->_readstream($f,1));
1420 $this->Error('16-bit depth not supported: '.$file);
1421 $ct=ord($this->_readstream($f,1));
1423 $colspace='DeviceGray';
1425 $colspace='DeviceRGB';
1427 $colspace='Indexed';
1429 $this->Error('Alpha channel not supported: '.$file);
1430 if(ord($this->_readstream($f,1))!=0)
1431 $this->Error('Unknown compression method: '.$file);
1432 if(ord($this->_readstream($f,1))!=0)
1433 $this->Error('Unknown filter method: '.$file);
1434 if(ord($this->_readstream($f,1))!=0)
1435 $this->Error('Interlacing not supported: '.$file);
1436 $this->_readstream($f,4);
1437 $parms='/DecodeParms <</Predictor 15 /Colors '.($ct==2 ? 3 : 1).' /BitsPerComponent '.$bpc.' /Columns '.$w.'>>';
1438 //Scan chunks looking for palette, transparency and image data
1444 $n=$this->_readint($f);
1445 $type=$this->_readstream($f,4);
1449 $pal=$this->_readstream($f,$n);
1450 $this->_readstream($f,4);
1452 elseif($type=='tRNS')
1454 //Read transparency info
1455 $t=$this->_readstream($f,$n);
1457 $trns=array(ord(substr($t,1,1)));
1459 $trns=array(ord(substr($t,1,1)), ord(substr($t,3,1)), ord(substr($t,5,1)));
1462 $pos=strpos($t,chr(0));
1466 $this->_readstream($f,4);
1468 elseif($type=='IDAT')
1470 //Read image data block
1471 $data.=$this->_readstream($f,$n);
1472 $this->_readstream($f,4);
1474 elseif($type=='IEND')
1477 $this->_readstream($f,$n+4);
1480 if($colspace=='Indexed' && empty($pal))
1481 $this->Error('Missing palette in '.$file);
1483 return array('w'=>$w, 'h'=>$h, 'cs'=>$colspace, 'bpc'=>$bpc, 'f'=>'FlateDecode', 'parms'=>$parms, 'pal'=>$pal, 'trns'=>$trns, 'data'=>$data);
1486 function _readstream($f, $n)
1488 //Read n bytes from stream
1490 while($n>0 && !feof($f))
1494 $this->Error('Error while reading stream');
1499 $this->Error('Unexpected end of stream');
1503 function _readint($f)
1505 //Read a 4-byte integer from stream
1506 $a=unpack('Ni',$this->_readstream($f,4));
1510 function _parsegif($file)
1512 //Extract info from a GIF file (via PNG conversion)
1513 if(!function_exists('imagepng'))
1514 $this->Error('GD extension is required for GIF support');
1515 if(!function_exists('imagecreatefromgif'))
1516 $this->Error('GD has no GIF read support');
1517 $im=imagecreatefromgif($file);
1519 $this->Error('Missing or incorrect image file: '.$file);
1520 imageinterlace($im,0);
1521 $tmp=tempnam('.','gif');
1523 $this->Error('Unable to create a temporary file');
1524 if(!imagepng($im,$tmp))
1525 $this->Error('Error while saving to temporary file');
1527 $info=$this->_parsepng($tmp);
1534 //Begin a new object
1536 $this->offsets[$this->n]=strlen($this->buffer);
1537 $this->_out($this->n.' 0 obj');
1540 function _putstream($s)
1542 $this->_out('stream');
1544 $this->_out('endstream');
1549 //Add a line to the document
1551 $this->pages[$this->page].=$s."\n";
1553 $this->buffer.=$s."\n";
1556 function _putpages()
1559 if(!empty($this->AliasNbPages))
1561 //Replace number of pages in fonts using subsets
1564 for($i=0;$i<strlen($nstr);$i++) {
1565 $r .= sprintf("%02s", strtoupper(dechex(intval($nstr[$i])+48)));
1567 for($n=1;$n<=$nb;$n++)
1568 $this->pages[$n]=str_replace('`'.$this->AliasNbPages.'`',$r,$this->pages[$n]);
1569 // Now repeat for no pages in non-subset fonts
1571 //Replace number of pages
1572 for($n=1;$n<=$nb;$n++)
1573 $this->pages[$n]=str_replace($this->AliasNbPages,$r,$this->pages[$n]);
1575 if($this->DefOrientation=='P')
1577 $wPt=$this->DefPageFormat[0]*$this->k;
1578 $hPt=$this->DefPageFormat[1]*$this->k;
1582 $wPt=$this->DefPageFormat[1]*$this->k;
1583 $hPt=$this->DefPageFormat[0]*$this->k;
1585 $filter=($this->compress) ? '/Filter /FlateDecode ' : '';
1586 for($n=1;$n<=$nb;$n++)
1590 $this->_out('<</Type /Page');
1591 $this->_out('/Parent 1 0 R');
1592 if(isset($this->PageSizes[$n]))
1593 $this->_out(sprintf('/MediaBox [0 0 %.2F %.2F]',$this->PageSizes[$n][0],$this->PageSizes[$n][1]));
1594 $this->_out('/Resources 2 0 R');
1595 if(isset($this->PageLinks[$n]))
1598 $annots='/Annots [';
1599 foreach($this->PageLinks[$n] as $pl)
1601 $rect=sprintf('%.2F %.2F %.2F %.2F',$pl[0],$pl[1],$pl[0]+$pl[2],$pl[1]-$pl[3]);
1602 $annots.='<</Type /Annot /Subtype /Link /Rect ['.$rect.'] /Border [0 0 0] ';
1603 if(is_string($pl[4]))
1604 $annots.='/A <</S /URI /URI '.$this->_textstring($pl[4]).'>>>>';
1607 $l=$this->links[$pl[4]];
1608 $h=isset($this->PageSizes[$l[0]]) ? $this->PageSizes[$l[0]][1] : $hPt;
1609 $annots.=sprintf('/Dest [%d 0 R /XYZ 0 %.2F null]>>',1+2*$l[0],$h-$l[1]*$this->k);
1612 $this->_out($annots.']');
1614 $this->_out('/Contents '.($this->n+1).' 0 R>>');
1615 $this->_out('endobj');
1617 $p=($this->compress) ? gzcompress($this->pages[$n]) : $this->pages[$n];
1619 $this->_out('<<'.$filter.'/Length '.strlen($p).'>>');
1620 $this->_putstream($p);
1621 $this->_out('endobj');
1624 $this->offsets[1]=strlen($this->buffer);
1625 $this->_out('1 0 obj');
1626 $this->_out('<</Type /Pages');
1628 for($i=0;$i<$nb;$i++)
1629 $kids.=(3+2*$i).' 0 R ';
1630 $this->_out($kids.']');
1631 $this->_out('/Count '.$nb);
1632 $this->_out(sprintf('/MediaBox [0 0 %.2F %.2F]',$wPt,$hPt));
1634 $this->_out('endobj');
1637 function _putfonts()
1640 foreach($this->diffs as $diff)
1644 $this->_out('<</Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences ['.$diff.']>>');
1645 $this->_out('endobj');
1647 foreach($this->FontFiles as $file=>$info)
1649 if (!isset($info['type']) || $info['type']!='TrueTypesubset') {
1650 //Font file embedding
1652 $this->FontFiles[$file]['n']=$this->n;
1654 $f=fopen($this->_getfontpath().$file,'rb',1);
1656 $this->Error('Font file not found');
1658 $font.=fread($f,8192);
1660 $compressed=(substr($file,-2)=='.z');
1661 if(!$compressed && isset($info['length2']))
1663 $header=(ord($font[0])==128);
1666 //Strip first binary header
1667 $font=substr($font,6);
1669 if($header && ord($font[$info['length1']])==128)
1671 //Strip second binary header
1672 $font=substr($font,0,$info['length1']).substr($font,$info['length1']+6);
1675 $this->_out('<</Length '.strlen($font));
1677 $this->_out('/Filter /FlateDecode');
1678 $this->_out('/Length1 '.$info['length1']);
1679 if(isset($info['length2']))
1680 $this->_out('/Length2 '.$info['length2'].' /Length3 0');
1682 $this->_putstream($font);
1683 $this->_out('endobj');
1686 foreach($this->fonts as $k=>$font)
1689 //$this->fonts[$k]['n']=$this->n+1;
1690 $type=$font['type'];
1691 $name=$font['name'];
1695 $this->fonts[$k]['n']=$this->n+1;
1697 $this->_out('<</Type /Font');
1698 $this->_out('/BaseFont /'.$name);
1699 $this->_out('/Subtype /Type1');
1700 if($name!='Symbol' && $name!='ZapfDingbats')
1701 $this->_out('/Encoding /WinAnsiEncoding');
1703 $this->_out('endobj');
1705 elseif($type=='Type1' || $type=='TrueType')
1707 //Additional Type1 or TrueType font
1708 $this->fonts[$k]['n']=$this->n+1;
1710 $this->_out('<</Type /Font');
1711 $this->_out('/BaseFont /'.$name);
1712 $this->_out('/Subtype /'.$type);
1713 $this->_out('/FirstChar 32 /LastChar 255');
1714 $this->_out('/Widths '.($this->n+1).' 0 R');
1715 $this->_out('/FontDescriptor '.($this->n+2).' 0 R');
1718 if(isset($font['diff']))
1719 $this->_out('/Encoding '.($nf+$font['diff']).' 0 R');
1721 $this->_out('/Encoding /WinAnsiEncoding');
1724 $this->_out('endobj');
1729 for($i=32;$i<=255;$i++)
1730 $s.=$cw[chr($i)].' ';
1731 $this->_out($s.']');
1732 $this->_out('endobj');
1735 $s='<</Type /FontDescriptor /FontName /'.$name;
1736 foreach($font['desc'] as $k=>$v)
1738 $file=$font['file'];
1740 $s.=' /FontFile'.($type=='Type1' ? '' : '2').' '.$this->FontFiles[$file]['n'].' 0 R';
1741 $this->_out($s.'>>');
1742 $this->_out('endobj');
1744 // TrueType embedded SUBSETS
1745 else if ($type=='TrueTypesubset') {
1747 require_once(dirname(__FILE__).'/font/unifont/ttfonts.php');
1748 $ttf = new TTFontFile();
1749 $ttf->getMetrics($font['file'], 1);
1750 for($sfid=0;$sfid<count($font['subsetfontids']);$sfid++) {
1751 $this->fonts[$k]['n'][$sfid]=$this->n+1; // NB an array for subset
1752 $subsetname = 'MPDFA'.$ssfaid.'+'.$font['name'];
1754 $subset = $font['subsets'][$sfid];
1756 $ttfontstream = $ttf->makeSubset($subset);
1757 $ttfontsize = strlen($ttfontstream);
1758 $fontstream = gzcompress($ttfontstream);
1761 foreach($font['subsets'][$sfid] AS $cp=>$u) {
1762 if (isset($font['cw'][$u])) {
1763 $widthstring .= $font['cw'][$u].' ';
1766 $widthstring .= $ttf->defaultWidth.' ';
1768 $toUnistring .= sprintf("<%02s> <%04s>\n", strtoupper(dechex($cp)), strtoupper(dechex($u)));
1771 //Additional Type1 or TrueType font
1773 $this->_out('<</Type /Font');
1774 $this->_out('/BaseFont /'.$subsetname);
1775 $this->_out('/Subtype /TrueType');
1776 $this->_out('/FirstChar 0 /LastChar '.(count($font['subsets'][$sfid])));
1777 $this->_out('/Widths '.($this->n+1).' 0 R');
1778 $this->_out('/FontDescriptor '.($this->n+2).' 0 R');
1779 $this->_out('/ToUnicode '.($this->n + 3).' 0 R');
1781 $this->_out('endobj');
1785 $this->_out('['.$widthstring.']');
1786 $this->_out('endobj');
1790 $s='<</Type /FontDescriptor /FontName /'.$subsetname."\n";
1791 foreach($font['desc'] as $kd=>$v) {
1792 if ($kd == 'Flags') { $v = $v | 4; $v = $v & ~32; }
1793 $s.=' /'.$kd.' '.$v."\n";
1796 $s.='/FontFile2 '.($this->n + 2).' 0 R';
1797 $this->_out($s.'>>');
1798 $this->_out('endobj');
1801 $toUni = "/CIDInit /ProcSet findresource begin\n";
1802 $toUni .= "12 dict begin\n";
1803 $toUni .= "begincmap\n";
1804 $toUni .= "/CIDSystemInfo\n";
1805 $toUni .= "<</Registry (Adobe)\n";
1806 $toUni .= "/Ordering (UCS)\n";
1807 $toUni .= "/Supplement 0\n";
1808 $toUni .= ">> def\n";
1809 $toUni .= "/CMapName /Adobe-Identity-UCS def\n";
1810 $toUni .= "/CMapType 2 def\n";
1811 $toUni .= "1 begincodespacerange\n";
1812 $toUni .= "<00> <FF>\n";
1813 $toUni .= "endcodespacerange\n";
1814 $toUni .= count($font['subsets'][$sfid])." beginbfchar\n";
1815 $toUni .= $toUnistring;
1816 $toUni .= "endbfchar\n";
1817 $toUni .= "endcmap\n";
1818 $toUni .= "CMapName currentdict /CMap defineresource pop\n";
1822 $this->_out('<</Length '.strlen($toUni).'>>');
1823 $this->_putstream($toUni);
1824 $this->_out('endobj');
1828 $this->_out('<</Length '.strlen($fontstream));
1829 $this->_out('/Filter /FlateDecode');
1830 $this->_out('/Length1 '.$ttfontsize);
1832 $this->_putstream($fontstream);
1833 $this->_out('endobj');
1839 //Allow for additional types
1840 $this->fonts[$k]['n']=$this->n+1;
1841 $mtd='_put'.strtolower($type);
1842 if(!method_exists($this,$mtd))
1843 $this->Error('Unsupported font type: '.$type);
1849 function _putimages()
1851 $filter=($this->compress) ? '/Filter /FlateDecode ' : '';
1852 reset($this->images);
1853 foreach($this->images as $file => $info)
1856 $this->images[$file]['n']=$this->n;
1857 $this->_out('<</Type /XObject');
1858 $this->_out('/Subtype /Image');
1859 $this->_out('/Width '.$info['w']);
1860 $this->_out('/Height '.$info['h']);
1861 if($info['cs']=='Indexed')
1862 $this->_out('/ColorSpace [/Indexed /DeviceRGB '.(strlen($info['pal'])/3-1).' '.($this->n+1).' 0 R]');
1865 $this->_out('/ColorSpace /'.$info['cs']);
1866 if($info['cs']=='DeviceCMYK')
1867 $this->_out('/Decode [1 0 1 0 1 0 1 0]');
1869 $this->_out('/BitsPerComponent '.$info['bpc']);
1870 if(isset($info['f']))
1871 $this->_out('/Filter /'.$info['f']);
1872 if(isset($info['parms']))
1873 $this->_out($info['parms']);
1874 if(isset($info['trns']) && is_array($info['trns']))
1877 for($i=0;$i<count($info['trns']);$i++)
1878 $trns.=$info['trns'][$i].' '.$info['trns'][$i].' ';
1879 $this->_out('/Mask ['.$trns.']');
1881 $this->_out('/Length '.strlen($info['data']).'>>');
1882 $this->_putstream($info['data']);
1883 unset($this->images[$file]['data']);
1884 $this->_out('endobj');
1886 if($info['cs']=='Indexed')
1889 $pal=($this->compress) ? gzcompress($info['pal']) : $info['pal'];
1890 $this->_out('<<'.$filter.'/Length '.strlen($pal).'>>');
1891 $this->_putstream($pal);
1892 $this->_out('endobj');
1897 function _putxobjectdict()
1899 foreach($this->images as $image)
1900 $this->_out('/I'.$image['i'].' '.$image['n'].' 0 R');
1903 function _putresourcedict()
1905 $this->_out('/ProcSet [/PDF /Text /ImageB /ImageC /ImageI]');
1906 $this->_out('/Font <<');
1907 foreach($this->fonts as $font) {
1908 if ($font['type']=='TrueTypesubset') {
1909 foreach($font['n'] AS $k => $fid) {
1910 $this->_out('/F'.$font['subsetfontids'][$k].' '.$font['n'][$k].' 0 R');
1914 $this->_out('/F'.$font['i'].' '.$font['n'].' 0 R');
1918 $this->_out('/XObject <<');
1919 $this->_putxobjectdict();
1923 function _putresources()
1926 $this->_putimages();
1927 //Resource dictionary
1928 $this->offsets[2]=strlen($this->buffer);
1929 $this->_out('2 0 obj');
1931 $this->_putresourcedict();
1933 $this->_out('endobj');
1938 $this->_out('/Producer '.$this->_textstring('tFPDF '.tFPDF_VERSION));
1939 if(!empty($this->title))
1940 $this->_out('/Title '.$this->_textstring($this->title));
1941 if(!empty($this->subject))
1942 $this->_out('/Subject '.$this->_textstring($this->subject));
1943 if(!empty($this->author))
1944 $this->_out('/Author '.$this->_textstring($this->author));
1945 if(!empty($this->keywords))
1946 $this->_out('/Keywords '.$this->_textstring($this->keywords));
1947 if(!empty($this->creator))
1948 $this->_out('/Creator '.$this->_textstring($this->creator));
1949 $this->_out('/CreationDate '.$this->_textstring('D:'.@date('YmdHis')));
1952 function _putcatalog()
1954 $this->_out('/Type /Catalog');
1955 $this->_out('/Pages 1 0 R');
1956 if($this->ZoomMode=='fullpage')
1957 $this->_out('/OpenAction [3 0 R /Fit]');
1958 elseif($this->ZoomMode=='fullwidth')
1959 $this->_out('/OpenAction [3 0 R /FitH null]');
1960 elseif($this->ZoomMode=='real')
1961 $this->_out('/OpenAction [3 0 R /XYZ null null 1]');
1962 elseif(!is_string($this->ZoomMode))
1963 $this->_out('/OpenAction [3 0 R /XYZ null null '.($this->ZoomMode/100).']');
1964 if($this->LayoutMode=='single')
1965 $this->_out('/PageLayout /SinglePage');
1966 elseif($this->LayoutMode=='continuous')
1967 $this->_out('/PageLayout /OneColumn');
1968 elseif($this->LayoutMode=='two')
1969 $this->_out('/PageLayout /TwoColumnLeft');
1972 function _putheader()
1974 $this->_out('%PDF-'.$this->PDFVersion);
1977 function _puttrailer()
1979 $this->_out('/Size '.($this->n+1));
1980 $this->_out('/Root '.$this->n.' 0 R');
1981 $this->_out('/Info '.($this->n-1).' 0 R');
1986 $this->_putheader();
1988 $this->_putresources();
1994 $this->_out('endobj');
1998 $this->_putcatalog();
2000 $this->_out('endobj');
2002 $o=strlen($this->buffer);
2003 $this->_out('xref');
2004 $this->_out('0 '.($this->n+1));
2005 $this->_out('0000000000 65535 f ');
2006 for($i=1;$i<=$this->n;$i++)
2007 $this->_out(sprintf('%010d 00000 n ',$this->offsets[$i]));
2009 $this->_out('trailer');
2011 $this->_puttrailer();
2013 $this->_out('startxref');
2015 $this->_out('%%EOF');
2019 // ********* NEW FUNCTIONS *********
2021 // Convert utf-8 string to <HHHHHH> for Font Subsets
2022 function UTF8toSubset($str) {
2024 if ($this->AliasNbPages)
2025 $str = preg_replace('/'.preg_quote($this->AliasNbPages,'/').'/', chr(7), $str );
2026 $unicode = $this->UTF8StringToArray($str);
2027 $orig_fid = $this->CurrentFont['subsetfontids'][0];
2028 $last_fid = $this->CurrentFont['subsetfontids'][0];
2029 foreach($unicode as $c) {
2031 if ($orig_fid != $last_fid) {
2032 $ret .= '> Tj /F'.$orig_fid.' '.$this->FontSizePt.' Tf <';
2033 $last_fid = $orig_fid;
2035 $ret .= '`'.$this->AliasNbPages.'`';
2038 for ($i=0; $i<99; $i++) {
2039 // return c as decimal char
2040 $init = array_search($c, $this->CurrentFont['subsets'][$i]);
2041 if ($init!==false) {
2042 if ($this->CurrentFont['subsetfontids'][$i] != $last_fid) {
2043 $ret .= '> Tj /F'.$this->CurrentFont['subsetfontids'][$i].' '.$this->FontSizePt.' Tf <';
2044 $last_fid = $this->CurrentFont['subsetfontids'][$i];
2046 $ret .= sprintf("%02s", strtoupper(dechex($init)));
2049 else if (count($this->CurrentFont['subsets'][$i]) < 255) {
2050 $n = count($this->CurrentFont['subsets'][$i]);
2051 $this->CurrentFont['subsets'][$i][$n] = $c;
2052 if ($this->CurrentFont['subsetfontids'][$i] != $last_fid) {
2053 $ret .= '> Tj /F'.$this->CurrentFont['subsetfontids'][$i].' '.$this->FontSizePt.' Tf <';
2054 $last_fid = $this->CurrentFont['subsetfontids'][$i];
2056 $ret .= sprintf("%02s", strtoupper(dechex($n)));
2059 else if (!isset($this->CurrentFont['subsets'][($i+1)])) {
2060 $this->CurrentFont['subsets'][($i+1)] = array(0=>0);
2061 $new_fid = count($this->fonts)+$this->extraFontSubsets+1;
2062 $this->CurrentFont['subsetfontids'][($i+1)] = $new_fid;
2063 $this->extraFontSubsets++;
2068 if ($last_fid != $orig_fid) {
2069 $ret .= ' Tj /F'.$orig_fid.' '.$this->FontSizePt.' Tf <> ';
2074 // Converts UTF-8 strings to codepoints array
2075 function UTF8StringToArray($str) {
2077 $len = strlen($str);
2078 for ($i = 0; $i < $len; $i++) {
2082 elseif ( $h >= 0xC2 ) {
2083 if ( ($h <= 0xDF) && ($i < $len -1) )
2084 $out[] = ($h & 0x1F) << 6 | (ord($str[++$i]) & 0x3F);
2085 elseif ( ($h <= 0xEF) && ($i < $len -2) )
2086 $out[] = ($h & 0x0F) << 12 | (ord($str[++$i]) & 0x3F) << 6
2087 | (ord($str[++$i]) & 0x3F);
2088 elseif ( ($h <= 0xF4) && ($i < $len -3) )
2089 $out[] = ($h & 0x0F) << 18 | (ord($str[++$i]) & 0x3F) << 12
2090 | (ord($str[++$i]) & 0x3F) << 6
2091 | (ord($str[++$i]) & 0x3F);
2101 //Handle special IE contype request
2102 if(isset($_SERVER['HTTP_USER_AGENT']) && $_SERVER['HTTP_USER_AGENT']=='contype')
2104 header('Content-Type: application/pdf');