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;
277 trigger_error('<b>tFPDF error:</b> '.$msg);
294 $this->InFooter=true;
296 $this->InFooter=false;
303 function AddPage($orientation='', $format='')
308 $family=$this->FontFamily;
309 $style=$this->FontStyle.($this->underline ? 'U' : '');
310 $size=$this->FontSizePt;
311 $lw=$this->LineWidth;
312 $dc=$this->DrawColor;
313 $fc=$this->FillColor;
314 $tc=$this->TextColor;
315 $cf=$this->ColorFlag;
319 $this->InFooter=true;
321 $this->InFooter=false;
326 $this->_beginpage($orientation,$format);
327 //Set line cap style to square
330 $this->LineWidth=$lw;
331 $this->_out(sprintf('%.2F w',$lw*$this->k));
334 $this->SetFont($family,$style,$size);
336 $this->DrawColor=$dc;
339 $this->FillColor=$fc;
342 $this->TextColor=$tc;
343 $this->ColorFlag=$cf;
345 $this->InHeader=true;
347 $this->InHeader=false;
349 if($this->LineWidth!=$lw)
351 $this->LineWidth=$lw;
352 $this->_out(sprintf('%.2F w',$lw*$this->k));
356 $this->SetFont($family,$style,$size);
358 if($this->DrawColor!=$dc)
360 $this->DrawColor=$dc;
363 if($this->FillColor!=$fc)
365 $this->FillColor=$fc;
368 $this->TextColor=$tc;
369 $this->ColorFlag=$cf;
374 //To be implemented in your own inherited class
379 //To be implemented in your own inherited class
384 //Get current page number
388 function SetDrawColor($r, $g=null, $b=null)
390 //Set color for all stroking operations
391 if(($r==0 && $g==0 && $b==0) || $g===null)
392 $this->DrawColor=sprintf('%.3F G',$r/255);
394 $this->DrawColor=sprintf('%.3F %.3F %.3F RG',$r/255,$g/255,$b/255);
396 $this->_out($this->DrawColor);
399 function SetFillColor($r, $g=null, $b=null)
401 //Set color for all filling operations
402 if(($r==0 && $g==0 && $b==0) || $g===null)
403 $this->FillColor=sprintf('%.3F g',$r/255);
405 $this->FillColor=sprintf('%.3F %.3F %.3F rg',$r/255,$g/255,$b/255);
406 $this->ColorFlag=($this->FillColor!=$this->TextColor);
408 $this->_out($this->FillColor);
411 function SetTextColor($r, $g=null, $b=null)
414 if(($r==0 && $g==0 && $b==0) || $g===null)
415 $this->TextColor=sprintf('%.3F g',$r/255);
417 $this->TextColor=sprintf('%.3F %.3F %.3F rg',$r/255,$g/255,$b/255);
418 $this->ColorFlag=($this->FillColor!=$this->TextColor);
421 function GetStringWidth($s)
423 //Get width of a string in the current font
425 $cw=&$this->CurrentFont['cw'];
427 if ($this->unifontSubset) {
428 $unicode = $this->UTF8StringToArray($s);
429 foreach($unicode as $char) {
430 if (isset($cw[$char])) { $w+=$cw[$char]; }
431 else if($char>0 && $char<128 && isset($cw[chr($char)])) { $w+=$cw[chr($char)]; }
432 else if(isset($this->CurrentFont['desc']['MissingWidth'])) { $w += $this->CurrentFont['desc']['MissingWidth']; }
433 else if(isset($this->CurrentFont['MissingWidth'])) { $w += $this->CurrentFont['MissingWidth']; }
442 return $w*$this->FontSize/1000;
445 function SetLineWidth($width)
448 $this->LineWidth=$width;
450 $this->_out(sprintf('%.2F w',$width*$this->k));
453 function Line($x1, $y1, $x2, $y2)
456 $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));
459 function Rect($x, $y, $w, $h, $style='')
464 elseif($style=='FD' || $style=='DF')
468 $this->_out(sprintf('%.2F %.2F %.2F %.2F re %s',$x*$this->k,($this->h-$y)*$this->k,$w*$this->k,-$h*$this->k,$op));
472 function AddFont($family, $style='', $file='', $uni=false)
474 //Add a TrueType or Type1 font
475 $family=strtolower($family);
478 $style=strtoupper($style);
483 $file=str_replace(' ','',$family).strtolower($style).'.ttf';
486 $file=str_replace(' ','',$family).strtolower($style).'.php';
489 $fontkey=$family.$style;
490 if(isset($this->fonts[$fontkey]))
496 $ttfilename = substr($file,0,1) == '/' ? $file : dirname(__FILE__). '/font/unifont/'.$file ;
499 $filename =str_replace(' ','',$filename );
500 $filename =str_replace('-','',$filename );
501 $unifilename = dirname(__FILE__).'/fontunifont/'.strtolower(substr($filename ,0,(strpos($filename ,'.'))));
504 if (file_exists($unifilename.'.mtx.php')) {
505 include($unifilename.'.mtx.php');
507 if (!isset($type) || $type != "TrueTypesubset") {
508 require_once(dirname(__FILE__).'/font/unifont/ttfonts.php');
509 $ttf = new TTFontFile();
510 $ttf->getMetrics($ttfilename);
511 $cw = $ttf->charWidths;
512 $type = "TrueTypesubset";
513 $name = preg_replace('/ /','',$ttf->fullName);
514 $desc= array('Ascent'=>round($ttf->ascent),
515 'Descent'=>round($ttf->descent),
516 'CapHeight'=>round($ttf->capHeight),
517 'Flags'=>$ttf->flags,
518 'FontBBox'=>'['.round($ttf->bbox[0])." ".round($ttf->bbox[1])." ".round($ttf->bbox[2])." ".round($ttf->bbox[3]).']',
519 'ItalicAngle'=>$ttf->italicAngle,
520 'StemV'=>round($ttf->stemV),
521 'MissingWidth'=>round($ttf->defaultWidth));
522 $up = round($ttf->underlinePosition);
523 $ut = round($ttf->underlineThickness);
524 //Generate metrics .php file
526 $s.='$type=\''.$type."';\n";
527 $s.='$name=\''.$name."';\n";
528 $s.='$desc='.var_export($desc,true).";\n";
529 $s.='$up='.$up.";\n";
530 $s.='$ut='.$ut.";\n";
531 $s.='$cw='.var_export($cw,true).";\n";
533 if (is_writable($this->_getfontpath().'unifont')) {
534 $fh = fopen($unifilename.'.mtx.php',"w");
535 fwrite($fh,$s,strlen($s));
541 $this->Error('Problem with the font definition file');
543 $i = count(is_array($this->fonts) ? $this->fonts : array())+$this->extraFontSubsets+1;
544 $sbarr = range(0,127);
545 $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);
549 include($this->_getfontpath().$file);
551 $this->Error('Could not include font definition file');
552 $i=count($this->fonts)+$this->extraFontSubsets+1;
553 $this->fonts[$fontkey]=array('i'=>$i, 'type'=>$type, 'name'=>$name, 'desc'=>$desc, 'up'=>$up, 'ut'=>$ut, 'cw'=>$cw, 'enc'=>$enc, 'file'=>$file);
558 //Search existing encodings
560 $nb=count($this->diffs);
561 for($i=1;$i<=$nb;$i++)
563 if($this->diffs[$i]==$diff)
572 $this->diffs[$d]=$diff;
574 $this->fonts[$fontkey]['diff']=$d;
578 if($type=='TrueType')
579 $this->FontFiles[$file]=array('length1'=>$originalsize);
580 else if ($uni && $type == "TrueTypesubset")
581 $this->FontFiles[$file]=array('type'=>"TrueTypesubset");
583 $this->FontFiles[$file]=array('length1'=>$size1, 'length2'=>$size2);
587 function SetFont($family, $style='', $size=0)
589 //Select a font; size given in points
590 global $fpdf_charwidths;
592 $family=strtolower($family);
594 $family=$this->FontFamily;
597 elseif($family=='symbol' || $family=='zapfdingbats')
599 $style=strtoupper($style);
600 if(strpos($style,'U')!==false)
602 $this->underline=true;
603 $style=str_replace('U','',$style);
606 $this->underline=false;
610 $size=$this->FontSizePt;
611 //Test if font is already selected
612 if($this->FontFamily==$family && $this->FontStyle==$style && $this->FontSizePt==$size)
614 //Test if used for the first time
615 $fontkey=$family.$style;
616 if(!isset($this->fonts[$fontkey]))
618 //Check if one of the standard fonts
619 if(isset($this->CoreFonts[$fontkey]))
621 if(!isset($fpdf_charwidths[$fontkey]))
625 if($family=='times' || $family=='helvetica')
626 $file.=strtolower($style);
627 //var_dump($file); var_dump($this->_getfontpath());
628 include($this->_getfontpath().$file.'.php');
629 if(!isset($fpdf_charwidths[$fontkey]))
630 $this->Error('Could not include font metric file');
632 $i=count($this->fonts)+1+$this->extraFontSubsets;
633 $name=$this->CoreFonts[$fontkey];
634 $cw=$fpdf_charwidths[$fontkey];
635 $this->fonts[$fontkey]=array('i'=>$i, 'type'=>'core', 'name'=>$name, 'up'=>-100, 'ut'=>50, 'cw'=>$cw);
638 $this->Error('Undefined font aa: '.$family.' '.$style);
641 $this->FontFamily=$family;
642 $this->FontStyle=$style;
643 $this->FontSizePt=$size;
644 $this->FontSize=$size/$this->k;
645 $this->CurrentFont=&$this->fonts[$fontkey];
646 if ($this->fonts[$fontkey]['type']=='TrueTypesubset') { $this->unifontSubset = true; }
647 else { $this->unifontSubset = false; }
649 $this->_out(sprintf('BT /F%d %.2F Tf ET',$this->CurrentFont['i'],$this->FontSizePt));
652 function SetFontSize($size)
654 //Set font size in points
655 if($this->FontSizePt==$size)
657 $this->FontSizePt=$size;
658 $this->FontSize=$size/$this->k;
660 $this->_out(sprintf('BT /F%d %.2F Tf ET',$this->CurrentFont['i'],$this->FontSizePt));
665 //Create a new internal link
666 $n=count($this->links)+1;
667 $this->links[$n]=array(0, 0);
671 function SetLink($link, $y=0, $page=-1)
673 //Set destination of internal link
678 $this->links[$link]=array($page, $y);
681 function Link($x, $y, $w, $h, $link)
683 //Put a link on the page
684 $this->PageLinks[$this->page][]=array($x*$this->k, $this->hPt-$y*$this->k, $w*$this->k, $h*$this->k, $link);
687 function Text($x, $y, $txt)
690 if ($this->unifontSubset)
691 $txt2 = $this->UTF8toSubset($txt);
693 $txt2='('.$this->_escape($txt).')';
694 $s=sprintf('BT %.2F %.2F Td %s Tj ET',$x*$this->k,($this->h-$y)*$this->k,$txt2);
695 if($this->underline && $txt!='')
696 $s.=' '.$this->_dounderline($x,$y,$txt);
698 $s='q '.$this->TextColor.' '.$s.' Q';
702 function AcceptPageBreak()
704 //Accept automatic page break or not
705 return $this->AutoPageBreak;
708 function Cell($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link='')
712 if($this->y+$h>$this->PageBreakTrigger && !$this->InHeader && !$this->InFooter && $this->AcceptPageBreak())
714 //Automatic page break
722 $this->AddPage($this->CurOrientation,$this->CurPageFormat);
727 $this->_out(sprintf('%.3F Tw',$ws*$k));
731 $w=$this->w-$this->rMargin-$this->x;
733 if($fill || $border==1)
736 $op=($border==1) ? 'B' : 'f';
739 $s=sprintf('%.2F %.2F %.2F %.2F re %s ',$this->x*$k,($this->h-$this->y)*$k,$w*$k,-$h*$k,$op);
741 if(is_string($border))
745 if(strpos($border,'L')!==false)
746 $s.=sprintf('%.2F %.2F m %.2F %.2F l S ',$x*$k,($this->h-$y)*$k,$x*$k,($this->h-($y+$h))*$k);
747 if(strpos($border,'T')!==false)
748 $s.=sprintf('%.2F %.2F m %.2F %.2F l S ',$x*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-$y)*$k);
749 if(strpos($border,'R')!==false)
750 $s.=sprintf('%.2F %.2F m %.2F %.2F l S ',($x+$w)*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
751 if(strpos($border,'B')!==false)
752 $s.=sprintf('%.2F %.2F m %.2F %.2F l S ',$x*$k,($this->h-($y+$h))*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
757 $dx=$w-$this->cMargin-$this->GetStringWidth($txt);
759 $dx=($w-$this->GetStringWidth($txt))/2;
763 $s.='q '.$this->TextColor.' ';
764 if ($this->unifontSubset)
765 $txt2 = $this->UTF8toSubset($txt);
767 $txt2='('.str_replace(')','\\)',str_replace('(','\\(',str_replace('\\','\\\\',$txt))).')';
768 $s.=sprintf('BT %.2F %.2F Td %s Tj ET',($this->x+$dx)*$k,($this->h-($this->y+.5*$h+.3*$this->FontSize))*$k,$txt2);
770 $s.=' '.$this->_dounderline($this->x+$dx,$this->y+.5*$h+.3*$this->FontSize,$txt);
774 $this->Link($this->x+$dx,$this->y+.5*$h-.5*$this->FontSize,$this->GetStringWidth($txt),$this->FontSize,$link);
784 $this->x=$this->lMargin;
790 function MultiCell($w, $h, $txt, $border=0, $align='J', $fill=false)
792 //Output text with automatic or explicit line breaks
793 $cw=&$this->CurrentFont['cw'];
795 $w=$this->w-$this->rMargin-$this->x;
796 $wmax=($w-2*$this->cMargin);
797 $s=str_replace("\r",'',$txt);
798 if ($this->unifontSubset) {
799 $nb=mb_strlen($s, 'utf-8');
800 while($nb>0 && mb_substr($s,$nb-1,1,'utf-8')=="\n") $nb--;
804 if($nb>0 && $s[$nb-1]=="\n")
820 if(strpos($border,'L')!==false)
822 if(strpos($border,'R')!==false)
824 $b=(strpos($border,'T')!==false) ? $b2.'T' : $b2;
836 if ($this->unifontSubset) {
837 $c = mb_substr($s,$i,1,'UTF-8');
843 //Explicit line break
849 if ($this->unifontSubset) {
850 $this->Cell($w,$h,mb_substr($s,$j,$i-$j,'UTF-8'),$b,2,$align,$fill);
853 $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
861 if($border && $nl==2)
872 if ($this->unifontSubset) { $l += $this->GetStringWidth($c); }
873 else { $l += $cw[$c]*$this->FontSize/1000; }
877 //Automatic line break
887 if ($this->unifontSubset) {
888 $this->Cell($w,$h,mb_substr($s,$j,$i-$j,'UTF-8'),$b,2,$align,$fill);
891 $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
898 $this->ws=($ns>1) ? ($wmax-$ls)/($ns-1) : 0;
899 $this->_out(sprintf('%.3F Tw',$this->ws*$this->k));
901 if ($this->unifontSubset) {
902 $this->Cell($w,$h,mb_substr($s,$j,$sep-$j,'UTF-8'),$b,2,$align,$fill);
905 $this->Cell($w,$h,substr($s,$j,$sep-$j),$b,2,$align,$fill);
914 if($border && $nl==2)
926 if($border && strpos($border,'B')!==false)
928 if ($this->unifontSubset) {
929 $this->Cell($w,$h,mb_substr($s,$j,$i-$j,'UTF-8'),$b,2,$align,$fill);
932 $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
934 $this->x=$this->lMargin;
937 function Write($h, $txt, $link='')
939 //Output text in flowing mode
940 $cw=&$this->CurrentFont['cw'];
941 $w=$this->w-$this->rMargin-$this->x;
943 $wmax=($w-2*$this->cMargin);
944 $s=str_replace("\r",'',$txt);
945 if ($this->unifontSubset) {
946 $nb=mb_strlen($s, 'UTF-8');
947 if($nb==1 && $s==" ") {
948 $this->x += $this->GetStringWidth($s);
965 if ($this->unifontSubset) {
966 $c = mb_substr($s,$i,1,'UTF-8');
972 //Explicit line break
973 if ($this->unifontSubset) {
974 $this->Cell($w,$h,mb_substr($s,$j,$i-$j,'UTF-8'),0,2,'',0,$link);
977 $this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link);
985 $this->x=$this->lMargin;
986 $w=$this->w-$this->rMargin-$this->x;
987 $wmax=($w-2*$this->cMargin);
995 if ($this->unifontSubset) { $l += $this->GetStringWidth($c); }
996 else { $l += $cw[$c]*$this->FontSize/1000; }
1000 //Automatic line break
1003 if($this->x>$this->lMargin)
1006 $this->x=$this->lMargin;
1008 $w=$this->w-$this->rMargin-$this->x;
1009 $wmax=($w-2*$this->cMargin);
1016 if ($this->unifontSubset) {
1017 $this->Cell($w,$h,mb_substr($s,$j,$i-$j,'UTF-8'),0,2,'',0,$link);
1020 $this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link);
1025 if ($this->unifontSubset) {
1026 $this->Cell($w,$h,mb_substr($s,$j,$sep-$j,'UTF-8'),0,2,'',0,$link);
1029 $this->Cell($w,$h,substr($s,$j,$sep-$j),0,2,'',0,$link);
1038 $this->x=$this->lMargin;
1039 $w=$this->w-$this->rMargin-$this->x;
1040 $wmax=($w-2*$this->cMargin);
1049 if ($this->unifontSubset) {
1050 $this->Cell($l,$h,mb_substr($s,$j,$i-$j,'UTF-8'),0,0,'',0,$link);
1053 $this->Cell($l,$h,substr($s,$j),0,0,'',0,$link);
1058 function Ln($h=null)
1060 //Line feed; default value is last cell height
1061 $this->x=$this->lMargin;
1063 $this->y+=$this->lasth;
1068 function Image($file, $x=null, $y=null, $w=0, $h=0, $type='', $link='')
1070 //Put an image on the page
1071 if(!isset($this->images[$file]))
1073 //First use of this image, get info
1076 $pos=strrpos($file,'.');
1078 $this->Error('Image file has no extension and no type was specified: '.$file);
1079 $type=substr($file,$pos+1);
1081 $type=strtolower($type);
1084 $mtd='_parse'.$type;
1085 if(!method_exists($this,$mtd))
1086 $this->Error('Unsupported image type: '.$type);
1087 $info=$this->$mtd($file);
1088 $info['i']=count($this->images)+1;
1089 $this->images[$file]=$info;
1092 $info=$this->images[$file];
1093 //Automatic width and height calculation if needed
1096 //Put image at 72 dpi
1097 $w=$info['w']/$this->k;
1098 $h=$info['h']/$this->k;
1101 $w=$h*$info['w']/$info['h'];
1103 $h=$w*$info['h']/$info['w'];
1107 if($this->y+$h>$this->PageBreakTrigger && !$this->InHeader && !$this->InFooter && $this->AcceptPageBreak())
1109 //Automatic page break
1111 $this->AddPage($this->CurOrientation,$this->CurPageFormat);
1119 $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']));
1121 $this->Link($x,$y,$w,$h,$link);
1136 $this->x=$this->w+$x;
1147 //Set y position and reset x
1148 $this->x=$this->lMargin;
1152 $this->y=$this->h+$y;
1155 function SetXY($x, $y)
1157 //Set x and y positions
1162 function Output($name='', $dest='')
1164 //Output PDF to some destination
1167 $dest=strtoupper($dest);
1181 //Send to standard output
1183 $this->Error('Some data has already been output, can\'t send PDF file');
1184 if(php_sapi_name()!='cli')
1186 //We send to a browser
1187 header('Content-Type: application/pdf');
1189 $this->Error('Some data has already been output, can\'t send PDF file');
1190 header('Content-Length: '.strlen($this->buffer));
1191 header('Content-Disposition: inline; filename="'.$name.'"');
1192 header('Cache-Control: private, max-age=0, must-revalidate');
1193 header('Pragma: public');
1194 ini_set('zlib.output_compression','0');
1201 $this->Error('Some data has already been output, can\'t send PDF file');
1202 header('Content-Type: application/x-download');
1204 $this->Error('Some data has already been output, can\'t send PDF file');
1205 header('Content-Length: '.strlen($this->buffer));
1206 header('Content-Disposition: attachment; filename="'.$name.'"');
1207 header('Cache-Control: private, max-age=0, must-revalidate');
1208 header('Pragma: public');
1209 ini_set('zlib.output_compression','0');
1213 //Save to local file
1214 $f=fopen($name,'wb');
1216 $this->Error('Unable to create output file: '.$name);
1217 fwrite($f,$this->buffer,strlen($this->buffer));
1221 //Return as a string
1222 return $this->buffer;
1224 $this->Error('Incorrect output destination: '.$dest);
1229 /*******************************************************************************
1231 * Protected methods *
1233 *******************************************************************************/
1234 function _dochecks()
1236 //Check availability of %F
1237 if(sprintf('%.1F',1.0)!='1.0')
1238 $this->Error('This version of PHP is not supported');
1239 //Check availability of mbstring
1240 if(!function_exists('mb_strlen'))
1241 $this->Error('mbstring extension is not available');
1242 //Check mbstring overloading
1243 if(ini_get('mbstring.func_overload') & 2)
1244 $this->Error('mbstring overloading must be disabled');
1245 //Disable runtime magic quotes
1249 function _getpageformat($format)
1251 $format=strtolower($format);
1252 if(!isset($this->PageFormats[$format]))
1253 $this->Error('Unknown page format: '.$format);
1254 $a=$this->PageFormats[$format];
1255 return array($a[0]/$this->k, $a[1]/$this->k);
1258 function _getfontpath()
1260 if(!defined('FPDF_FONTPATH') && is_dir(dirname(__FILE__).'/font'))
1261 define('FPDF_FONTPATH',dirname(__FILE__).'/font/');
1262 return defined('FPDF_FONTPATH') ? FPDF_FONTPATH : '';
1265 function _beginpage($orientation, $format)
1268 $this->pages[$this->page]='';
1270 $this->x=$this->lMargin;
1271 $this->y=$this->tMargin;
1272 $this->FontFamily='';
1274 if($orientation=='') {
1275 $orientation=$this->DefOrientation;
1277 $orientation=strtoupper($orientation[0]);
1280 $format=$this->DefPageFormat;
1282 if(is_string($format)) {
1283 $format=$this->_getpageformat($format);
1286 if($orientation!=$this->CurOrientation || empty($format) || $format[0] !=$this->CurPageFormat[0] || $format[1]!=$this->CurPageFormat[1])
1289 if($orientation=='P')
1291 $this->w=$format[0];
1292 $this->h=$format[1];
1296 $this->w= $format[1];
1297 $this->h= $format[0];
1299 $this->wPt=$this->w*$this->k;
1300 $this->hPt=$this->h*$this->k;
1301 $this->PageBreakTrigger=$this->h-$this->bMargin;
1302 $this->CurOrientation=$orientation;
1303 $this->CurPageFormat=$format;
1305 if($orientation!=$this->DefOrientation || $format[0]!=$this->DefPageFormat[0] || $format[1]!=$this->DefPageFormat[1])
1306 $this->PageSizes[$this->page]=array($this->wPt, $this->hPt);
1314 function _escape($s)
1316 //Escape special characters in strings
1317 $s=str_replace('\\','\\\\',$s);
1318 $s=str_replace('(','\\(',$s);
1319 $s=str_replace(')','\\)',$s);
1320 $s=str_replace("\r",'\\r',$s);
1324 function _textstring($s)
1326 //Format a text string
1327 return '('.$this->_escape($s).')';
1330 function _UTF8toUTF16($s)
1332 //Convert UTF-8 to UTF-16BE with BOM
1344 $res.=chr((($c1 & 0x0F)<<4) + (($c2 & 0x3C)>>2));
1345 $res.=chr((($c2 & 0x03)<<6) + ($c3 & 0x3F));
1351 $res.=chr(($c1 & 0x1C)>>2);
1352 $res.=chr((($c1 & 0x03)<<6) + ($c2 & 0x3F));
1356 //Single-byte character
1357 $res.="\0".chr($c1);
1363 function _dounderline($x, $y, $txt)
1366 $up=$this->CurrentFont['up'];
1367 $ut=$this->CurrentFont['ut'];
1368 $w=$this->GetStringWidth($txt)+$this->ws*substr_count($txt,' ');
1369 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);
1372 function _parsejpg($file)
1374 //Extract info from a JPEG file
1375 $a=GetImageSize($file);
1377 $this->Error('Missing or incorrect image file: '.$file);
1379 $this->Error('Not a JPEG file: '.$file);
1380 if(!isset($a['channels']) || $a['channels']==3)
1381 $colspace='DeviceRGB';
1382 elseif($a['channels']==4)
1383 $colspace='DeviceCMYK';
1385 $colspace='DeviceGray';
1386 $bpc=isset($a['bits']) ? $a['bits'] : 8;
1388 $f=fopen($file,'rb');
1391 $data.=fread($f,8192);
1393 return array('w'=>$a[0], 'h'=>$a[1], 'cs'=>$colspace, 'bpc'=>$bpc, 'f'=>'DCTDecode', 'data'=>$data);
1396 function _parsepng($file)
1398 //Extract info from a PNG file
1399 $f=fopen($file,'rb');
1401 $this->Error('Can\'t open image file: '.$file);
1403 if($this->_readstream($f,8)!=chr(137).'PNG'.chr(13).chr(10).chr(26).chr(10))
1404 $this->Error('Not a PNG file: '.$file);
1406 $this->_readstream($f,4);
1407 if($this->_readstream($f,4)!='IHDR')
1408 $this->Error('Incorrect PNG file: '.$file);
1409 $w=$this->_readint($f);
1410 $h=$this->_readint($f);
1411 $bpc=ord($this->_readstream($f,1));
1413 $this->Error('16-bit depth not supported: '.$file);
1414 $ct=ord($this->_readstream($f,1));
1416 $colspace='DeviceGray';
1418 $colspace='DeviceRGB';
1420 $colspace='Indexed';
1422 $this->Error('Alpha channel not supported: '.$file);
1423 if(ord($this->_readstream($f,1))!=0)
1424 $this->Error('Unknown compression method: '.$file);
1425 if(ord($this->_readstream($f,1))!=0)
1426 $this->Error('Unknown filter method: '.$file);
1427 if(ord($this->_readstream($f,1))!=0)
1428 $this->Error('Interlacing not supported: '.$file);
1429 $this->_readstream($f,4);
1430 $parms='/DecodeParms <</Predictor 15 /Colors '.($ct==2 ? 3 : 1).' /BitsPerComponent '.$bpc.' /Columns '.$w.'>>';
1431 //Scan chunks looking for palette, transparency and image data
1437 $n=$this->_readint($f);
1438 $type=$this->_readstream($f,4);
1442 $pal=$this->_readstream($f,$n);
1443 $this->_readstream($f,4);
1445 elseif($type=='tRNS')
1447 //Read transparency info
1448 $t=$this->_readstream($f,$n);
1450 $trns=array(ord(substr($t,1,1)));
1452 $trns=array(ord(substr($t,1,1)), ord(substr($t,3,1)), ord(substr($t,5,1)));
1455 $pos=strpos($t,chr(0));
1459 $this->_readstream($f,4);
1461 elseif($type=='IDAT')
1463 //Read image data block
1464 $data.=$this->_readstream($f,$n);
1465 $this->_readstream($f,4);
1467 elseif($type=='IEND')
1470 $this->_readstream($f,$n+4);
1473 if($colspace=='Indexed' && empty($pal))
1474 $this->Error('Missing palette in '.$file);
1476 return array('w'=>$w, 'h'=>$h, 'cs'=>$colspace, 'bpc'=>$bpc, 'f'=>'FlateDecode', 'parms'=>$parms, 'pal'=>$pal, 'trns'=>$trns, 'data'=>$data);
1479 function _readstream($f, $n)
1481 //Read n bytes from stream
1483 while($n>0 && !feof($f))
1487 $this->Error('Error while reading stream');
1492 $this->Error('Unexpected end of stream');
1496 function _readint($f)
1498 //Read a 4-byte integer from stream
1499 $a=unpack('Ni',$this->_readstream($f,4));
1503 function _parsegif($file)
1505 //Extract info from a GIF file (via PNG conversion)
1506 if(!function_exists('imagepng'))
1507 $this->Error('GD extension is required for GIF support');
1508 if(!function_exists('imagecreatefromgif'))
1509 $this->Error('GD has no GIF read support');
1510 $im=imagecreatefromgif($file);
1512 $this->Error('Missing or incorrect image file: '.$file);
1513 imageinterlace($im,0);
1514 $tmp=tempnam('.','gif');
1516 $this->Error('Unable to create a temporary file');
1517 if(!imagepng($im,$tmp))
1518 $this->Error('Error while saving to temporary file');
1520 $info=$this->_parsepng($tmp);
1527 //Begin a new object
1529 $this->offsets[$this->n]=strlen($this->buffer);
1530 $this->_out($this->n.' 0 obj');
1533 function _putstream($s)
1535 $this->_out('stream');
1537 $this->_out('endstream');
1542 //Add a line to the document
1544 $this->pages[$this->page].=$s."\n";
1546 $this->buffer.=$s."\n";
1549 function _putpages()
1552 if(!empty($this->AliasNbPages))
1554 //Replace number of pages in fonts using subsets
1557 for($i=0;$i<strlen($nstr);$i++) {
1558 $r .= sprintf("%02s", strtoupper(dechex(intval($nstr[$i])+48)));
1560 for($n=1;$n<=$nb;$n++)
1561 $this->pages[$n]=str_replace('`'.$this->AliasNbPages.'`',$r,$this->pages[$n]);
1562 // Now repeat for no pages in non-subset fonts
1564 //Replace number of pages
1565 for($n=1;$n<=$nb;$n++)
1566 $this->pages[$n]=str_replace($this->AliasNbPages,$r,$this->pages[$n]);
1568 if($this->DefOrientation=='P')
1570 $wPt=$this->DefPageFormat[0]*$this->k;
1571 $hPt=$this->DefPageFormat[1]*$this->k;
1575 $wPt=$this->DefPageFormat[1]*$this->k;
1576 $hPt=$this->DefPageFormat[0]*$this->k;
1578 $filter=($this->compress) ? '/Filter /FlateDecode ' : '';
1579 for($n=1;$n<=$nb;$n++)
1583 $this->_out('<</Type /Page');
1584 $this->_out('/Parent 1 0 R');
1585 if(isset($this->PageSizes[$n]))
1586 $this->_out(sprintf('/MediaBox [0 0 %.2F %.2F]',$this->PageSizes[$n][0],$this->PageSizes[$n][1]));
1587 $this->_out('/Resources 2 0 R');
1588 if(isset($this->PageLinks[$n]))
1591 $annots='/Annots [';
1592 foreach($this->PageLinks[$n] as $pl)
1594 $rect=sprintf('%.2F %.2F %.2F %.2F',$pl[0],$pl[1],$pl[0]+$pl[2],$pl[1]-$pl[3]);
1595 $annots.='<</Type /Annot /Subtype /Link /Rect ['.$rect.'] /Border [0 0 0] ';
1596 if(is_string($pl[4]))
1597 $annots.='/A <</S /URI /URI '.$this->_textstring($pl[4]).'>>>>';
1600 $l=$this->links[$pl[4]];
1601 $h=isset($this->PageSizes[$l[0]]) ? $this->PageSizes[$l[0]][1] : $hPt;
1602 $annots.=sprintf('/Dest [%d 0 R /XYZ 0 %.2F null]>>',1+2*$l[0],$h-$l[1]*$this->k);
1605 $this->_out($annots.']');
1607 $this->_out('/Contents '.($this->n+1).' 0 R>>');
1608 $this->_out('endobj');
1610 $p=($this->compress) ? gzcompress($this->pages[$n]) : $this->pages[$n];
1612 $this->_out('<<'.$filter.'/Length '.strlen($p).'>>');
1613 $this->_putstream($p);
1614 $this->_out('endobj');
1617 $this->offsets[1]=strlen($this->buffer);
1618 $this->_out('1 0 obj');
1619 $this->_out('<</Type /Pages');
1621 for($i=0;$i<$nb;$i++)
1622 $kids.=(3+2*$i).' 0 R ';
1623 $this->_out($kids.']');
1624 $this->_out('/Count '.$nb);
1625 $this->_out(sprintf('/MediaBox [0 0 %.2F %.2F]',$wPt,$hPt));
1627 $this->_out('endobj');
1630 function _putfonts()
1633 foreach($this->diffs as $diff)
1637 $this->_out('<</Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences ['.$diff.']>>');
1638 $this->_out('endobj');
1640 foreach($this->FontFiles as $file=>$info)
1642 if (!isset($info['type']) || $info['type']!='TrueTypesubset') {
1643 //Font file embedding
1645 $this->FontFiles[$file]['n']=$this->n;
1647 $f=fopen($this->_getfontpath().$file,'rb',1);
1649 $this->Error('Font file not found');
1651 $font.=fread($f,8192);
1653 $compressed=(substr($file,-2)=='.z');
1654 if(!$compressed && isset($info['length2']))
1656 $header=(ord($font[0])==128);
1659 //Strip first binary header
1660 $font=substr($font,6);
1662 if($header && ord($font[$info['length1']])==128)
1664 //Strip second binary header
1665 $font=substr($font,0,$info['length1']).substr($font,$info['length1']+6);
1668 $this->_out('<</Length '.strlen($font));
1670 $this->_out('/Filter /FlateDecode');
1671 $this->_out('/Length1 '.$info['length1']);
1672 if(isset($info['length2']))
1673 $this->_out('/Length2 '.$info['length2'].' /Length3 0');
1675 $this->_putstream($font);
1676 $this->_out('endobj');
1679 foreach($this->fonts as $k=>$font)
1682 //$this->fonts[$k]['n']=$this->n+1;
1683 $type=$font['type'];
1684 $name=$font['name'];
1688 $this->fonts[$k]['n']=$this->n+1;
1690 $this->_out('<</Type /Font');
1691 $this->_out('/BaseFont /'.$name);
1692 $this->_out('/Subtype /Type1');
1693 if($name!='Symbol' && $name!='ZapfDingbats')
1694 $this->_out('/Encoding /WinAnsiEncoding');
1696 $this->_out('endobj');
1698 elseif($type=='Type1' || $type=='TrueType')
1700 //Additional Type1 or TrueType font
1701 $this->fonts[$k]['n']=$this->n+1;
1703 $this->_out('<</Type /Font');
1704 $this->_out('/BaseFont /'.$name);
1705 $this->_out('/Subtype /'.$type);
1706 $this->_out('/FirstChar 32 /LastChar 255');
1707 $this->_out('/Widths '.($this->n+1).' 0 R');
1708 $this->_out('/FontDescriptor '.($this->n+2).' 0 R');
1711 if(isset($font['diff']))
1712 $this->_out('/Encoding '.($nf+$font['diff']).' 0 R');
1714 $this->_out('/Encoding /WinAnsiEncoding');
1717 $this->_out('endobj');
1722 for($i=32;$i<=255;$i++)
1723 $s.=$cw[chr($i)].' ';
1724 $this->_out($s.']');
1725 $this->_out('endobj');
1728 $s='<</Type /FontDescriptor /FontName /'.$name;
1729 foreach($font['desc'] as $k=>$v)
1731 $file=$font['file'];
1733 $s.=' /FontFile'.($type=='Type1' ? '' : '2').' '.$this->FontFiles[$file]['n'].' 0 R';
1734 $this->_out($s.'>>');
1735 $this->_out('endobj');
1737 // TrueType embedded SUBSETS
1738 else if ($type=='TrueTypesubset') {
1740 require_once(dirname(__FILE__).'/font/unifont/ttfonts.php');
1741 $ttf = new TTFontFile();
1742 $ttf->getMetrics($font['file'], 1);
1743 for($sfid=0;$sfid<count($font['subsetfontids']);$sfid++) {
1744 $this->fonts[$k]['n'][$sfid]=$this->n+1; // NB an array for subset
1745 $subsetname = 'MPDFA'.$ssfaid.'+'.$font['name'];
1747 $subset = $font['subsets'][$sfid];
1749 $ttfontstream = $ttf->makeSubset($subset);
1750 $ttfontsize = strlen($ttfontstream);
1751 $fontstream = gzcompress($ttfontstream);
1754 foreach($font['subsets'][$sfid] AS $cp=>$u) {
1755 if (isset($font['cw'][$u])) {
1756 $widthstring .= $font['cw'][$u].' ';
1759 $widthstring .= $ttf->defaultWidth.' ';
1761 $toUnistring .= sprintf("<%02s> <%04s>\n", strtoupper(dechex($cp)), strtoupper(dechex($u)));
1764 //Additional Type1 or TrueType font
1766 $this->_out('<</Type /Font');
1767 $this->_out('/BaseFont /'.$subsetname);
1768 $this->_out('/Subtype /TrueType');
1769 $this->_out('/FirstChar 0 /LastChar '.(count($font['subsets'][$sfid])));
1770 $this->_out('/Widths '.($this->n+1).' 0 R');
1771 $this->_out('/FontDescriptor '.($this->n+2).' 0 R');
1772 $this->_out('/ToUnicode '.($this->n + 3).' 0 R');
1774 $this->_out('endobj');
1778 $this->_out('['.$widthstring.']');
1779 $this->_out('endobj');
1783 $s='<</Type /FontDescriptor /FontName /'.$subsetname."\n";
1784 foreach($font['desc'] as $kd=>$v) {
1785 if ($kd == 'Flags') { $v = $v | 4; $v = $v & ~32; }
1786 $s.=' /'.$kd.' '.$v."\n";
1789 $s.='/FontFile2 '.($this->n + 2).' 0 R';
1790 $this->_out($s.'>>');
1791 $this->_out('endobj');
1794 $toUni = "/CIDInit /ProcSet findresource begin\n";
1795 $toUni .= "12 dict begin\n";
1796 $toUni .= "begincmap\n";
1797 $toUni .= "/CIDSystemInfo\n";
1798 $toUni .= "<</Registry (Adobe)\n";
1799 $toUni .= "/Ordering (UCS)\n";
1800 $toUni .= "/Supplement 0\n";
1801 $toUni .= ">> def\n";
1802 $toUni .= "/CMapName /Adobe-Identity-UCS def\n";
1803 $toUni .= "/CMapType 2 def\n";
1804 $toUni .= "1 begincodespacerange\n";
1805 $toUni .= "<00> <FF>\n";
1806 $toUni .= "endcodespacerange\n";
1807 $toUni .= count($font['subsets'][$sfid])." beginbfchar\n";
1808 $toUni .= $toUnistring;
1809 $toUni .= "endbfchar\n";
1810 $toUni .= "endcmap\n";
1811 $toUni .= "CMapName currentdict /CMap defineresource pop\n";
1815 $this->_out('<</Length '.strlen($toUni).'>>');
1816 $this->_putstream($toUni);
1817 $this->_out('endobj');
1821 $this->_out('<</Length '.strlen($fontstream));
1822 $this->_out('/Filter /FlateDecode');
1823 $this->_out('/Length1 '.$ttfontsize);
1825 $this->_putstream($fontstream);
1826 $this->_out('endobj');
1832 //Allow for additional types
1833 $this->fonts[$k]['n']=$this->n+1;
1834 $mtd='_put'.strtolower($type);
1835 if(!method_exists($this,$mtd))
1836 $this->Error('Unsupported font type: '.$type);
1842 function _putimages()
1844 $filter=($this->compress) ? '/Filter /FlateDecode ' : '';
1845 reset($this->images);
1846 foreach($this->images as $file_info) {
1848 list($file,$info)=$file_info;
1851 $this->images[$file]['n']=$this->n;
1852 $this->_out('<</Type /XObject');
1853 $this->_out('/Subtype /Image');
1854 $this->_out('/Width '.$info['w']);
1855 $this->_out('/Height '.$info['h']);
1856 if($info['cs']=='Indexed')
1857 $this->_out('/ColorSpace [/Indexed /DeviceRGB '.(strlen($info['pal'])/3-1).' '.($this->n+1).' 0 R]');
1860 $this->_out('/ColorSpace /'.$info['cs']);
1861 if($info['cs']=='DeviceCMYK')
1862 $this->_out('/Decode [1 0 1 0 1 0 1 0]');
1864 $this->_out('/BitsPerComponent '.$info['bpc']);
1865 if(isset($info['f']))
1866 $this->_out('/Filter /'.$info['f']);
1867 if(isset($info['parms']))
1868 $this->_out($info['parms']);
1869 if(isset($info['trns']) && is_array($info['trns']))
1872 for($i=0;$i<count($info['trns']);$i++)
1873 $trns.=$info['trns'][$i].' '.$info['trns'][$i].' ';
1874 $this->_out('/Mask ['.$trns.']');
1876 $this->_out('/Length '.strlen($info['data']).'>>');
1877 $this->_putstream($info['data']);
1878 unset($this->images[$file]['data']);
1879 $this->_out('endobj');
1881 if($info['cs']=='Indexed')
1884 $pal=($this->compress) ? gzcompress($info['pal']) : $info['pal'];
1885 $this->_out('<<'.$filter.'/Length '.strlen($pal).'>>');
1886 $this->_putstream($pal);
1887 $this->_out('endobj');
1892 function _putxobjectdict()
1894 foreach($this->images as $image)
1895 $this->_out('/I'.$image['i'].' '.$image['n'].' 0 R');
1898 function _putresourcedict()
1900 $this->_out('/ProcSet [/PDF /Text /ImageB /ImageC /ImageI]');
1901 $this->_out('/Font <<');
1902 foreach($this->fonts as $font) {
1903 if ($font['type']=='TrueTypesubset') {
1904 foreach($font['n'] AS $k => $fid) {
1905 $this->_out('/F'.$font['subsetfontids'][$k].' '.$font['n'][$k].' 0 R');
1909 $this->_out('/F'.$font['i'].' '.$font['n'].' 0 R');
1913 $this->_out('/XObject <<');
1914 $this->_putxobjectdict();
1918 function _putresources()
1921 $this->_putimages();
1922 //Resource dictionary
1923 $this->offsets[2]=strlen($this->buffer);
1924 $this->_out('2 0 obj');
1926 $this->_putresourcedict();
1928 $this->_out('endobj');
1933 $this->_out('/Producer '.$this->_textstring('tFPDF '.tFPDF_VERSION));
1934 if(!empty($this->title))
1935 $this->_out('/Title '.$this->_textstring($this->title));
1936 if(!empty($this->subject))
1937 $this->_out('/Subject '.$this->_textstring($this->subject));
1938 if(!empty($this->author))
1939 $this->_out('/Author '.$this->_textstring($this->author));
1940 if(!empty($this->keywords))
1941 $this->_out('/Keywords '.$this->_textstring($this->keywords));
1942 if(!empty($this->creator))
1943 $this->_out('/Creator '.$this->_textstring($this->creator));
1944 $this->_out('/CreationDate '.$this->_textstring('D:'.date('YmdHis')));
1947 function _putcatalog()
1949 $this->_out('/Type /Catalog');
1950 $this->_out('/Pages 1 0 R');
1951 if($this->ZoomMode=='fullpage')
1952 $this->_out('/OpenAction [3 0 R /Fit]');
1953 elseif($this->ZoomMode=='fullwidth')
1954 $this->_out('/OpenAction [3 0 R /FitH null]');
1955 elseif($this->ZoomMode=='real')
1956 $this->_out('/OpenAction [3 0 R /XYZ null null 1]');
1957 elseif(!is_string($this->ZoomMode))
1958 $this->_out('/OpenAction [3 0 R /XYZ null null '.($this->ZoomMode/100).']');
1959 if($this->LayoutMode=='single')
1960 $this->_out('/PageLayout /SinglePage');
1961 elseif($this->LayoutMode=='continuous')
1962 $this->_out('/PageLayout /OneColumn');
1963 elseif($this->LayoutMode=='two')
1964 $this->_out('/PageLayout /TwoColumnLeft');
1967 function _putheader()
1969 $this->_out('%PDF-'.$this->PDFVersion);
1972 function _puttrailer()
1974 $this->_out('/Size '.($this->n+1));
1975 $this->_out('/Root '.$this->n.' 0 R');
1976 $this->_out('/Info '.($this->n-1).' 0 R');
1981 $this->_putheader();
1983 $this->_putresources();
1989 $this->_out('endobj');
1993 $this->_putcatalog();
1995 $this->_out('endobj');
1997 $o=strlen($this->buffer);
1998 $this->_out('xref');
1999 $this->_out('0 '.($this->n+1));
2000 $this->_out('0000000000 65535 f ');
2001 for($i=1;$i<=$this->n;$i++)
2002 $this->_out(sprintf('%010d 00000 n ',$this->offsets[$i]));
2004 $this->_out('trailer');
2006 $this->_puttrailer();
2008 $this->_out('startxref');
2010 $this->_out('%%EOF');
2014 // ********* NEW FUNCTIONS *********
2016 // Convert utf-8 string to <HHHHHH> for Font Subsets
2017 function UTF8toSubset($str) {
2019 if ($this->AliasNbPages)
2020 $str = preg_replace('/'.preg_quote($this->AliasNbPages,'/').'/', chr(7), $str );
2021 $unicode = $this->UTF8StringToArray($str);
2022 $orig_fid = $this->CurrentFont['subsetfontids'][0];
2023 $last_fid = $this->CurrentFont['subsetfontids'][0];
2024 foreach($unicode as $c) {
2026 if ($orig_fid != $last_fid) {
2027 $ret .= '> Tj /F'.$orig_fid.' '.$this->FontSizePt.' Tf <';
2028 $last_fid = $orig_fid;
2030 $ret .= '`'.$this->AliasNbPages.'`';
2033 for ($i=0; $i<99; $i++) {
2034 // return c as decimal char
2035 $init = array_search($c, $this->CurrentFont['subsets'][$i]);
2036 if ($init!==false) {
2037 if ($this->CurrentFont['subsetfontids'][$i] != $last_fid) {
2038 $ret .= '> Tj /F'.$this->CurrentFont['subsetfontids'][$i].' '.$this->FontSizePt.' Tf <';
2039 $last_fid = $this->CurrentFont['subsetfontids'][$i];
2041 $ret .= sprintf("%02s", strtoupper(dechex($init)));
2044 else if (count($this->CurrentFont['subsets'][$i]) < 255) {
2045 $n = count($this->CurrentFont['subsets'][$i]);
2046 $this->CurrentFont['subsets'][$i][$n] = $c;
2047 if ($this->CurrentFont['subsetfontids'][$i] != $last_fid) {
2048 $ret .= '> Tj /F'.$this->CurrentFont['subsetfontids'][$i].' '.$this->FontSizePt.' Tf <';
2049 $last_fid = $this->CurrentFont['subsetfontids'][$i];
2051 $ret .= sprintf("%02s", strtoupper(dechex($n)));
2054 else if (!isset($this->CurrentFont['subsets'][($i+1)])) {
2055 $this->CurrentFont['subsets'][($i+1)] = array(0=>0);
2056 $new_fid = count($this->fonts)+$this->extraFontSubsets+1;
2057 $this->CurrentFont['subsetfontids'][($i+1)] = $new_fid;
2058 $this->extraFontSubsets++;
2063 if ($last_fid != $orig_fid) {
2064 $ret .= ' Tj /F'.$orig_fid.' '.$this->FontSizePt.' Tf <> ';
2069 // Converts UTF-8 strings to codepoints array
2070 function UTF8StringToArray($str) {
2072 $len = strlen($str);
2073 for ($i = 0; $i < $len; $i++) {
2077 elseif ( $h >= 0xC2 ) {
2078 if ( ($h <= 0xDF) && ($i < $len -1) )
2079 $out[] = ($h & 0x1F) << 6 | (ord($str[++$i]) & 0x3F);
2080 elseif ( ($h <= 0xEF) && ($i < $len -2) )
2081 $out[] = ($h & 0x0F) << 12 | (ord($str[++$i]) & 0x3F) << 6
2082 | (ord($str[++$i]) & 0x3F);
2083 elseif ( ($h <= 0xF4) && ($i < $len -3) )
2084 $out[] = ($h & 0x0F) << 18 | (ord($str[++$i]) & 0x3F) << 12
2085 | (ord($str[++$i]) & 0x3F) << 6
2086 | (ord($str[++$i]) & 0x3F);
2096 //Handle special IE contype request
2097 if(isset($_SERVER['HTTP_USER_AGENT']) && $_SERVER['HTTP_USER_AGENT']=='contype')
2099 header('Content-Type: application/pdf');