Changeset 11339
- Timestamp:
- 02/10/11 14:37:06 (2 years ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
freepbx/branches/2.9/amp_conf/htdocs/admin/cdr/lib/fpdf.php
r11047 r11339 1 1 <?php 2 2 /******************************************************************************* 3 * Logiciel : FPDF * 4 * Version : 1.52 * 5 * Date : 30/12/2003 * 6 * Auteur : Olivier PLATHEY * 7 * Licence : Freeware * 3 * FPDF * 8 4 * * 9 * Vous pouvez utiliser et modifier ce logiciel comme vous le souhaitez. * 5 * Version: 1.6 * 6 * Date: 2008-08-03 * 7 * Author: Olivier PLATHEY * 10 8 *******************************************************************************/ 11 defined('FREEPBX_IS_AUTH') OR die('No direct script access allowed'); 12 if(!class_exists('FPDF')) 13 { 14 define('FPDF_VERSION','1.52'); 9 10 define('FPDF_VERSION','1.6'); 15 11 16 12 class FPDF 17 13 { 18 //Private properties19 14 var $page; //current page number 20 15 var $n; //current object number … … 24 19 var $state; //current document state 25 20 var $compress; //compression flag 21 var $k; //scale factor (number of points in user unit) 26 22 var $DefOrientation; //default orientation 27 23 var $CurOrientation; //current orientation 28 var $ OrientationChanges; //array indicating orientation changes29 var $ k; //scale factor (number of points in user unit)30 var $ fwPt,$fhPt; //dimensions of page format in points31 var $ fw,$fh; //dimensions of page format in user unit32 var $wPt,$hPt; // current dimensions ofpage in points33 var $w,$h; // current dimensions ofpage in user unit24 var $PageFormats; //available page formats 25 var $DefPageFormat; //default page format 26 var $CurPageFormat; //current page format 27 var $PageSizes; //array storing non-default page sizes 28 var $wPt,$hPt; //dimensions of current page in points 29 var $w,$h; //dimensions of current page in user unit 34 30 var $lMargin; //left margin 35 31 var $tMargin; //top margin … … 37 33 var $bMargin; //page break margin 38 34 var $cMargin; //cell margin 39 var $x,$y; //current position in user unit for cell positioning40 var $lasth; //height of last cell printed35 var $x,$y; //current position in user unit 36 var $lasth; //height of last printed cell 41 37 var $LineWidth; //line width in user unit 42 38 var $CoreFonts; //array of standard font names … … 44 40 var $FontFiles; //array of font files 45 41 var $diffs; //array of encoding differences 46 var $images; //array of used images47 var $PageLinks; //array of links in pages48 var $links; //array of internal links49 42 var $FontFamily; //current font family 50 43 var $FontStyle; //current font style … … 58 51 var $ColorFlag; //indicates whether fill and text colors are different 59 52 var $ws; //word spacing 53 var $images; //array of used images 54 var $PageLinks; //array of links in pages 55 var $links; //array of internal links 60 56 var $AutoPageBreak; //automatic page breaking 61 57 var $PageBreakTrigger; //threshold used to trigger page breaks 58 var $InHeader; //flag set when processing header 62 59 var $InFooter; //flag set when processing footer 63 60 var $ZoomMode; //zoom display mode … … 69 66 var $creator; //creator 70 67 var $AliasNbPages; //alias for total number of pages 68 var $PDFVersion; //PDF version number 71 69 72 70 /******************************************************************************* … … 75 73 * * 76 74 *******************************************************************************/ 77 function FPDF($orientation='P', $unit='mm',$format='A4')75 function FPDF($orientation='P', $unit='mm', $format='A4') 78 76 { 79 77 //Some checks … … 84 82 $this->buffer=''; 85 83 $this->pages=array(); 86 $this-> OrientationChanges=array();84 $this->PageSizes=array(); 87 85 $this->state=0; 88 86 $this->fonts=array(); … … 91 89 $this->images=array(); 92 90 $this->links=array(); 91 $this->InHeader=false; 93 92 $this->InFooter=false; 94 93 $this->lasth=0; … … 103 102 $this->ws=0; 104 103 //Standard fonts 105 $this->CoreFonts=array('courier'=>'Courier', 'courierB'=>'Courier-Bold','courierI'=>'Courier-Oblique','courierBI'=>'Courier-BoldOblique',106 'helvetica'=>'Helvetica', 'helveticaB'=>'Helvetica-Bold','helveticaI'=>'Helvetica-Oblique','helveticaBI'=>'Helvetica-BoldOblique',107 'times'=>'Times-Roman', 'timesB'=>'Times-Bold','timesI'=>'Times-Italic','timesBI'=>'Times-BoldItalic',108 'symbol'=>'Symbol', 'zapfdingbats'=>'ZapfDingbats');104 $this->CoreFonts=array('courier'=>'Courier', 'courierB'=>'Courier-Bold', 'courierI'=>'Courier-Oblique', 'courierBI'=>'Courier-BoldOblique', 105 'helvetica'=>'Helvetica', 'helveticaB'=>'Helvetica-Bold', 'helveticaI'=>'Helvetica-Oblique', 'helveticaBI'=>'Helvetica-BoldOblique', 106 'times'=>'Times-Roman', 'timesB'=>'Times-Bold', 'timesI'=>'Times-Italic', 'timesBI'=>'Times-BoldItalic', 107 'symbol'=>'Symbol', 'zapfdingbats'=>'ZapfDingbats'); 109 108 //Scale factor 110 109 if($unit=='pt') … … 119 118 $this->Error('Incorrect unit: '.$unit); 120 119 //Page format 120 $this->PageFormats=array('a3'=>array(841.89,1190.55), 'a4'=>array(595.28,841.89), 'a5'=>array(420.94,595.28), 121 'letter'=>array(612,792), 'legal'=>array(612,1008)); 121 122 if(is_string($format)) 122 { 123 $format=strtolower($format); 124 if($format=='a3') 125 $format=array(841.89,1190.55); 126 elseif($format=='a4') 127 $format=array(595.28,841.89); 128 elseif($format=='a5') 129 $format=array(420.94,595.28); 130 elseif($format=='letter') 131 $format=array(612,792); 132 elseif($format=='legal') 133 $format=array(612,1008); 134 else 135 $this->Error('Unknown page format: '.$format); 136 $this->fwPt=$format[0]; 137 $this->fhPt=$format[1]; 138 } 139 else 140 { 141 $this->fwPt=$format[0]*$this->k; 142 $this->fhPt=$format[1]*$this->k; 143 } 144 $this->fw=$this->fwPt/$this->k; 145 $this->fh=$this->fhPt/$this->k; 123 $format=$this->_getpageformat($format); 124 $this->DefPageFormat=$format; 125 $this->CurPageFormat=$format; 146 126 //Page orientation 147 127 $orientation=strtolower($orientation); 148 if($orientation=='p' or$orientation=='portrait')128 if($orientation=='p' || $orientation=='portrait') 149 129 { 150 130 $this->DefOrientation='P'; 151 $this->w Pt=$this->fwPt;152 $this->h Pt=$this->fhPt;153 } 154 elseif($orientation=='l' or$orientation=='landscape')131 $this->w=$this->DefPageFormat[0]; 132 $this->h=$this->DefPageFormat[1]; 133 } 134 elseif($orientation=='l' || $orientation=='landscape') 155 135 { 156 136 $this->DefOrientation='L'; 157 $this->w Pt=$this->fhPt;158 $this->h Pt=$this->fwPt;137 $this->w=$this->DefPageFormat[1]; 138 $this->h=$this->DefPageFormat[0]; 159 139 } 160 140 else 161 141 $this->Error('Incorrect orientation: '.$orientation); 162 142 $this->CurOrientation=$this->DefOrientation; 163 $this->w =$this->wPt/$this->k;164 $this->h =$this->hPt/$this->k;143 $this->wPt=$this->w*$this->k; 144 $this->hPt=$this->h*$this->k; 165 145 //Page margins (1 cm) 166 146 $margin=28.35/$this->k; … … 174 154 //Full width display mode 175 155 $this->SetDisplayMode('fullwidth'); 176 // Compression156 //Enable compression 177 157 $this->SetCompression(true); 178 } 179 180 function SetMargins($left,$top,$right=-1) 158 //Set default PDF version number 159 $this->PDFVersion='1.3'; 160 } 161 162 function SetMargins($left, $top, $right=null) 181 163 { 182 164 //Set left, top and right margins 183 165 $this->lMargin=$left; 184 166 $this->tMargin=$top; 185 if($right== -1)167 if($right===null) 186 168 $right=$left; 187 169 $this->rMargin=$right; … … 192 174 //Set left margin 193 175 $this->lMargin=$margin; 194 if($this->page>0 and$this->x<$margin)176 if($this->page>0 && $this->x<$margin) 195 177 $this->x=$margin; 196 178 } … … 208 190 } 209 191 210 function SetAutoPageBreak($auto, $margin=0)192 function SetAutoPageBreak($auto, $margin=0) 211 193 { 212 194 //Set auto page break mode and triggering margin … … 216 198 } 217 199 218 function SetDisplayMode($zoom, $layout='continuous')200 function SetDisplayMode($zoom, $layout='continuous') 219 201 { 220 202 //Set display mode in viewer 221 if($zoom=='fullpage' or $zoom=='fullwidth' or $zoom=='real' or $zoom=='default' or!is_string($zoom))203 if($zoom=='fullpage' || $zoom=='fullwidth' || $zoom=='real' || $zoom=='default' || !is_string($zoom)) 222 204 $this->ZoomMode=$zoom; 223 205 else 224 206 $this->Error('Incorrect zoom display mode: '.$zoom); 225 if($layout=='single' or $layout=='continuous' or $layout=='two' or$layout=='default')207 if($layout=='single' || $layout=='continuous' || $layout=='two' || $layout=='default') 226 208 $this->LayoutMode=$layout; 227 209 else … … 238 220 } 239 221 240 function SetTitle($title )222 function SetTitle($title, $isUTF8=false) 241 223 { 242 224 //Title of document 225 if($isUTF8) 226 $title=$this->_UTF8toUTF16($title); 243 227 $this->title=$title; 244 228 } 245 229 246 function SetSubject($subject )230 function SetSubject($subject, $isUTF8=false) 247 231 { 248 232 //Subject of document 233 if($isUTF8) 234 $subject=$this->_UTF8toUTF16($subject); 249 235 $this->subject=$subject; 250 236 } 251 237 252 function SetAuthor($author )238 function SetAuthor($author, $isUTF8=false) 253 239 { 254 240 //Author of document 241 if($isUTF8) 242 $author=$this->_UTF8toUTF16($author); 255 243 $this->author=$author; 256 244 } 257 245 258 function SetKeywords($keywords )246 function SetKeywords($keywords, $isUTF8=false) 259 247 { 260 248 //Keywords of document 249 if($isUTF8) 250 $keywords=$this->_UTF8toUTF16($keywords); 261 251 $this->keywords=$keywords; 262 252 } 263 253 264 function SetCreator($creator )254 function SetCreator($creator, $isUTF8=false) 265 255 { 266 256 //Creator of document 257 if($isUTF8) 258 $creator=$this->_UTF8toUTF16($creator); 267 259 $this->creator=$creator; 268 260 } … … 277 269 { 278 270 //Fatal error 279 die('< B>FPDF error: </B>'.$msg);271 die('<b>FPDF error:</b> '.$msg); 280 272 } 281 273 … … 283 275 { 284 276 //Begin document 285 if($this->state==0) 286 $this->_begindoc(); 277 $this->state=1; 287 278 } 288 279 … … 304 295 } 305 296 306 function AddPage($orientation='' )297 function AddPage($orientation='', $format='') 307 298 { 308 299 //Start a new page … … 327 318 } 328 319 //Start new page 329 $this->_beginpage($orientation );320 $this->_beginpage($orientation,$format); 330 321 //Set line cap style to square 331 322 $this->_out('2 J'); 332 323 //Set line width 333 324 $this->LineWidth=$lw; 334 $this->_out(sprintf('%.2 fw',$lw*$this->k));325 $this->_out(sprintf('%.2F w',$lw*$this->k)); 335 326 //Set font 336 327 if($family) … … 346 337 $this->ColorFlag=$cf; 347 338 //Page header 339 $this->InHeader=true; 348 340 $this->Header(); 341 $this->InHeader=false; 349 342 //Restore line width 350 343 if($this->LineWidth!=$lw) 351 344 { 352 345 $this->LineWidth=$lw; 353 $this->_out(sprintf('%.2 fw',$lw*$this->k));346 $this->_out(sprintf('%.2F w',$lw*$this->k)); 354 347 } 355 348 //Restore font … … 387 380 } 388 381 389 function SetDrawColor($r, $g=-1,$b=-1)382 function SetDrawColor($r, $g=null, $b=null) 390 383 { 391 384 //Set color for all stroking operations 392 if(($r==0 and $g==0 and $b==0) or $g==-1)393 $this->DrawColor=sprintf('%.3 fG',$r/255);394 else 395 $this->DrawColor=sprintf('%.3 f %.3f %.3fRG',$r/255,$g/255,$b/255);385 if(($r==0 && $g==0 && $b==0) || $g===null) 386 $this->DrawColor=sprintf('%.3F G',$r/255); 387 else 388 $this->DrawColor=sprintf('%.3F %.3F %.3F RG',$r/255,$g/255,$b/255); 396 389 if($this->page>0) 397 390 $this->_out($this->DrawColor); 398 391 } 399 392 400 function SetFillColor($r, $g=-1,$b=-1)393 function SetFillColor($r, $g=null, $b=null) 401 394 { 402 395 //Set color for all filling operations 403 if(($r==0 and $g==0 and $b==0) or $g==-1)404 $this->FillColor=sprintf('%.3 fg',$r/255);405 else 406 $this->FillColor=sprintf('%.3 f %.3f %.3frg',$r/255,$g/255,$b/255);396 if(($r==0 && $g==0 && $b==0) || $g===null) 397 $this->FillColor=sprintf('%.3F g',$r/255); 398 else 399 $this->FillColor=sprintf('%.3F %.3F %.3F rg',$r/255,$g/255,$b/255); 407 400 $this->ColorFlag=($this->FillColor!=$this->TextColor); 408 401 if($this->page>0) … … 410 403 } 411 404 412 function SetTextColor($r, $g=-1,$b=-1)405 function SetTextColor($r, $g=null, $b=null) 413 406 { 414 407 //Set color for text 415 if(($r==0 and $g==0 and $b==0) or $g==-1)416 $this->TextColor=sprintf('%.3 fg',$r/255);417 else 418 $this->TextColor=sprintf('%.3 f %.3f %.3frg',$r/255,$g/255,$b/255);408 if(($r==0 && $g==0 && $b==0) || $g===null) 409 $this->TextColor=sprintf('%.3F g',$r/255); 410 else 411 $this->TextColor=sprintf('%.3F %.3F %.3F rg',$r/255,$g/255,$b/255); 419 412 $this->ColorFlag=($this->FillColor!=$this->TextColor); 420 413 } … … 428 421 $l=strlen($s); 429 422 for($i=0;$i<$l;$i++) 430 $w+=$cw[$s {$i}];423 $w+=$cw[$s[$i]]; 431 424 return $w*$this->FontSize/1000; 432 425 } … … 437 430 $this->LineWidth=$width; 438 431 if($this->page>0) 439 $this->_out(sprintf('%.2 fw',$width*$this->k));440 } 441 442 function Line($x1, $y1,$x2,$y2)432 $this->_out(sprintf('%.2F w',$width*$this->k)); 433 } 434 435 function Line($x1, $y1, $x2, $y2) 443 436 { 444 437 //Draw a line 445 $this->_out(sprintf('%.2 f %.2f m %.2f %.2fl S',$x1*$this->k,($this->h-$y1)*$this->k,$x2*$this->k,($this->h-$y2)*$this->k));446 } 447 448 function Rect($x, $y,$w,$h,$style='')438 $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)); 439 } 440 441 function Rect($x, $y, $w, $h, $style='') 449 442 { 450 443 //Draw a rectangle 451 444 if($style=='F') 452 445 $op='f'; 453 elseif($style=='FD' or$style=='DF')446 elseif($style=='FD' || $style=='DF') 454 447 $op='B'; 455 448 else 456 449 $op='S'; 457 $this->_out(sprintf('%.2 f %.2f %.2f %.2fre %s',$x*$this->k,($this->h-$y)*$this->k,$w*$this->k,-$h*$this->k,$op));458 } 459 460 function AddFont($family, $style='',$file='')450 $this->_out(sprintf('%.2F %.2F %.2F %.2F re %s',$x*$this->k,($this->h-$y)*$this->k,$w*$this->k,-$h*$this->k,$op)); 451 } 452 453 function AddFont($family, $style='', $file='') 461 454 { 462 455 //Add a TrueType or Type1 font 463 456 $family=strtolower($family); 457 if($file=='') 458 $file=str_replace(' ','',$family).strtolower($style).'.php'; 464 459 if($family=='arial') 465 460 $family='helvetica'; … … 467 462 if($style=='IB') 468 463 $style='BI'; 469 if(isset($this->fonts[$family.$style])) 470 $this->Error('Font already added: '.$family.' '.$style); 471 if($file=='') 472 $file=str_replace(' ','',$family).strtolower($style).'.php'; 473 if(defined('FPDF_FONTPATH')) 474 $file=FPDF_FONTPATH.$file; 475 include($file); 464 $fontkey=$family.$style; 465 if(isset($this->fonts[$fontkey])) 466 return; 467 include($this->_getfontpath().$file); 476 468 if(!isset($name)) 477 469 $this->Error('Could not include font definition file'); 478 470 $i=count($this->fonts)+1; 479 $this->fonts[$f amily.$style]=array('i'=>$i,'type'=>$type,'name'=>$name,'desc'=>$desc,'up'=>$up,'ut'=>$ut,'cw'=>$cw,'enc'=>$enc,'file'=>$file);471 $this->fonts[$fontkey]=array('i'=>$i, 'type'=>$type, 'name'=>$name, 'desc'=>$desc, 'up'=>$up, 'ut'=>$ut, 'cw'=>$cw, 'enc'=>$enc, 'file'=>$file); 480 472 if($diff) 481 473 { … … 484 476 $nb=count($this->diffs); 485 477 for($i=1;$i<=$nb;$i++) 478 { 486 479 if($this->diffs[$i]==$diff) 487 480 { … … 489 482 break; 490 483 } 484 } 491 485 if($d==0) 492 486 { … … 494 488 $this->diffs[$d]=$diff; 495 489 } 496 $this->fonts[$f amily.$style]['diff']=$d;490 $this->fonts[$fontkey]['diff']=$d; 497 491 } 498 492 if($file) … … 501 495 $this->FontFiles[$file]=array('length1'=>$originalsize); 502 496 else 503 $this->FontFiles[$file]=array('length1'=>$size1, 'length2'=>$size2);504 } 505 } 506 507 function SetFont($family, $style='',$size=0)497 $this->FontFiles[$file]=array('length1'=>$size1, 'length2'=>$size2); 498 } 499 } 500 501 function SetFont($family, $style='', $size=0) 508 502 { 509 503 //Select a font; size given in points … … 515 509 if($family=='arial') 516 510 $family='helvetica'; 517 elseif($family=='symbol' or$family=='zapfdingbats')511 elseif($family=='symbol' || $family=='zapfdingbats') 518 512 $style=''; 519 513 $style=strtoupper($style); 520 if( is_int(strpos($style,'U')))514 if(strpos($style,'U')!==false) 521 515 { 522 516 $this->underline=true; … … 530 524 $size=$this->FontSizePt; 531 525 //Test if font is already selected 532 if($this->FontFamily==$family and $this->FontStyle==$style and$this->FontSizePt==$size)526 if($this->FontFamily==$family && $this->FontStyle==$style && $this->FontSizePt==$size) 533 527 return; 534 528 //Test if used for the first time … … 543 537 //Load metric file 544 538 $file=$family; 545 if($family=='times' or$family=='helvetica')539 if($family=='times' || $family=='helvetica') 546 540 $file.=strtolower($style); 547 $file.='.php'; 548 if(defined('FPDF_FONTPATH')) 549 $file=FPDF_FONTPATH.$file; 550 include($file); 541 include($this->_getfontpath().$file.'.php'); 551 542 if(!isset($fpdf_charwidths[$fontkey])) 552 543 $this->Error('Could not include font metric file'); 553 544 } 554 545 $i=count($this->fonts)+1; 555 $this->fonts[$fontkey]=array('i'=>$i,'type'=>'core','name'=>$this->CoreFonts[$fontkey],'up'=>-100,'ut'=>50,'cw'=>$fpdf_charwidths[$fontkey]); 546 $name=$this->CoreFonts[$fontkey]; 547 $cw=$fpdf_charwidths[$fontkey]; 548 $this->fonts[$fontkey]=array('i'=>$i, 'type'=>'core', 'name'=>$name, 'up'=>-100, 'ut'=>50, 'cw'=>$cw); 556 549 } 557 550 else … … 565 558 $this->CurrentFont=&$this->fonts[$fontkey]; 566 559 if($this->page>0) 567 $this->_out(sprintf('BT /F%d %.2 fTf ET',$this->CurrentFont['i'],$this->FontSizePt));560 $this->_out(sprintf('BT /F%d %.2F Tf ET',$this->CurrentFont['i'],$this->FontSizePt)); 568 561 } 569 562 … … 576 569 $this->FontSize=$size/$this->k; 577 570 if($this->page>0) 578 $this->_out(sprintf('BT /F%d %.2 fTf ET',$this->CurrentFont['i'],$this->FontSizePt));571 $this->_out(sprintf('BT /F%d %.2F Tf ET',$this->CurrentFont['i'],$this->FontSizePt)); 579 572 } 580 573 … … 583 576 //Create a new internal link 584 577 $n=count($this->links)+1; 585 $this->links[$n]=array(0, 0);578 $this->links[$n]=array(0, 0); 586 579 return $n; 587 580 } 588 581 589 function SetLink($link, $y=0,$page=-1)582 function SetLink($link, $y=0, $page=-1) 590 583 { 591 584 //Set destination of internal link … … 594 587 if($page==-1) 595 588 $page=$this->page; 596 $this->links[$link]=array($page, $y);597 } 598 599 function Link($x, $y,$w,$h,$link)589 $this->links[$link]=array($page, $y); 590 } 591 592 function Link($x, $y, $w, $h, $link) 600 593 { 601 594 //Put a link on the page 602 $this->PageLinks[$this->page][]=array($x*$this->k, $this->hPt-$y*$this->k,$w*$this->k,$h*$this->k,$link);603 } 604 605 function Text($x, $y,$txt)595 $this->PageLinks[$this->page][]=array($x*$this->k, $this->hPt-$y*$this->k, $w*$this->k, $h*$this->k, $link); 596 } 597 598 function Text($x, $y, $txt) 606 599 { 607 600 //Output a string 608 $s=sprintf('BT %.2 f %.2fTd (%s) Tj ET',$x*$this->k,($this->h-$y)*$this->k,$this->_escape($txt));609 if($this->underline and$txt!='')601 $s=sprintf('BT %.2F %.2F Td (%s) Tj ET',$x*$this->k,($this->h-$y)*$this->k,$this->_escape($txt)); 602 if($this->underline && $txt!='') 610 603 $s.=' '.$this->_dounderline($x,$y,$txt); 611 604 if($this->ColorFlag) … … 620 613 } 621 614 622 function Cell($w, $h=0,$txt='',$border=0,$ln=0,$align='',$fill=0,$link='')615 function Cell($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link='') 623 616 { 624 617 //Output a cell 625 618 $k=$this->k; 626 if($this->y+$h>$this->PageBreakTrigger and !$this->InFooter and$this->AcceptPageBreak())619 if($this->y+$h>$this->PageBreakTrigger && !$this->InHeader && !$this->InFooter && $this->AcceptPageBreak()) 627 620 { 628 621 //Automatic page break … … 634 627 $this->_out('0 Tw'); 635 628 } 636 $this->AddPage($this->CurOrientation );629 $this->AddPage($this->CurOrientation,$this->CurPageFormat); 637 630 $this->x=$x; 638 631 if($ws>0) 639 632 { 640 633 $this->ws=$ws; 641 $this->_out(sprintf('%.3 fTw',$ws*$k));634 $this->_out(sprintf('%.3F Tw',$ws*$k)); 642 635 } 643 636 } … … 645 638 $w=$this->w-$this->rMargin-$this->x; 646 639 $s=''; 647 if($fill ==1 or$border==1)648 { 649 if($fill ==1)640 if($fill || $border==1) 641 { 642 if($fill) 650 643 $op=($border==1) ? 'B' : 'f'; 651 644 else 652 645 $op='S'; 653 $s=sprintf('%.2 f %.2f %.2f %.2fre %s ',$this->x*$k,($this->h-$this->y)*$k,$w*$k,-$h*$k,$op);646 $s=sprintf('%.2F %.2F %.2F %.2F re %s ',$this->x*$k,($this->h-$this->y)*$k,$w*$k,-$h*$k,$op); 654 647 } 655 648 if(is_string($border)) … … 657 650 $x=$this->x; 658 651 $y=$this->y; 659 if( is_int(strpos($border,'L')))660 $s.=sprintf('%.2 f %.2f m %.2f %.2fl S ',$x*$k,($this->h-$y)*$k,$x*$k,($this->h-($y+$h))*$k);661 if( is_int(strpos($border,'T')))662 $s.=sprintf('%.2 f %.2f m %.2f %.2fl S ',$x*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-$y)*$k);663 if( is_int(strpos($border,'R')))664 $s.=sprintf('%.2 f %.2f m %.2f %.2fl S ',($x+$w)*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-($y+$h))*$k);665 if( is_int(strpos($border,'B')))666 $s.=sprintf('%.2 f %.2f m %.2f %.2fl S ',$x*$k,($this->h-($y+$h))*$k,($x+$w)*$k,($this->h-($y+$h))*$k);667 } 668 if($txt!= '')652 if(strpos($border,'L')!==false) 653 $s.=sprintf('%.2F %.2F m %.2F %.2F l S ',$x*$k,($this->h-$y)*$k,$x*$k,($this->h-($y+$h))*$k); 654 if(strpos($border,'T')!==false) 655 $s.=sprintf('%.2F %.2F m %.2F %.2F l S ',$x*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-$y)*$k); 656 if(strpos($border,'R')!==false) 657 $s.=sprintf('%.2F %.2F m %.2F %.2F l S ',($x+$w)*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-($y+$h))*$k); 658 if(strpos($border,'B')!==false) 659 $s.=sprintf('%.2F %.2F m %.2F %.2F l S ',$x*$k,($this->h-($y+$h))*$k,($x+$w)*$k,($this->h-($y+$h))*$k); 660 } 661 if($txt!=='') 669 662 { 670 663 if($align=='R') … … 677 670 $s.='q '.$this->TextColor.' '; 678 671 $txt2=str_replace(')','\\)',str_replace('(','\\(',str_replace('\\','\\\\',$txt))); 679 $s.=sprintf('BT %.2 f %.2fTd (%s) Tj ET',($this->x+$dx)*$k,($this->h-($this->y+.5*$h+.3*$this->FontSize))*$k,$txt2);672 $s.=sprintf('BT %.2F %.2F Td (%s) Tj ET',($this->x+$dx)*$k,($this->h-($this->y+.5*$h+.3*$this->FontSize))*$k,$txt2); 680 673 if($this->underline) 681 674 $s.=' '.$this->_dounderline($this->x+$dx,$this->y+.5*$h+.3*$this->FontSize,$txt); … … 699 692 } 700 693 701 function MultiCell($w, $h,$txt,$border=0,$align='J',$fill=0)694 function MultiCell($w, $h, $txt, $border=0, $align='J', $fill=false) 702 695 { 703 696 //Output text with automatic or explicit line breaks … … 708 701 $s=str_replace("\r",'',$txt); 709 702 $nb=strlen($s); 710 if($nb>0 and$s[$nb-1]=="\n")703 if($nb>0 && $s[$nb-1]=="\n") 711 704 $nb--; 712 705 $b=0; … … 722 715 { 723 716 $b2=''; 724 if( is_int(strpos($border,'L')))717 if(strpos($border,'L')!==false) 725 718 $b2.='L'; 726 if( is_int(strpos($border,'R')))719 if(strpos($border,'R')!==false) 727 720 $b2.='R'; 728 $b= is_int(strpos($border,'T')) ? $b2.'T' : $b2;721 $b=(strpos($border,'T')!==false) ? $b2.'T' : $b2; 729 722 } 730 723 } … … 738 731 { 739 732 //Get next character 740 $c=$s {$i};733 $c=$s[$i]; 741 734 if($c=="\n") 742 735 { … … 754 747 $ns=0; 755 748 $nl++; 756 if($border and$nl==2)749 if($border && $nl==2) 757 750 $b=$b2; 758 751 continue; … … 784 777 { 785 778 $this->ws=($ns>1) ? ($wmax-$ls)/1000*$this->FontSize/($ns-1) : 0; 786 $this->_out(sprintf('%.3 fTw',$this->ws*$this->k));779 $this->_out(sprintf('%.3F Tw',$this->ws*$this->k)); 787 780 } 788 781 $this->Cell($w,$h,substr($s,$j,$sep-$j),$b,2,$align,$fill); … … 794 787 $ns=0; 795 788 $nl++; 796 if($border and$nl==2)789 if($border && $nl==2) 797 790 $b=$b2; 798 791 } … … 806 799 $this->_out('0 Tw'); 807 800 } 808 if($border and is_int(strpos($border,'B')))801 if($border && strpos($border,'B')!==false) 809 802 $b.='B'; 810 803 $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill); … … 812 805 } 813 806 814 function Write($h, $txt,$link='')807 function Write($h, $txt, $link='') 815 808 { 816 809 //Output text in flowing mode … … 828 821 { 829 822 //Get next character 830 $c=$s {$i};823 $c=$s[$i]; 831 824 if($c=="\n") 832 825 { … … 893 886 } 894 887 895 function Image($file,$x,$y,$w=0,$h=0,$type='',$link='') 888 function Ln($h=null) 889 { 890 //Line feed; default value is last cell height 891 $this->x=$this->lMargin; 892 if($h===null) 893 $this->y+=$this->lasth; 894 else 895 $this->y+=$h; 896 } 897 898 function Image($file, $x=null, $y=null, $w=0, $h=0, $type='', $link='') 896 899 { 897 900 //Put an image on the page 898 901 if(!isset($this->images[$file])) 899 902 { 900 //First use of image, get info903 //First use of this image, get info 901 904 if($type=='') 902 905 { … … 907 910 } 908 911 $type=strtolower($type); 909 $mqr=get_magic_quotes_runtime(); 910 set_magic_quotes_runtime(0); 911 if($type=='jpg' or $type=='jpeg') 912 $info=$this->_parsejpg($file); 913 elseif($type=='png') 914 $info=$this->_parsepng($file); 915 else 916 { 917 //Allow for additional formats 918 $mtd='_parse'.$type; 919 if(!method_exists($this,$mtd)) 920 $this->Error('Unsupported image type: '.$type); 921 $info=$this->$mtd($file); 922 } 923 set_magic_quotes_runtime($mqr); 912 if($type=='jpeg') 913 $type='jpg'; 914 $mtd='_parse'.$type; 915 if(!method_exists($this,$mtd)) 916 $this->Error('Unsupported image type: '.$type); 917 $info=$this->$mtd($file); 924 918 $info['i']=count($this->images)+1; 925 919 $this->images[$file]=$info; … … 928 922 $info=$this->images[$file]; 929 923 //Automatic width and height calculation if needed 930 if($w==0 and$h==0)924 if($w==0 && $h==0) 931 925 { 932 926 //Put image at 72 dpi … … 934 928 $h=$info['h']/$this->k; 935 929 } 936 if($w==0)930 elseif($w==0) 937 931 $w=$h*$info['w']/$info['h']; 938 if($h==0)932 elseif($h==0) 939 933 $h=$w*$info['h']/$info['w']; 940 $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'])); 934 //Flowing mode 935 if($y===null) 936 { 937 if($this->y+$h>$this->PageBreakTrigger && !$this->InHeader && !$this->InFooter && $this->AcceptPageBreak()) 938 { 939 //Automatic page break 940 $x2=$this->x; 941 $this->AddPage($this->CurOrientation,$this->CurPageFormat); 942 $this->x=$x2; 943 } 944 $y=$this->y; 945 $this->y+=$h; 946 } 947 if($x===null) 948 $x=$this->x; 949 $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'])); 941 950 if($link) 942 951 $this->Link($x,$y,$w,$h,$link); 943 }944 945 function Ln($h='')946 {947 //Line feed; default value is last cell height948 $this->x=$this->lMargin;949 if(is_string($h))950 $this->y+=$this->lasth;951 else952 $this->y+=$h;953 952 } 954 953 … … 984 983 } 985 984 986 function SetXY($x, $y)985 function SetXY($x, $y) 987 986 { 988 987 //Set x and y positions … … 991 990 } 992 991 993 function Output($name='', $dest='')992 function Output($name='', $dest='') 994 993 { 995 994 //Output PDF to some destination 996 global $HTTP_SERVER_VARS;997 998 //Finish document if necessary999 995 if($this->state<3) 1000 996 $this->Close(); 1001 //Normalize parameters1002 if(is_bool($dest))1003 $dest=$dest ? 'D' : 'F';1004 997 $dest=strtoupper($dest); 1005 998 if($dest=='') … … 1017 1010 case 'I': 1018 1011 //Send to standard output 1019 if(isset($HTTP_SERVER_VARS['SERVER_NAME'])) 1012 if(ob_get_length()) 1013 $this->Error('Some data has already been output, can\'t send PDF file'); 1014 if(php_sapi_name()!='cli') 1020 1015 { 1021 1016 //We send to a browser 1022 Header('Content-Type: application/pdf');1017 header('Content-Type: application/pdf'); 1023 1018 if(headers_sent()) 1024 $this->Error('Some data has already been output to browser, can\'t send PDF file'); 1025 Header('Content-Length: '.strlen($this->buffer)); 1026 Header('Content-disposition: inline; filename='.$name); 1019 $this->Error('Some data has already been output, can\'t send PDF file'); 1020 header('Content-Length: '.strlen($this->buffer)); 1021 header('Content-Disposition: inline; filename="'.$name.'"'); 1022 header('Cache-Control: private, max-age=0, must-revalidate'); 1023 header('Pragma: public'); 1024 ini_set('zlib.output_compression','0'); 1027 1025 } 1028 1026 echo $this->buffer; … … 1030 1028 case 'D': 1031 1029 //Download file 1032 if(isset($HTTP_SERVER_VARS['HTTP_USER_AGENT']) and strpos($HTTP_SERVER_VARS['HTTP_USER_AGENT'],'MSIE')) 1033 Header('Content-Type: application/force-download'); 1034 else 1035 Header('Content-Type: application/octet-stream'); 1030 if(ob_get_length()) 1031 $this->Error('Some data has already been output, can\'t send PDF file'); 1032 header('Content-Type: application/x-download'); 1036 1033 if(headers_sent()) 1037 $this->Error('Some data has already been output to browser, can\'t send PDF file'); 1038 Header('Content-Length: '.strlen($this->buffer)); 1039 Header('Content-disposition: attachment; filename='.$name); 1034 $this->Error('Some data has already been output, can\'t send PDF file'); 1035 header('Content-Length: '.strlen($this->buffer)); 1036 header('Content-Disposition: attachment; filename="'.$name.'"'); 1037 header('Cache-Control: private, max-age=0, must-revalidate'); 1038 header('Pragma: public'); 1039 ini_set('zlib.output_compression','0'); 1040 1040 echo $this->buffer; 1041 1041 break; … … 1064 1064 function _dochecks() 1065 1065 { 1066 //Check for locale-related bug 1067 if(1.1==1) 1068 $this->Error('Don\'t alter the locale before including class file'); 1069 //Check for decimal separator 1070 if(sprintf('%.1f',1.0)!='1.0') 1071 setlocale(LC_NUMERIC,'C'); 1072 } 1073 1074 function _begindoc() 1075 { 1076 //Start document 1066 //Check availability of %F 1067 if(sprintf('%.1F',1.0)!='1.0') 1068 $this->Error('This version of PHP is not supported'); 1069 //Check mbstring overloading 1070 if(ini_get('mbstring.func_overload') & 2) 1071 $this->Error('mbstring overloading must be disabled'); 1072 //Disable runtime magic quotes 1073 if(get_magic_quotes_runtime()) 1074 @set_magic_quotes_runtime(0); 1075 } 1076 1077 function _getpageformat($format) 1078 { 1079 $format=strtolower($format); 1080 if(!isset($this->PageFormats[$format])) 1081 $this->Error('Unknown page format: '.$format); 1082 $a=$this->PageFormats[$format]; 1083 return array($a[0]/$this->k, $a[1]/$this->k); 1084 } 1085 1086 function _getfontpath() 1087 { 1088 if(!defined('FPDF_FONTPATH') && is_dir(dirname(__FILE__).'/font')) 1089 define('FPDF_FONTPATH',dirname(__FILE__).'/font/'); 1090 return defined('FPDF_FONTPATH') ? FPDF_FONTPATH : ''; 1091 } 1092 1093 function _beginpage($orientation, $format) 1094 { 1095 $this->page++; 1096 $this->pages[$this->page]=''; 1097 $this->state=2; 1098 $this->x=$this->lMargin; 1099 $this->y=$this->tMargin; 1100 $this->FontFamily=''; 1101 //Check page size 1102 if($orientation=='') 1103 $orientation=$this->DefOrientation; 1104 else 1105 $orientation=strtoupper($orientation[0]); 1106 if($format=='') 1107 $format=$this->DefPageFormat; 1108 else 1109 { 1110 if(is_string($format)) 1111 $format=$this->_getpageformat($format); 1112 } 1113 if($orientation!=$this->CurOrientation || $format[0]!=$this->CurPageFormat[0] || $format[1]!=$this->CurPageFormat[1]) 1114 { 1115 //New size 1116 if($orientation=='P') 1117 { 1118 $this->w=$format[0]; 1119 $this->h=$format[1]; 1120 } 1121 else 1122 { 1123 $this->w=$format[1]; 1124 $this->h=$format[0]; 1125 } 1126 $this->wPt=$this->w*$this->k; 1127 $this->hPt=$this->h*$this->k; 1128 $this->PageBreakTrigger=$this->h-$this->bMargin; 1129 $this->CurOrientation=$orientation; 1130 $this->CurPageFormat=$format; 1131 } 1132 if($orientation!=$this->DefOrientation || $format[0]!=$this->DefPageFormat[0] || $format[1]!=$this->DefPageFormat[1]) 1133 $this->PageSizes[$this->page]=array($this->wPt, $this->hPt); 1134 } 1135 1136 function _endpage() 1137 { 1077 1138 $this->state=1; 1078 $this->_out('%PDF-1.3'); 1139 } 1140 1141 function _escape($s) 1142 { 1143 //Escape special characters in strings 1144 $s=str_replace('\\','\\\\',$s); 1145 $s=str_replace('(','\\(',$s); 1146 $s=str_replace(')','\\)',$s); 1147 $s=str_replace("\r",'\\r',$s); 1148 return $s; 1149 } 1150 1151 function _textstring($s) 1152 { 1153 //Format a text string 1154 return '('.$this->_escape($s).')'; 1155 } 1156 1157 function _UTF8toUTF16($s) 1158 { 1159 //Convert UTF-8 to UTF-16BE with BOM 1160 $res="\xFE\xFF"; 1161 $nb=strlen($s); 1162 $i=0; 1163 while($i<$nb) 1164 { 1165 $c1=ord($s[$i++]); 1166 if($c1>=224) 1167 { 1168 //3-byte character 1169 $c2=ord($s[$i++]); 1170 $c3=ord($s[$i++]); 1171 $res.=chr((($c1 & 0x0F)<<4) + (($c2 & 0x3C)>>2)); 1172 $res.=chr((($c2 & 0x03)<<6) + ($c3 & 0x3F)); 1173 } 1174 elseif($c1>=192) 1175 { 1176 //2-byte character 1177 $c2=ord($s[$i++]); 1178 $res.=chr(($c1 & 0x1C)>>2); 1179 $res.=chr((($c1 & 0x03)<<6) + ($c2 & 0x3F)); 1180 } 1181 else 1182 { 1183 //Single-byte character 1184 $res.="\0".chr($c1); 1185 } 1186 } 1187 return $res; 1188 } 1189 1190 function _dounderline($x, $y, $txt) 1191 { 1192 //Underline text 1193 $up=$this->CurrentFont['up']; 1194 $ut=$this->CurrentFont['ut']; 1195 $w=$this->GetStringWidth($txt)+$this->ws*substr_count($txt,' '); 1196 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); 1197 } 1198 1199 function _parsejpg($file) 1200 { 1201 //Extract info from a JPEG file 1202 $a=GetImageSize($file); 1203 if(!$a) 1204 $this->Error('Missing or incorrect image file: '.$file); 1205 if($a[2]!=2) 1206 $this->Error('Not a JPEG file: '.$file); 1207 if(!isset($a['channels']) || $a['channels']==3) 1208 $colspace='DeviceRGB'; 1209 elseif($a['channels']==4) 1210 $colspace='DeviceCMYK'; 1211 else 1212 $colspace='DeviceGray'; 1213 $bpc=isset($a['bits']) ? $a['bits'] : 8; 1214 //Read whole file 1215 $f=fopen($file,'rb'); 1216 $data=''; 1217 while(!feof($f)) 1218 $data.=fread($f,8192); 1219 fclose($f); 1220 return array('w'=>$a[0], 'h'=>$a[1], 'cs'=>$colspace, 'bpc'=>$bpc, 'f'=>'DCTDecode', 'data'=>$data); 1221 } 1222 1223 function _parsepng($file) 1224 { 1225 //Extract info from a PNG file 1226 $f=fopen($file,'rb'); 1227 if(!$f) 1228 $this->Error('Can\'t open image file: '.$file); 1229 //Check signature 1230 if($this->_readstream($f,8)!=chr(137).'PNG'.chr(13).chr(10).chr(26).chr(10)) 1231 $this->Error('Not a PNG file: '.$file); 1232 //Read header chunk 1233 $this->_readstream($f,4); 1234 if($this->_readstream($f,4)!='IHDR') 1235 $this->Error('Incorrect PNG file: '.$file); 1236 $w=$this->_readint($f); 1237 $h=$this->_readint($f); 1238 $bpc=ord($this->_readstream($f,1)); 1239 if($bpc>8) 1240 $this->Error('16-bit depth not supported: '.$file); 1241 $ct=ord($this->_readstream($f,1)); 1242 if($ct==0) 1243 $colspace='DeviceGray'; 1244 elseif($ct==2) 1245 $colspace='DeviceRGB'; 1246 elseif($ct==3) 1247 $colspace='Indexed'; 1248 else 1249 $this->Error('Alpha channel not supported: '.$file); 1250 if(ord($this->_readstream($f,1))!=0) 1251 $this->Error('Unknown compression method: '.$file); 1252 if(ord($this->_readstream($f,1))!=0) 1253 $this->Error('Unknown filter method: '.$file); 1254 if(ord($this->_readstream($f,1))!=0) 1255 $this->Error('Interlacing not supported: '.$file); 1256 $this->_readstream($f,4); 1257 $parms='/DecodeParms <</Predictor 15 /Colors '.($ct==2 ? 3 : 1).' /BitsPerComponent '.$bpc.' /Columns '.$w.'>>'; 1258 //Scan chunks looking for palette, transparency and image data 1259 $pal=''; 1260 $trns=''; 1261 $data=''; 1262 do 1263 { 1264 $n=$this->_readint($f); 1265 $type=$this->_readstream($f,4); 1266 if($type=='PLTE') 1267 { 1268 //Read palette 1269 $pal=$this->_readstream($f,$n); 1270 $this->_readstream($f,4); 1271 } 1272 elseif($type=='tRNS') 1273 { 1274 //Read transparency info 1275 $t=$this->_readstream($f,$n); 1276 if($ct==0) 1277 $trns=array(ord(substr($t,1,1))); 1278 elseif($ct==2) 1279 $trns=array(ord(substr($t,1,1)), ord(substr($t,3,1)), ord(substr($t,5,1))); 1280 else 1281 { 1282 $pos=strpos($t,chr(0)); 1283 if($pos!==false) 1284 $trns=array($pos); 1285 } 1286 $this->_readstream($f,4); 1287 } 1288 elseif($type=='IDAT') 1289 { 1290 //Read image data block 1291 $data.=$this->_readstream($f,$n); 1292 $this->_readstream($f,4); 1293 } 1294 elseif($type=='IEND') 1295 break; 1296 else 1297 $this->_readstream($f,$n+4); 1298 } 1299 while($n); 1300 if($colspace=='Indexed' && empty($pal)) 1301 $this->Error('Missing palette in '.$file); 1302 fclose($f); 1303 return array('w'=>$w, 'h'=>$h, 'cs'=>$colspace, 'bpc'=>$bpc, 'f'=>'FlateDecode', 'parms'=>$parms, 'pal'=>$pal, 'trns'=>$trns, 'data'=>$data); 1304 } 1305 1306 function _readstream($f, $n) 1307 { 1308 //Read n bytes from stream 1309 $res=''; 1310 while($n>0 && !feof($f)) 1311 { 1312 $s=fread($f,$n); 1313 if($s===false) 1314 $this->Error('Error while reading stream'); 1315 $n-=strlen($s); 1316 $res.=$s; 1317 } 1318 if($n>0) 1319 $this->Error('Unexpected end of stream'); 1320 return $res; 1321 } 1322 1323 function _readint($f) 1324 { 1325 //Read a 4-byte integer from stream 1326 $a=unpack('Ni',$this->_readstream($f,4)); 1327 return $a['i']; 1328 } 1329 1330 function _parsegif($file) 1331 { 1332 //Extract info from a GIF file (via PNG conversion) 1333 if(!function_exists('imagepng')) 1334 $this->Error('GD extension is required for GIF support'); 1335 if(!function_exists('imagecreatefromgif')) 1336 $this->Error('GD has no GIF read support'); 1337 $im=imagecreatefromgif($file); 1338 if(!$im) 1339 $this->Error('Missing or incorrect image file: '.$file); 1340 imageinterlace($im,0); 1341 $tmp=tempnam('.','gif'); 1342 if(!$tmp) 1343 $this->Error('Unable to create a temporary file'); 1344 if(!imagepng($im,$tmp)) 1345 $this->Error('Error while saving to temporary file'); 1346 imagedestroy($im); 1347 $info=$this->_parsepng($tmp); 1348 unlink($tmp); 1349 return $info; 1350 } 1351 1352 function _newobj() 1353 { 1354 //Begin a new object 1355 $this->n++; 1356 $this->offsets[$this->n]=strlen($this->buffer); 1357 $this->_out($this->n.' 0 obj'); 1358 } 1359 1360 function _putstream($s) 1361 { 1362 $this->_out('stream'); 1363 $this->_out($s); 1364 $this->_out('endstream'); 1365 } 1366 1367 function _out($s) 1368 { 1369 //Add a line to the document 1370 if($this->state==2) 1371 $this->pages[$this->page].=$s."\n"; 1372 else 1373 $this->buffer.=$s."\n"; 1079 1374 } 1080 1375 … … 1090 1385 if($this->DefOrientation=='P') 1091 1386 { 1092 $wPt=$this-> fwPt;1093 $hPt=$this-> fhPt;1094 } 1095 else 1096 { 1097 $wPt=$this-> fhPt;1098 $hPt=$this-> fwPt;1387 $wPt=$this->DefPageFormat[0]*$this->k; 1388 $hPt=$this->DefPageFormat[1]*$this->k; 1389 } 1390 else 1391 { 1392 $wPt=$this->DefPageFormat[1]*$this->k; 1393 $hPt=$this->DefPageFormat[0]*$this->k; 1099 1394 } 1100 1395 $filter=($this->compress) ? '/Filter /FlateDecode ' : ''; … … 1105 1400 $this->_out('<</Type /Page'); 1106 1401 $this->_out('/Parent 1 0 R'); 1107 if(isset($this-> OrientationChanges[$n]))1108 $this->_out(sprintf('/MediaBox [0 0 %.2 f %.2f]',$hPt,$wPt));1402 if(isset($this->PageSizes[$n])) 1403 $this->_out(sprintf('/MediaBox [0 0 %.2F %.2F]',$this->PageSizes[$n][0],$this->PageSizes[$n][1])); 1109 1404 $this->_out('/Resources 2 0 R'); 1110 1405 if(isset($this->PageLinks[$n])) … … 1114 1409 foreach($this->PageLinks[$n] as $pl) 1115 1410 { 1116 $rect=sprintf('%.2 f %.2f %.2f %.2f',$pl[0],$pl[1],$pl[0]+$pl[2],$pl[1]-$pl[3]);1411 $rect=sprintf('%.2F %.2F %.2F %.2F',$pl[0],$pl[1],$pl[0]+$pl[2],$pl[1]-$pl[3]); 1117 1412 $annots.='<</Type /Annot /Subtype /Link /Rect ['.$rect.'] /Border [0 0 0] '; 1118 1413 if(is_string($pl[4])) … … 1121 1416 { 1122 1417 $l=$this->links[$pl[4]]; 1123 $h=isset($this-> OrientationChanges[$l[0]]) ? $wPt: $hPt;1124 $annots.=sprintf('/Dest [%d 0 R /XYZ 0 %.2 fnull]>>',1+2*$l[0],$h-$l[1]*$this->k);1418 $h=isset($this->PageSizes[$l[0]]) ? $this->PageSizes[$l[0]][1] : $hPt; 1419 $annots.=sprintf('/Dest [%d 0 R /XYZ 0 %.2F null]>>',1+2*$l[0],$h-$l[1]*$this->k); 1125 1420 } 1126 1421 } … … 1145 1440 $this->_out($kids.']'); 1146 1441 $this->_out('/Count '.$nb); 1147 $this->_out(sprintf('/MediaBox [0 0 %.2 f %.2f]',$wPt,$hPt));1442 $this->_out(sprintf('/MediaBox [0 0 %.2F %.2F]',$wPt,$hPt)); 1148 1443 $this->_out('>>'); 1149 1444 $this->_out('endobj'); … … 1160 1455 $this->_out('endobj'); 1161 1456 } 1162 $mqr=get_magic_quotes_runtime();1163 set_magic_quotes_runtime(0);1164 1457 foreach($this->FontFiles as $file=>$info) 1165 1458 { … … 1167 1460 $this->_newobj(); 1168 1461 $this->FontFiles[$file]['n']=$this->n; 1169 if(defined('FPDF_FONTPATH')) 1170 $file=FPDF_FONTPATH.$file; 1171 $size=filesize($file); 1172 if(!$size) 1462 $font=''; 1463 $f=fopen($this->_getfontpath().$file,'rb',1); 1464 if(!$f) 1173 1465 $this->Error('Font file not found'); 1174 $this->_out('<</Length '.$size); 1175 if(substr($file,-2)=='.z') 1466 while(!feof($f)) 1467 $font.=fread($f,8192); 1468 fclose($f); 1469 $compressed=(substr($file,-2)=='.z'); 1470 if(!$compressed && isset($info['length2'])) 1471 { 1472 $header=(ord($font[0])==128); 1473 if($header) 1474 { 1475 //Strip first binary header 1476 $font=substr($font,6); 1477 } 1478 if($header && ord($font[$info['length1']])==128) 1479 { 1480 //Strip second binary header 1481 $font=substr($font,0,$info['length1']).substr($font,$info['length1']+6); 1482 } 1483 } 1484 $this->_out('<</Length '.strlen($font)); 1485 if($compressed) 1176 1486 $this->_out('/Filter /FlateDecode'); 1177 1487 $this->_out('/Length1 '.$info['length1']); … … 1179 1489 $this->_out('/Length2 '.$info['length2'].' /Length3 0'); 1180 1490 $this->_out('>>'); 1181 $f=fopen($file,'rb'); 1182 $this->_putstream(fread($f,$size)); 1183 fclose($f); 1491 $this->_putstream($font); 1184 1492 $this->_out('endobj'); 1185 1493 } 1186 set_magic_quotes_runtime($mqr);1187 1494 foreach($this->fonts as $k=>$font) 1188 1495 { … … 1198 1505 $this->_out('/BaseFont /'.$name); 1199 1506 $this->_out('/Subtype /Type1'); 1200 if($name!='Symbol' and$name!='ZapfDingbats')1507 if($name!='Symbol' && $name!='ZapfDingbats') 1201 1508 $this->_out('/Encoding /WinAnsiEncoding'); 1202 1509 $this->_out('>>'); 1203 1510 $this->_out('endobj'); 1204 1511 } 1205 elseif($type=='Type1' or$type=='TrueType')1512 elseif($type=='Type1' || $type=='TrueType') 1206 1513 { 1207 1514 //Additional Type1 or TrueType font … … 1273 1580 } 1274 1581 $this->_out('/BitsPerComponent '.$info['bpc']); 1275 $this->_out('/Filter /'.$info['f']); 1582 if(isset($info['f'])) 1583 $this->_out('/Filter /'.$info['f']); 1276 1584 if(isset($info['parms'])) 1277 1585 $this->_out($info['parms']); 1278 if(isset($info['trns']) andis_array($info['trns']))1586 if(isset($info['trns']) && is_array($info['trns'])) 1279 1587 { 1280 1588 $trns=''; … … 1299 1607 } 1300 1608 1609 function _putxobjectdict() 1610 { 1611 foreach($this->images as $image) 1612 $this->_out('/I'.$image['i'].' '.$image['n'].' 0 R'); 1613 } 1614 1615 function _putresourcedict() 1616 { 1617 $this->_out('/ProcSet [/PDF /Text /ImageB /ImageC /ImageI]'); 1618 $this->_out('/Font <<'); 1619 foreach($this->fonts as $font) 1620 $this->_out('/F'.$font['i'].' '.$font['n'].' 0 R'); 1621 $this->_out('>>'); 1622 $this->_out('/XObject <<'); 1623 $this->_putxobjectdict(); 1624 $this->_out('>>'); 1625 } 1626 1301 1627 function _putresources() 1302 1628 { … … 1306 1632 $this->offsets[2]=strlen($this->buffer); 1307 1633 $this->_out('2 0 obj'); 1308 $this->_out('<</ProcSet [/PDF /Text /ImageB /ImageC /ImageI]'); 1309 $this->_out('/Font <<'); 1310 foreach($this->fonts as $font) 1311 $this->_out('/F'.$font['i'].' '.$font['n'].' 0 R'); 1312 $this->_out('>>'); 1313 if(count($this->images)) 1314 { 1315 $this->_out('/XObject <<'); 1316 foreach($this->images as $image) 1317 $this->_out('/I'.$image['i'].' '.$image['n'].' 0 R'); 1318 $this->_out('>>'); 1319 } 1634 $this->_out('<<'); 1635 $this->_putresourcedict(); 1320 1636 $this->_out('>>'); 1321 1637 $this->_out('endobj'); … … 1335 1651 if(!empty($this->creator)) 1336 1652 $this->_out('/Creator '.$this->_textstring($this->creator)); 1337 $this->_out('/CreationDate '.$this->_textstring('D:'. date('YmdHis')));1653 $this->_out('/CreationDate '.$this->_textstring('D:'.@date('YmdHis'))); 1338 1654 } 1339 1655 … … 1358 1674 } 1359 1675 1676 function _putheader() 1677 { 1678 $this->_out('%PDF-'.$this->PDFVersion); 1679 } 1680 1360 1681 function _puttrailer() 1361 1682 { … … 1367 1688 function _enddoc() 1368 1689 { 1690 $this->_putheader(); 1369 1691 $this->_putpages(); 1370 1692 $this->_putresources(); … … 1398 1720 $this->state=3; 1399 1721 } 1400 1401 function _beginpage($orientation)1402 {1403 $this->page++;1404 $this->pages[$this->page]='';1405 $this->state=2;1406 $this->x=$this->lMargin;1407 $this->y=$this->tMargin;1408 $this->FontFamily='';1409 //Page orientation1410 if(!$orientation)1411 $orientation=$this->DefOrientation;1412 else1413 {1414 $orientation=strtoupper($orientation{0});1415 if($orientation!=$this->DefOrientation)1416 $this->OrientationChanges[$this->page]=true;1417 }1418 if($orientation!=$this->CurOrientation)1419 {1420 //Change orientation1421 if($orientation=='P')1422 {1423 $this->wPt=$this->fwPt;1424 $this->hPt=$this->fhPt;1425 $this->w=$this->fw;1426 $this->h=$this->fh;1427 }1428 else1429 {1430 $this->wPt=$this->fhPt;1431 $this->hPt=$this->fwPt;1432 $this->w=$this->fh;1433 $this->h=$this->fw;1434 }1435 $this->PageBreakTrigger=$this->h-$this->bMargin;1436 $this->CurOrientation=$orientation;1437 }1438 }1439 1440 function _endpage()1441 {1442 //End of page contents1443 $this->state=1;1444 }1445 1446 function _newobj()1447 {1448 //Begin a new object1449 $this->n++;1450 $this->offsets[$this->n]=strlen($this->buffer);1451 $this->_out($this->n.' 0 obj');1452 }1453 1454 function _dounderline($x,$y,$txt)1455 {1456 //Underline text1457 $up=$this->CurrentFont['up'];1458 $ut=$this->CurrentFont['ut'];1459 $w=$this->GetStringWidth($txt)+$this->ws*substr_count($txt,' ');1460 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);1461 }1462 1463 function _parsejpg($file)1464 {1465 //Extract info from a JPEG file1466 $a=GetImageSize($file);1467 if(!$a)1468 $this->Error('Missing or incorrect image file: '.$file);1469 if($a[2]!=2)1470 $this->Error('Not a JPEG file: '.$file);1471 if(!isset($a['channels']) or $a['channels']==3)1472 $colspace='DeviceRGB';1473 elseif($a['channels']==4)1474 $colspace='DeviceCMYK';1475 else1476 $colspace='DeviceGray';1477 $bpc=isset($a['bits']) ? $a['bits'] : 8;1478 //Read whole file1479 $f=fopen($file,'rb');1480 $data='';1481 while(!feof($f))1482 $data.=fread($f,4096);1483 fclose($f);1484 return array('w'=>$a[0],'h'=>$a[1],'cs'=>$colspace,'bpc'=>$bpc,'f'=>'DCTDecode','data'=>$data);1485 }1486 1487 function _parsepng($file)1488 {1489 //Extract info from a PNG file1490 $f=fopen($file,'rb');1491 if(!$f)1492 $this->Error('Can\'t open image file: '.$file);1493 //Check signature1494 if(fread($f,8)!=chr(137).'PNG'.chr(13).chr(10).chr(26).chr(10))1495 $this->Error('Not a PNG file: '.$file);1496 //Read header chunk1497 fread($f,4);1498 if(fread($f,4)!='IHDR')1499 $this->Error('Incorrect PNG file: '.$file);1500 $w=$this->_freadint($f);1501 $h=$this->_freadint($f);1502 $bpc=ord(fread($f,1));1503 if($bpc>8)1504 $this->Error('16-bit depth not supported: '.$file);1505 $ct=ord(fread($f,1));1506 if($ct==0)1507 $colspace='DeviceGray';1508 elseif($ct==2)1509 $colspace='DeviceRGB';1510 elseif($ct==3)1511 $colspace='Indexed';1512 else1513 $this->Error('Alpha channel not supported: '.$file);1514 if(ord(fread($f,1))!=0)1515 $this->Error('Unknown compression method: '.$file);1516 if(ord(fread($f,1))!=0)1517 $this->Error('Unknown filter method: '.$file);1518 if(ord(fread($f,1))!=0)1519 $this->Error('Interlacing not supported: '.$file);1520 fread($f,4);1521 $parms='/DecodeParms <</Predictor 15 /Colors '.($ct==2 ? 3 : 1).' /BitsPerComponent '.$bpc.' /Columns '.$w.'>>';1522 //Scan chunks looking for palette, transparency and image data1523 $pal='';1524 $trns='';1525 $data='';1526 do1527 {1528 $n=$this->_freadint($f);1529 $type=fread($f,4);1530 if($type=='PLTE')1531 {1532 //Read palette1533 $pal=fread($f,$n);1534 fread($f,4);1535 }1536 elseif($type=='tRNS')1537 {1538 //Read transparency info1539 $t=fread($f,$n);1540 if($ct==0)1541 $trns=array(ord(substr($t,1,1)));1542 elseif($ct==2)1543 $trns=array(ord(substr($t,1,1)),ord(substr($t,3,1)),ord(substr($t,5,1)));1544 else1545 {1546 $pos=strpos($t,chr(0));1547 if(is_int($pos))1548 $trns=array($pos);1549 }1550 fread($f,4);1551 }1552 elseif($type=='IDAT')1553 {1554 //Read image data block1555 $data.=fread($f,$n);1556 fread($f,4);1557 }1558 elseif($type=='IEND')1559 break;1560 else1561 fread($f,$n+4);1562 }1563 while($n);1564 if($colspace=='Indexed' and empty($pal))1565 $this->Error('Missing palette in '.$file);1566 fclose($f);1567 return array('w'=>$w,'h'=>$h,'cs'=>$colspace,'bpc'=>$bpc,'f'=>'FlateDecode','parms'=>$parms,'pal'=>$pal,'trns'=>$trns,'data'=>$data);1568 }1569 1570 function _freadint($f)1571 {1572 //Read a 4-byte integer from file1573 $i=ord(fread($f,1))<<24;1574 $i+=ord(fread($f,1))<<16;1575 $i+=ord(fread($f,1))<<8;1576 $i+=ord(fread($f,1));1577 return $i;1578 }1579 1580 function _textstring($s)1581 {1582 //Format a text string1583 return '('.$this->_escape($s).')';1584 }1585 1586 function _escape($s)1587 {1588 //Add \ before \, ( and )1589 return str_replace(')','\\)',str_replace('(','\\(',str_replace('\\','\\\\',$s)));1590 }1591 1592 function _putstream($s)1593 {1594 $this->_out('stream');1595 $this->_out($s);1596 $this->_out('endstream');1597 }1598 1599 function _out($s)1600 {1601 //Add a line to the document1602 if($this->state==2)1603 $this->pages[$this->page].=$s."\n";1604 else1605 $this->buffer.=$s."\n";1606 }1607 1722 //End of class 1608 1723 } 1609 1724 1610 1725 //Handle special IE contype request 1611 if(isset($ HTTP_SERVER_VARS['HTTP_USER_AGENT']) and $HTTP_SERVER_VARS['HTTP_USER_AGENT']=='contype')1612 { 1613 Header('Content-Type: application/pdf');1726 if(isset($_SERVER['HTTP_USER_AGENT']) && $_SERVER['HTTP_USER_AGENT']=='contype') 1727 { 1728 header('Content-Type: application/pdf'); 1614 1729 exit; 1615 1730 } 1616 1731 1617 }1618 1732 ?>
