Changeset 20993 for branches/version-2_11-dev/data/module/fpdf/fpdf.php
- Timestamp:
- 2011/06/25 16:45:04 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/version-2_11-dev/data/module/fpdf/fpdf.php
r20119 r20993 3 3 * FPDF * 4 4 * * 5 * Version: 1. 6*6 * Date: 20 08-08-03*5 * Version: 1.7 * 6 * Date: 2011-06-18 * 7 7 * Author: Olivier PLATHEY * 8 8 *******************************************************************************/ 9 9 10 define('FPDF_VERSION','1. 6');10 define('FPDF_VERSION','1.7'); 11 11 12 12 class FPDF 13 13 { 14 var $page; //current page number 15 var $n; //current object number 16 var $offsets; //array of object offsets 17 var $buffer; //buffer holding in-memory PDF 18 var $pages; //array containing pages 19 var $state; //current document state 20 var $compress; //compression flag 21 var $k; //scale factor (number of points in user unit) 22 var $DefOrientation; //default orientation 23 var $CurOrientation; //current orientation 24 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 30 var $lMargin; //left margin 31 var $tMargin; //top margin 32 var $rMargin; //right margin 33 var $bMargin; //page break margin 34 var $cMargin; //cell margin 35 var $x,$y; //current position in user unit 36 var $lasth; //height of last printed cell 37 var $LineWidth; //line width in user unit 38 var $CoreFonts; //array of standard font names 39 var $fonts; //array of used fonts 40 var $FontFiles; //array of font files 41 var $diffs; //array of encoding differences 42 var $FontFamily; //current font family 43 var $FontStyle; //current font style 44 var $underline; //underlining flag 45 var $CurrentFont; //current font info 46 var $FontSizePt; //current font size in points 47 var $FontSize; //current font size in user unit 48 var $DrawColor; //commands for drawing color 49 var $FillColor; //commands for filling color 50 var $TextColor; //commands for text color 51 var $ColorFlag; //indicates whether fill and text colors are different 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 56 var $AutoPageBreak; //automatic page breaking 57 var $PageBreakTrigger; //threshold used to trigger page breaks 58 var $InHeader; //flag set when processing header 59 var $InFooter; //flag set when processing footer 60 var $ZoomMode; //zoom display mode 61 var $LayoutMode; //layout display mode 62 var $title; //title 63 var $subject; //subject 64 var $author; //author 65 var $keywords; //keywords 66 var $creator; //creator 67 var $AliasNbPages; //alias for total number of pages 68 var $PDFVersion; //PDF version number 14 var $page; // current page number 15 var $n; // current object number 16 var $offsets; // array of object offsets 17 var $buffer; // buffer holding in-memory PDF 18 var $pages; // array containing pages 19 var $state; // current document state 20 var $compress; // compression flag 21 var $k; // scale factor (number of points in user unit) 22 var $DefOrientation; // default orientation 23 var $CurOrientation; // current orientation 24 var $StdPageSizes; // standard page sizes 25 var $DefPageSize; // default page size 26 var $CurPageSize; // current page size 27 var $PageSizes; // used for pages with non default sizes or orientations 28 var $wPt, $hPt; // dimensions of current page in points 29 var $w, $h; // dimensions of current page in user unit 30 var $lMargin; // left margin 31 var $tMargin; // top margin 32 var $rMargin; // right margin 33 var $bMargin; // page break margin 34 var $cMargin; // cell margin 35 var $x, $y; // current position in user unit 36 var $lasth; // height of last printed cell 37 var $LineWidth; // line width in user unit 38 var $fontpath; // path containing fonts 39 var $CoreFonts; // array of core font names 40 var $fonts; // array of used fonts 41 var $FontFiles; // array of font files 42 var $diffs; // array of encoding differences 43 var $FontFamily; // current font family 44 var $FontStyle; // current font style 45 var $underline; // underlining flag 46 var $CurrentFont; // current font info 47 var $FontSizePt; // current font size in points 48 var $FontSize; // current font size in user unit 49 var $DrawColor; // commands for drawing color 50 var $FillColor; // commands for filling color 51 var $TextColor; // commands for text color 52 var $ColorFlag; // indicates whether fill and text colors are different 53 var $ws; // word spacing 54 var $images; // array of used images 55 var $PageLinks; // array of links in pages 56 var $links; // array of internal links 57 var $AutoPageBreak; // automatic page breaking 58 var $PageBreakTrigger; // threshold used to trigger page breaks 59 var $InHeader; // flag set when processing header 60 var $InFooter; // flag set when processing footer 61 var $ZoomMode; // zoom display mode 62 var $LayoutMode; // layout display mode 63 var $title; // title 64 var $subject; // subject 65 var $author; // author 66 var $keywords; // keywords 67 var $creator; // creator 68 var $AliasNbPages; // alias for total number of pages 69 var $PDFVersion; // PDF version number 69 70 70 71 /******************************************************************************* … … 73 74 * * 74 75 *******************************************************************************/ 75 function FPDF($orientation='P', $unit='mm', $ format='A4')76 { 77 // Some checks76 function FPDF($orientation='P', $unit='mm', $size='A4') 77 { 78 // Some checks 78 79 $this->_dochecks(); 79 //Initialization of properties 80 $this->page=0; 81 $this->n=2; 82 $this->buffer=''; 83 $this->pages=array(); 84 $this->PageSizes=array(); 85 $this->state=0; 86 $this->fonts=array(); 87 $this->FontFiles=array(); 88 $this->diffs=array(); 89 $this->images=array(); 90 $this->links=array(); 91 $this->InHeader=false; 92 $this->InFooter=false; 93 $this->lasth=0; 94 $this->FontFamily=''; 95 $this->FontStyle=''; 96 $this->FontSizePt=12; 97 $this->underline=false; 98 $this->DrawColor='0 G'; 99 $this->FillColor='0 g'; 100 $this->TextColor='0 g'; 101 $this->ColorFlag=false; 102 $this->ws=0; 103 //Standard fonts 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'); 108 //Scale factor 80 // Initialization of properties 81 $this->page = 0; 82 $this->n = 2; 83 $this->buffer = ''; 84 $this->pages = array(); 85 $this->PageSizes = array(); 86 $this->state = 0; 87 $this->fonts = array(); 88 $this->FontFiles = array(); 89 $this->diffs = array(); 90 $this->images = array(); 91 $this->links = array(); 92 $this->InHeader = false; 93 $this->InFooter = false; 94 $this->lasth = 0; 95 $this->FontFamily = ''; 96 $this->FontStyle = ''; 97 $this->FontSizePt = 12; 98 $this->underline = false; 99 $this->DrawColor = '0 G'; 100 $this->FillColor = '0 g'; 101 $this->TextColor = '0 g'; 102 $this->ColorFlag = false; 103 $this->ws = 0; 104 // Font path 105 if(defined('FPDF_FONTPATH')) 106 { 107 $this->fontpath = FPDF_FONTPATH; 108 if(substr($this->fontpath,-1)!='/' && substr($this->fontpath,-1)!='\\') 109 $this->fontpath .= '/'; 110 } 111 elseif(is_dir(dirname(__FILE__).'/font')) 112 $this->fontpath = dirname(__FILE__).'/font/'; 113 else 114 $this->fontpath = ''; 115 // Core fonts 116 $this->CoreFonts = array('courier', 'helvetica', 'times', 'symbol', 'zapfdingbats'); 117 // Scale factor 109 118 if($unit=='pt') 110 $this->k =1;119 $this->k = 1; 111 120 elseif($unit=='mm') 112 $this->k =72/25.4;121 $this->k = 72/25.4; 113 122 elseif($unit=='cm') 114 $this->k =72/2.54;123 $this->k = 72/2.54; 115 124 elseif($unit=='in') 116 $this->k =72;125 $this->k = 72; 117 126 else 118 127 $this->Error('Incorrect unit: '.$unit); 119 // Page format120 $this-> PageFormats=array('a3'=>array(841.89,1190.55), 'a4'=>array(595.28,841.89), 'a5'=>array(420.94,595.28),128 // Page sizes 129 $this->StdPageSizes = array('a3'=>array(841.89,1190.55), 'a4'=>array(595.28,841.89), 'a5'=>array(420.94,595.28), 121 130 'letter'=>array(612,792), 'legal'=>array(612,1008)); 122 if(is_string($format)) 123 $format=$this->_getpageformat($format); 124 $this->DefPageFormat=$format; 125 $this->CurPageFormat=$format; 126 //Page orientation 127 $orientation=strtolower($orientation); 131 $size = $this->_getpagesize($size); 132 $this->DefPageSize = $size; 133 $this->CurPageSize = $size; 134 // Page orientation 135 $orientation = strtolower($orientation); 128 136 if($orientation=='p' || $orientation=='portrait') 129 137 { 130 $this->DefOrientation ='P';131 $this->w =$this->DefPageFormat[0];132 $this->h =$this->DefPageFormat[1];138 $this->DefOrientation = 'P'; 139 $this->w = $size[0]; 140 $this->h = $size[1]; 133 141 } 134 142 elseif($orientation=='l' || $orientation=='landscape') 135 143 { 136 $this->DefOrientation ='L';137 $this->w =$this->DefPageFormat[1];138 $this->h =$this->DefPageFormat[0];144 $this->DefOrientation = 'L'; 145 $this->w = $size[1]; 146 $this->h = $size[0]; 139 147 } 140 148 else 141 149 $this->Error('Incorrect orientation: '.$orientation); 142 $this->CurOrientation =$this->DefOrientation;143 $this->wPt =$this->w*$this->k;144 $this->hPt =$this->h*$this->k;145 // Page margins (1 cm)146 $margin =28.35/$this->k;150 $this->CurOrientation = $this->DefOrientation; 151 $this->wPt = $this->w*$this->k; 152 $this->hPt = $this->h*$this->k; 153 // Page margins (1 cm) 154 $margin = 28.35/$this->k; 147 155 $this->SetMargins($margin,$margin); 148 // Interior cell margin (1 mm)149 $this->cMargin =$margin/10;150 // Line width (0.2 mm)151 $this->LineWidth =.567/$this->k;152 // Automatic page break156 // Interior cell margin (1 mm) 157 $this->cMargin = $margin/10; 158 // Line width (0.2 mm) 159 $this->LineWidth = .567/$this->k; 160 // Automatic page break 153 161 $this->SetAutoPageBreak(true,2*$margin); 154 // Full widthdisplay mode155 $this->SetDisplayMode(' fullwidth');156 // Enable compression162 // Default display mode 163 $this->SetDisplayMode('default'); 164 // Enable compression 157 165 $this->SetCompression(true); 158 // Set default PDF version number159 $this->PDFVersion ='1.3';166 // Set default PDF version number 167 $this->PDFVersion = '1.3'; 160 168 } 161 169 162 170 function SetMargins($left, $top, $right=null) 163 171 { 164 // Set left, top and right margins165 $this->lMargin =$left;166 $this->tMargin =$top;172 // Set left, top and right margins 173 $this->lMargin = $left; 174 $this->tMargin = $top; 167 175 if($right===null) 168 $right =$left;169 $this->rMargin =$right;176 $right = $left; 177 $this->rMargin = $right; 170 178 } 171 179 172 180 function SetLeftMargin($margin) 173 181 { 174 // Set left margin175 $this->lMargin =$margin;182 // Set left margin 183 $this->lMargin = $margin; 176 184 if($this->page>0 && $this->x<$margin) 177 $this->x =$margin;185 $this->x = $margin; 178 186 } 179 187 180 188 function SetTopMargin($margin) 181 189 { 182 // Set top margin183 $this->tMargin =$margin;190 // Set top margin 191 $this->tMargin = $margin; 184 192 } 185 193 186 194 function SetRightMargin($margin) 187 195 { 188 // Set right margin189 $this->rMargin =$margin;196 // Set right margin 197 $this->rMargin = $margin; 190 198 } 191 199 192 200 function SetAutoPageBreak($auto, $margin=0) 193 201 { 194 // Set auto page break mode and triggering margin195 $this->AutoPageBreak =$auto;196 $this->bMargin =$margin;197 $this->PageBreakTrigger =$this->h-$margin;198 } 199 200 function SetDisplayMode($zoom, $layout=' continuous')201 { 202 // Set display mode in viewer202 // Set auto page break mode and triggering margin 203 $this->AutoPageBreak = $auto; 204 $this->bMargin = $margin; 205 $this->PageBreakTrigger = $this->h-$margin; 206 } 207 208 function SetDisplayMode($zoom, $layout='default') 209 { 210 // Set display mode in viewer 203 211 if($zoom=='fullpage' || $zoom=='fullwidth' || $zoom=='real' || $zoom=='default' || !is_string($zoom)) 204 $this->ZoomMode =$zoom;212 $this->ZoomMode = $zoom; 205 213 else 206 214 $this->Error('Incorrect zoom display mode: '.$zoom); 207 215 if($layout=='single' || $layout=='continuous' || $layout=='two' || $layout=='default') 208 $this->LayoutMode =$layout;216 $this->LayoutMode = $layout; 209 217 else 210 218 $this->Error('Incorrect layout display mode: '.$layout); … … 213 221 function SetCompression($compress) 214 222 { 215 // Set page compression223 // Set page compression 216 224 if(function_exists('gzcompress')) 217 $this->compress =$compress;218 else 219 $this->compress =false;225 $this->compress = $compress; 226 else 227 $this->compress = false; 220 228 } 221 229 222 230 function SetTitle($title, $isUTF8=false) 223 231 { 224 // Title of document232 // Title of document 225 233 if($isUTF8) 226 $title =$this->_UTF8toUTF16($title);227 $this->title =$title;234 $title = $this->_UTF8toUTF16($title); 235 $this->title = $title; 228 236 } 229 237 230 238 function SetSubject($subject, $isUTF8=false) 231 239 { 232 // Subject of document240 // Subject of document 233 241 if($isUTF8) 234 $subject =$this->_UTF8toUTF16($subject);235 $this->subject =$subject;242 $subject = $this->_UTF8toUTF16($subject); 243 $this->subject = $subject; 236 244 } 237 245 238 246 function SetAuthor($author, $isUTF8=false) 239 247 { 240 // Author of document248 // Author of document 241 249 if($isUTF8) 242 $author =$this->_UTF8toUTF16($author);243 $this->author =$author;250 $author = $this->_UTF8toUTF16($author); 251 $this->author = $author; 244 252 } 245 253 246 254 function SetKeywords($keywords, $isUTF8=false) 247 255 { 248 // Keywords of document256 // Keywords of document 249 257 if($isUTF8) 250 $keywords =$this->_UTF8toUTF16($keywords);251 $this->keywords =$keywords;258 $keywords = $this->_UTF8toUTF16($keywords); 259 $this->keywords = $keywords; 252 260 } 253 261 254 262 function SetCreator($creator, $isUTF8=false) 255 263 { 256 // Creator of document264 // Creator of document 257 265 if($isUTF8) 258 $creator =$this->_UTF8toUTF16($creator);259 $this->creator =$creator;266 $creator = $this->_UTF8toUTF16($creator); 267 $this->creator = $creator; 260 268 } 261 269 262 270 function AliasNbPages($alias='{nb}') 263 271 { 264 // Define an alias for total number of pages265 $this->AliasNbPages =$alias;272 // Define an alias for total number of pages 273 $this->AliasNbPages = $alias; 266 274 } 267 275 268 276 function Error($msg) 269 277 { 270 // Fatal error278 // Fatal error 271 279 die('<b>FPDF error:</b> '.$msg); 272 280 } … … 274 282 function Open() 275 283 { 276 // Begin document277 $this->state =1;284 // Begin document 285 $this->state = 1; 278 286 } 279 287 280 288 function Close() 281 289 { 282 // Terminate document290 // Terminate document 283 291 if($this->state==3) 284 292 return; 285 293 if($this->page==0) 286 294 $this->AddPage(); 287 // Page footer288 $this->InFooter =true;295 // Page footer 296 $this->InFooter = true; 289 297 $this->Footer(); 290 $this->InFooter =false;291 // Close page298 $this->InFooter = false; 299 // Close page 292 300 $this->_endpage(); 293 // Close document301 // Close document 294 302 $this->_enddoc(); 295 303 } 296 304 297 function AddPage($orientation='', $ format='')298 { 299 // Start a new page305 function AddPage($orientation='', $size='') 306 { 307 // Start a new page 300 308 if($this->state==0) 301 309 $this->Open(); 302 $family =$this->FontFamily;303 $style =$this->FontStyle.($this->underline ? 'U' : '');304 $ size=$this->FontSizePt;305 $lw =$this->LineWidth;306 $dc =$this->DrawColor;307 $fc =$this->FillColor;308 $tc =$this->TextColor;309 $cf =$this->ColorFlag;310 $family = $this->FontFamily; 311 $style = $this->FontStyle.($this->underline ? 'U' : ''); 312 $fontsize = $this->FontSizePt; 313 $lw = $this->LineWidth; 314 $dc = $this->DrawColor; 315 $fc = $this->FillColor; 316 $tc = $this->TextColor; 317 $cf = $this->ColorFlag; 310 318 if($this->page>0) 311 319 { 312 // Page footer313 $this->InFooter =true;320 // Page footer 321 $this->InFooter = true; 314 322 $this->Footer(); 315 $this->InFooter =false;316 // Close page323 $this->InFooter = false; 324 // Close page 317 325 $this->_endpage(); 318 326 } 319 // Start new page320 $this->_beginpage($orientation,$ format);321 // Set line cap style to square327 // Start new page 328 $this->_beginpage($orientation,$size); 329 // Set line cap style to square 322 330 $this->_out('2 J'); 323 // Set line width324 $this->LineWidth =$lw;331 // Set line width 332 $this->LineWidth = $lw; 325 333 $this->_out(sprintf('%.2F w',$lw*$this->k)); 326 // Set font334 // Set font 327 335 if($family) 328 $this->SetFont($family,$style,$ size);329 // Set colors330 $this->DrawColor =$dc;336 $this->SetFont($family,$style,$fontsize); 337 // Set colors 338 $this->DrawColor = $dc; 331 339 if($dc!='0 G') 332 340 $this->_out($dc); 333 $this->FillColor =$fc;341 $this->FillColor = $fc; 334 342 if($fc!='0 g') 335 343 $this->_out($fc); 336 $this->TextColor =$tc;337 $this->ColorFlag =$cf;338 // Page header339 $this->InHeader =true;344 $this->TextColor = $tc; 345 $this->ColorFlag = $cf; 346 // Page header 347 $this->InHeader = true; 340 348 $this->Header(); 341 $this->InHeader =false;342 // Restore line width349 $this->InHeader = false; 350 // Restore line width 343 351 if($this->LineWidth!=$lw) 344 352 { 345 $this->LineWidth =$lw;353 $this->LineWidth = $lw; 346 354 $this->_out(sprintf('%.2F w',$lw*$this->k)); 347 355 } 348 // Restore font356 // Restore font 349 357 if($family) 350 $this->SetFont($family,$style,$ size);351 // Restore colors358 $this->SetFont($family,$style,$fontsize); 359 // Restore colors 352 360 if($this->DrawColor!=$dc) 353 361 { 354 $this->DrawColor =$dc;362 $this->DrawColor = $dc; 355 363 $this->_out($dc); 356 364 } 357 365 if($this->FillColor!=$fc) 358 366 { 359 $this->FillColor =$fc;367 $this->FillColor = $fc; 360 368 $this->_out($fc); 361 369 } 362 $this->TextColor =$tc;363 $this->ColorFlag =$cf;370 $this->TextColor = $tc; 371 $this->ColorFlag = $cf; 364 372 } 365 373 366 374 function Header() 367 375 { 368 // To be implemented in your own inherited class376 // To be implemented in your own inherited class 369 377 } 370 378 371 379 function Footer() 372 380 { 373 // To be implemented in your own inherited class381 // To be implemented in your own inherited class 374 382 } 375 383 376 384 function PageNo() 377 385 { 378 // Get current page number386 // Get current page number 379 387 return $this->page; 380 388 } … … 382 390 function SetDrawColor($r, $g=null, $b=null) 383 391 { 384 // Set color for all stroking operations392 // Set color for all stroking operations 385 393 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);394 $this->DrawColor = sprintf('%.3F G',$r/255); 395 else 396 $this->DrawColor = sprintf('%.3F %.3F %.3F RG',$r/255,$g/255,$b/255); 389 397 if($this->page>0) 390 398 $this->_out($this->DrawColor); … … 393 401 function SetFillColor($r, $g=null, $b=null) 394 402 { 395 // Set color for all filling operations403 // Set color for all filling operations 396 404 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);400 $this->ColorFlag =($this->FillColor!=$this->TextColor);405 $this->FillColor = sprintf('%.3F g',$r/255); 406 else 407 $this->FillColor = sprintf('%.3F %.3F %.3F rg',$r/255,$g/255,$b/255); 408 $this->ColorFlag = ($this->FillColor!=$this->TextColor); 401 409 if($this->page>0) 402 410 $this->_out($this->FillColor); … … 405 413 function SetTextColor($r, $g=null, $b=null) 406 414 { 407 // Set color for text415 // Set color for text 408 416 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);412 $this->ColorFlag =($this->FillColor!=$this->TextColor);417 $this->TextColor = sprintf('%.3F g',$r/255); 418 else 419 $this->TextColor = sprintf('%.3F %.3F %.3F rg',$r/255,$g/255,$b/255); 420 $this->ColorFlag = ($this->FillColor!=$this->TextColor); 413 421 } 414 422 415 423 function GetStringWidth($s) 416 424 { 417 // Get width of a string in the current font418 $s =(string)$s;419 $cw =&$this->CurrentFont['cw'];420 $w =0;421 $l =strlen($s);425 // Get width of a string in the current font 426 $s = (string)$s; 427 $cw = &$this->CurrentFont['cw']; 428 $w = 0; 429 $l = strlen($s); 422 430 for($i=0;$i<$l;$i++) 423 $w +=$cw[$s[$i]];431 $w += $cw[$s[$i]]; 424 432 return $w*$this->FontSize/1000; 425 433 } … … 427 435 function SetLineWidth($width) 428 436 { 429 // Set line width430 $this->LineWidth =$width;437 // Set line width 438 $this->LineWidth = $width; 431 439 if($this->page>0) 432 440 $this->_out(sprintf('%.2F w',$width*$this->k)); … … 435 443 function Line($x1, $y1, $x2, $y2) 436 444 { 437 // Draw a line445 // Draw a line 438 446 $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 447 } … … 441 449 function Rect($x, $y, $w, $h, $style='') 442 450 { 443 // Draw a rectangle451 // Draw a rectangle 444 452 if($style=='F') 445 $op ='f';453 $op = 'f'; 446 454 elseif($style=='FD' || $style=='DF') 447 $op ='B';448 else 449 $op ='S';455 $op = 'B'; 456 else 457 $op = 'S'; 450 458 $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 459 } … … 453 461 function AddFont($family, $style='', $file='') 454 462 { 455 // Add a TrueType or Type1 font456 $family =strtolower($family);463 // Add a TrueType, OpenType or Type1 font 464 $family = strtolower($family); 457 465 if($file=='') 458 $file=str_replace(' ','',$family).strtolower($style).'.php'; 459 if($family=='arial') 460 $family='helvetica'; 461 $style=strtoupper($style); 466 $file = str_replace(' ','',$family).strtolower($style).'.php'; 467 $style = strtoupper($style); 462 468 if($style=='IB') 463 $style ='BI';464 $fontkey =$family.$style;469 $style = 'BI'; 470 $fontkey = $family.$style; 465 471 if(isset($this->fonts[$fontkey])) 466 472 return; 467 include($this->_getfontpath().$file); 468 if(!isset($name)) 469 $this->Error('Could not include font definition file'); 470 $i=count($this->fonts)+1; 471 $this->fonts[$fontkey]=array('i'=>$i, 'type'=>$type, 'name'=>$name, 'desc'=>$desc, 'up'=>$up, 'ut'=>$ut, 'cw'=>$cw, 'enc'=>$enc, 'file'=>$file); 472 if($diff) 473 { 474 //Search existing encodings 475 $d=0; 476 $nb=count($this->diffs); 477 for($i=1;$i<=$nb;$i++) 478 { 479 if($this->diffs[$i]==$diff) 480 { 481 $d=$i; 482 break; 483 } 484 } 485 if($d==0) 486 { 487 $d=$nb+1; 488 $this->diffs[$d]=$diff; 489 } 490 $this->fonts[$fontkey]['diff']=$d; 491 } 492 if($file) 493 { 494 if($type=='TrueType') 495 $this->FontFiles[$file]=array('length1'=>$originalsize); 473 $info = $this->_loadfont($file); 474 $info['i'] = count($this->fonts)+1; 475 if(!empty($info['diff'])) 476 { 477 // Search existing encodings 478 $n = array_search($info['diff'],$this->diffs); 479 if(!$n) 480 { 481 $n = count($this->diffs)+1; 482 $this->diffs[$n] = $info['diff']; 483 } 484 $info['diffn'] = $n; 485 } 486 if(!empty($info['file'])) 487 { 488 // Embedded font 489 if($info['type']=='TrueType') 490 $this->FontFiles[$info['file']] = array('length1'=>$info['originalsize']); 496 491 else 497 $this->FontFiles[$file]=array('length1'=>$size1, 'length2'=>$size2); 498 } 492 $this->FontFiles[$info['file']] = array('length1'=>$info['size1'], 'length2'=>$info['size2']); 493 } 494 $this->fonts[$fontkey] = $info; 499 495 } 500 496 501 497 function SetFont($family, $style='', $size=0) 502 498 { 503 //Select a font; size given in points 504 global $fpdf_charwidths; 505 506 $family=strtolower($family); 499 // Select a font; size given in points 507 500 if($family=='') 508 $family=$this->FontFamily; 509 if($family=='arial') 510 $family='helvetica'; 511 elseif($family=='symbol' || $family=='zapfdingbats') 512 $style=''; 513 $style=strtoupper($style); 501 $family = $this->FontFamily; 502 else 503 $family = strtolower($family); 504 $style = strtoupper($style); 514 505 if(strpos($style,'U')!==false) 515 506 { 516 $this->underline =true;517 $style =str_replace('U','',$style);518 } 519 else 520 $this->underline =false;507 $this->underline = true; 508 $style = str_replace('U','',$style); 509 } 510 else 511 $this->underline = false; 521 512 if($style=='IB') 522 $style ='BI';513 $style = 'BI'; 523 514 if($size==0) 524 $size =$this->FontSizePt;525 // Test if font is already selected515 $size = $this->FontSizePt; 516 // Test if font is already selected 526 517 if($this->FontFamily==$family && $this->FontStyle==$style && $this->FontSizePt==$size) 527 518 return; 528 // Test if used for the first time529 $fontkey =$family.$style;519 // Test if font is already loaded 520 $fontkey = $family.$style; 530 521 if(!isset($this->fonts[$fontkey])) 531 522 { 532 //Check if one of the standard fonts 533 if(isset($this->CoreFonts[$fontkey])) 534 { 535 if(!isset($fpdf_charwidths[$fontkey])) 536 { 537 //Load metric file 538 $file=$family; 539 if($family=='times' || $family=='helvetica') 540 $file.=strtolower($style); 541 include($this->_getfontpath().$file.'.php'); 542 if(!isset($fpdf_charwidths[$fontkey])) 543 $this->Error('Could not include font metric file'); 544 } 545 $i=count($this->fonts)+1; 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); 523 // Test if one of the core fonts 524 if($family=='arial') 525 $family = 'helvetica'; 526 if(in_array($family,$this->CoreFonts)) 527 { 528 if($family=='symbol' || $family=='zapfdingbats') 529 $style = ''; 530 $fontkey = $family.$style; 531 if(!isset($this->fonts[$fontkey])) 532 $this->AddFont($family,$style); 549 533 } 550 534 else 551 535 $this->Error('Undefined font: '.$family.' '.$style); 552 536 } 553 // Select it554 $this->FontFamily =$family;555 $this->FontStyle =$style;556 $this->FontSizePt =$size;557 $this->FontSize =$size/$this->k;558 $this->CurrentFont =&$this->fonts[$fontkey];537 // Select it 538 $this->FontFamily = $family; 539 $this->FontStyle = $style; 540 $this->FontSizePt = $size; 541 $this->FontSize = $size/$this->k; 542 $this->CurrentFont = &$this->fonts[$fontkey]; 559 543 if($this->page>0) 560 544 $this->_out(sprintf('BT /F%d %.2F Tf ET',$this->CurrentFont['i'],$this->FontSizePt)); … … 563 547 function SetFontSize($size) 564 548 { 565 // Set font size in points549 // Set font size in points 566 550 if($this->FontSizePt==$size) 567 551 return; 568 $this->FontSizePt =$size;569 $this->FontSize =$size/$this->k;552 $this->FontSizePt = $size; 553 $this->FontSize = $size/$this->k; 570 554 if($this->page>0) 571 555 $this->_out(sprintf('BT /F%d %.2F Tf ET',$this->CurrentFont['i'],$this->FontSizePt)); … … 574 558 function AddLink() 575 559 { 576 // Create a new internal link577 $n =count($this->links)+1;578 $this->links[$n] =array(0, 0);560 // Create a new internal link 561 $n = count($this->links)+1; 562 $this->links[$n] = array(0, 0); 579 563 return $n; 580 564 } … … 582 566 function SetLink($link, $y=0, $page=-1) 583 567 { 584 // Set destination of internal link568 // Set destination of internal link 585 569 if($y==-1) 586 $y =$this->y;570 $y = $this->y; 587 571 if($page==-1) 588 $page =$this->page;589 $this->links[$link] =array($page, $y);572 $page = $this->page; 573 $this->links[$link] = array($page, $y); 590 574 } 591 575 592 576 function Link($x, $y, $w, $h, $link) 593 577 { 594 // Put a link on the page595 $this->PageLinks[$this->page][] =array($x*$this->k, $this->hPt-$y*$this->k, $w*$this->k, $h*$this->k, $link);578 // Put a link on the page 579 $this->PageLinks[$this->page][] = array($x*$this->k, $this->hPt-$y*$this->k, $w*$this->k, $h*$this->k, $link); 596 580 } 597 581 598 582 function Text($x, $y, $txt) 599 583 { 600 // Output a string601 $s =sprintf('BT %.2F %.2F Td (%s) Tj ET',$x*$this->k,($this->h-$y)*$this->k,$this->_escape($txt));584 // Output a string 585 $s = sprintf('BT %.2F %.2F Td (%s) Tj ET',$x*$this->k,($this->h-$y)*$this->k,$this->_escape($txt)); 602 586 if($this->underline && $txt!='') 603 $s .=' '.$this->_dounderline($x,$y,$txt);587 $s .= ' '.$this->_dounderline($x,$y,$txt); 604 588 if($this->ColorFlag) 605 $s ='q '.$this->TextColor.' '.$s.' Q';589 $s = 'q '.$this->TextColor.' '.$s.' Q'; 606 590 $this->_out($s); 607 591 } … … 609 593 function AcceptPageBreak() 610 594 { 611 // Accept automatic page break or not595 // Accept automatic page break or not 612 596 return $this->AutoPageBreak; 613 597 } … … 615 599 function Cell($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link='') 616 600 { 617 // Output a cell618 $k =$this->k;601 // Output a cell 602 $k = $this->k; 619 603 if($this->y+$h>$this->PageBreakTrigger && !$this->InHeader && !$this->InFooter && $this->AcceptPageBreak()) 620 604 { 621 // Automatic page break622 $x =$this->x;623 $ws =$this->ws;605 // Automatic page break 606 $x = $this->x; 607 $ws = $this->ws; 624 608 if($ws>0) 625 609 { 626 $this->ws =0;610 $this->ws = 0; 627 611 $this->_out('0 Tw'); 628 612 } 629 $this->AddPage($this->CurOrientation,$this->CurPage Format);630 $this->x =$x;613 $this->AddPage($this->CurOrientation,$this->CurPageSize); 614 $this->x = $x; 631 615 if($ws>0) 632 616 { 633 $this->ws =$ws;617 $this->ws = $ws; 634 618 $this->_out(sprintf('%.3F Tw',$ws*$k)); 635 619 } 636 620 } 637 621 if($w==0) 638 $w =$this->w-$this->rMargin-$this->x;639 $s ='';622 $w = $this->w-$this->rMargin-$this->x; 623 $s = ''; 640 624 if($fill || $border==1) 641 625 { 642 626 if($fill) 643 $op =($border==1) ? 'B' : 'f';627 $op = ($border==1) ? 'B' : 'f'; 644 628 else 645 $op ='S';646 $s =sprintf('%.2F %.2F %.2F %.2F re %s ',$this->x*$k,($this->h-$this->y)*$k,$w*$k,-$h*$k,$op);629 $op = 'S'; 630 $s = sprintf('%.2F %.2F %.2F %.2F re %s ',$this->x*$k,($this->h-$this->y)*$k,$w*$k,-$h*$k,$op); 647 631 } 648 632 if(is_string($border)) 649 633 { 650 $x =$this->x;651 $y =$this->y;634 $x = $this->x; 635 $y = $this->y; 652 636 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);637 $s .= sprintf('%.2F %.2F m %.2F %.2F l S ',$x*$k,($this->h-$y)*$k,$x*$k,($this->h-($y+$h))*$k); 654 638 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);639 $s .= sprintf('%.2F %.2F m %.2F %.2F l S ',$x*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-$y)*$k); 656 640 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);641 $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 642 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);643 $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 644 } 661 645 if($txt!=='') 662 646 { 663 647 if($align=='R') 664 $dx =$w-$this->cMargin-$this->GetStringWidth($txt);648 $dx = $w-$this->cMargin-$this->GetStringWidth($txt); 665 649 elseif($align=='C') 666 $dx =($w-$this->GetStringWidth($txt))/2;650 $dx = ($w-$this->GetStringWidth($txt))/2; 667 651 else 668 $dx =$this->cMargin;652 $dx = $this->cMargin; 669 653 if($this->ColorFlag) 670 $s .='q '.$this->TextColor.' ';671 $txt2 =str_replace(')','\\)',str_replace('(','\\(',str_replace('\\','\\\\',$txt)));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);654 $s .= 'q '.$this->TextColor.' '; 655 $txt2 = str_replace(')','\\)',str_replace('(','\\(',str_replace('\\','\\\\',$txt))); 656 $s .= sprintf('BT %.2F %.2F Td (%s) Tj ET',($this->x+$dx)*$k,($this->h-($this->y+.5*$h+.3*$this->FontSize))*$k,$txt2); 673 657 if($this->underline) 674 $s .=' '.$this->_dounderline($this->x+$dx,$this->y+.5*$h+.3*$this->FontSize,$txt);658 $s .= ' '.$this->_dounderline($this->x+$dx,$this->y+.5*$h+.3*$this->FontSize,$txt); 675 659 if($this->ColorFlag) 676 $s .=' Q';660 $s .= ' Q'; 677 661 if($link) 678 662 $this->Link($this->x+$dx,$this->y+.5*$h-.5*$this->FontSize,$this->GetStringWidth($txt),$this->FontSize,$link); … … 680 664 if($s) 681 665 $this->_out($s); 682 $this->lasth =$h;666 $this->lasth = $h; 683 667 if($ln>0) 684 668 { 685 // Go to next line686 $this->y +=$h;669 // Go to next line 670 $this->y += $h; 687 671 if($ln==1) 688 $this->x =$this->lMargin;689 } 690 else 691 $this->x +=$w;672 $this->x = $this->lMargin; 673 } 674 else 675 $this->x += $w; 692 676 } 693 677 694 678 function MultiCell($w, $h, $txt, $border=0, $align='J', $fill=false) 695 679 { 696 // Output text with automatic or explicit line breaks697 $cw =&$this->CurrentFont['cw'];680 // Output text with automatic or explicit line breaks 681 $cw = &$this->CurrentFont['cw']; 698 682 if($w==0) 699 $w =$this->w-$this->rMargin-$this->x;700 $wmax =($w-2*$this->cMargin)*1000/$this->FontSize;701 $s =str_replace("\r",'',$txt);702 $nb =strlen($s);683 $w = $this->w-$this->rMargin-$this->x; 684 $wmax = ($w-2*$this->cMargin)*1000/$this->FontSize; 685 $s = str_replace("\r",'',$txt); 686 $nb = strlen($s); 703 687 if($nb>0 && $s[$nb-1]=="\n") 704 688 $nb--; 705 $b =0;689 $b = 0; 706 690 if($border) 707 691 { 708 692 if($border==1) 709 693 { 710 $border ='LTRB';711 $b ='LRT';712 $b2 ='LR';694 $border = 'LTRB'; 695 $b = 'LRT'; 696 $b2 = 'LR'; 713 697 } 714 698 else 715 699 { 716 $b2 ='';700 $b2 = ''; 717 701 if(strpos($border,'L')!==false) 718 $b2 .='L';702 $b2 .= 'L'; 719 703 if(strpos($border,'R')!==false) 720 $b2 .='R';721 $b =(strpos($border,'T')!==false) ? $b2.'T' : $b2;722 } 723 } 724 $sep =-1;725 $i =0;726 $j =0;727 $l =0;728 $ns =0;729 $nl =1;704 $b2 .= 'R'; 705 $b = (strpos($border,'T')!==false) ? $b2.'T' : $b2; 706 } 707 } 708 $sep = -1; 709 $i = 0; 710 $j = 0; 711 $l = 0; 712 $ns = 0; 713 $nl = 1; 730 714 while($i<$nb) 731 715 { 732 // Get next character733 $c =$s[$i];716 // Get next character 717 $c = $s[$i]; 734 718 if($c=="\n") 735 719 { 736 // Explicit line break720 // Explicit line break 737 721 if($this->ws>0) 738 722 { 739 $this->ws =0;723 $this->ws = 0; 740 724 $this->_out('0 Tw'); 741 725 } 742 726 $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill); 743 727 $i++; 744 $sep =-1;745 $j =$i;746 $l =0;747 $ns =0;728 $sep = -1; 729 $j = $i; 730 $l = 0; 731 $ns = 0; 748 732 $nl++; 749 733 if($border && $nl==2) 750 $b =$b2;734 $b = $b2; 751 735 continue; 752 736 } 753 737 if($c==' ') 754 738 { 755 $sep =$i;756 $ls =$l;739 $sep = $i; 740 $ls = $l; 757 741 $ns++; 758 742 } 759 $l +=$cw[$c];743 $l += $cw[$c]; 760 744 if($l>$wmax) 761 745 { 762 // Automatic line break746 // Automatic line break 763 747 if($sep==-1) 764 748 { … … 767 751 if($this->ws>0) 768 752 { 769 $this->ws =0;753 $this->ws = 0; 770 754 $this->_out('0 Tw'); 771 755 } … … 776 760 if($align=='J') 777 761 { 778 $this->ws =($ns>1) ? ($wmax-$ls)/1000*$this->FontSize/($ns-1) : 0;762 $this->ws = ($ns>1) ? ($wmax-$ls)/1000*$this->FontSize/($ns-1) : 0; 779 763 $this->_out(sprintf('%.3F Tw',$this->ws*$this->k)); 780 764 } 781 765 $this->Cell($w,$h,substr($s,$j,$sep-$j),$b,2,$align,$fill); 782 $i =$sep+1;766 $i = $sep+1; 783 767 } 784 $sep =-1;785 $j =$i;786 $l =0;787 $ns =0;768 $sep = -1; 769 $j = $i; 770 $l = 0; 771 $ns = 0; 788 772 $nl++; 789 773 if($border && $nl==2) 790 $b =$b2;774 $b = $b2; 791 775 } 792 776 else 793 777 $i++; 794 778 } 795 // Last chunk779 // Last chunk 796 780 if($this->ws>0) 797 781 { 798 $this->ws =0;782 $this->ws = 0; 799 783 $this->_out('0 Tw'); 800 784 } 801 785 if($border && strpos($border,'B')!==false) 802 $b .='B';786 $b .= 'B'; 803 787 $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill); 804 $this->x =$this->lMargin;788 $this->x = $this->lMargin; 805 789 } 806 790 807 791 function Write($h, $txt, $link='') 808 792 { 809 // Output text in flowing mode810 $cw =&$this->CurrentFont['cw'];811 $w =$this->w-$this->rMargin-$this->x;812 $wmax =($w-2*$this->cMargin)*1000/$this->FontSize;813 $s =str_replace("\r",'',$txt);814 $nb =strlen($s);815 $sep =-1;816 $i =0;817 $j =0;818 $l =0;819 $nl =1;793 // Output text in flowing mode 794 $cw = &$this->CurrentFont['cw']; 795 $w = $this->w-$this->rMargin-$this->x; 796 $wmax = ($w-2*$this->cMargin)*1000/$this->FontSize; 797 $s = str_replace("\r",'',$txt); 798 $nb = strlen($s); 799 $sep = -1; 800 $i = 0; 801 $j = 0; 802 $l = 0; 803 $nl = 1; 820 804 while($i<$nb) 821 805 { 822 // Get next character823 $c =$s[$i];806 // Get next character 807 $c = $s[$i]; 824 808 if($c=="\n") 825 809 { 826 // Explicit line break810 // Explicit line break 827 811 $this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link); 828 812 $i++; 829 $sep =-1;830 $j =$i;831 $l =0;813 $sep = -1; 814 $j = $i; 815 $l = 0; 832 816 if($nl==1) 833 817 { 834 $this->x =$this->lMargin;835 $w =$this->w-$this->rMargin-$this->x;836 $wmax =($w-2*$this->cMargin)*1000/$this->FontSize;818 $this->x = $this->lMargin; 819 $w = $this->w-$this->rMargin-$this->x; 820 $wmax = ($w-2*$this->cMargin)*1000/$this->FontSize; 837 821 } 838 822 $nl++; … … 840 824 } 841 825 if($c==' ') 842 $sep =$i;843 $l +=$cw[$c];826 $sep = $i; 827 $l += $cw[$c]; 844 828 if($l>$wmax) 845 829 { 846 // Automatic line break830 // Automatic line break 847 831 if($sep==-1) 848 832 { 849 833 if($this->x>$this->lMargin) 850 834 { 851 // Move to next line852 $this->x =$this->lMargin;853 $this->y +=$h;854 $w =$this->w-$this->rMargin-$this->x;855 $wmax =($w-2*$this->cMargin)*1000/$this->FontSize;835 // Move to next line 836 $this->x = $this->lMargin; 837 $this->y += $h; 838 $w = $this->w-$this->rMargin-$this->x; 839 $wmax = ($w-2*$this->cMargin)*1000/$this->FontSize; 856 840 $i++; 857 841 $nl++; … … 865 849 { 866 850 $this->Cell($w,$h,substr($s,$j,$sep-$j),0,2,'',0,$link); 867 $i =$sep+1;851 $i = $sep+1; 868 852 } 869 $sep =-1;870 $j =$i;871 $l =0;853 $sep = -1; 854 $j = $i; 855 $l = 0; 872 856 if($nl==1) 873 857 { 874 $this->x =$this->lMargin;875 $w =$this->w-$this->rMargin-$this->x;876 $wmax =($w-2*$this->cMargin)*1000/$this->FontSize;858 $this->x = $this->lMargin; 859 $w = $this->w-$this->rMargin-$this->x; 860 $wmax = ($w-2*$this->cMargin)*1000/$this->FontSize; 877 861 } 878 862 $nl++; … … 881 865 $i++; 882 866 } 883 // Last chunk867 // Last chunk 884 868 if($i!=$j) 885 869 $this->Cell($l/1000*$this->FontSize,$h,substr($s,$j),0,0,'',0,$link); … … 888 872 function Ln($h=null) 889 873 { 890 // Line feed; default value is last cell height891 $this->x =$this->lMargin;874 // Line feed; default value is last cell height 875 $this->x = $this->lMargin; 892 876 if($h===null) 893 $this->y +=$this->lasth;894 else 895 $this->y +=$h;877 $this->y += $this->lasth; 878 else 879 $this->y += $h; 896 880 } 897 881 898 882 function Image($file, $x=null, $y=null, $w=0, $h=0, $type='', $link='') 899 883 { 900 // Put an image on the page884 // Put an image on the page 901 885 if(!isset($this->images[$file])) 902 886 { 903 // First use of this image, get info887 // First use of this image, get info 904 888 if($type=='') 905 889 { 906 $pos =strrpos($file,'.');890 $pos = strrpos($file,'.'); 907 891 if(!$pos) 908 892 $this->Error('Image file has no extension and no type was specified: '.$file); 909 $type =substr($file,$pos+1);910 } 911 $type =strtolower($type);893 $type = substr($file,$pos+1); 894 } 895 $type = strtolower($type); 912 896 if($type=='jpeg') 913 $type ='jpg';914 $mtd ='_parse'.$type;897 $type = 'jpg'; 898 $mtd = '_parse'.$type; 915 899 if(!method_exists($this,$mtd)) 916 900 $this->Error('Unsupported image type: '.$type); 917 $info=$this->$mtd($file); 918 $info['i']=count($this->images)+1; 919 $this->images[$file]=$info; 920 } 921 else 922 $info=$this->images[$file]; 923 //Automatic width and height calculation if needed 901 $info = $this->$mtd($file); 902 $info['i'] = count($this->images)+1; 903 $this->images[$file] = $info; 904 } 905 else 906 $info = $this->images[$file]; 907 908 // Automatic width and height calculation if needed 924 909 if($w==0 && $h==0) 925 910 { 926 //Put image at 72 dpi 927 $w=$info['w']/$this->k; 928 $h=$info['h']/$this->k; 929 } 930 elseif($w==0) 931 $w=$h*$info['w']/$info['h']; 932 elseif($h==0) 933 $h=$w*$info['h']/$info['w']; 934 //Flowing mode 911 // Put image at 96 dpi 912 $w = -96; 913 $h = -96; 914 } 915 if($w<0) 916 $w = -$info['w']*72/$w/$this->k; 917 if($h<0) 918 $h = -$info['h']*72/$h/$this->k; 919 if($w==0) 920 $w = $h*$info['w']/$info['h']; 921 if($h==0) 922 $h = $w*$info['h']/$info['w']; 923 924 // Flowing mode 935 925 if($y===null) 936 926 { 937 927 if($this->y+$h>$this->PageBreakTrigger && !$this->InHeader && !$this->InFooter && $this->AcceptPageBreak()) 938 928 { 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 } 929 // Automatic page break 930 $x2 = $this->x; 931 $this->AddPage($this->CurOrientation,$this->CurPageSize); 932 $this->x = $x2; 933 } 934 $y = $this->y; 935 $this->y += $h; 936 } 937 947 938 if($x===null) 948 $x =$this->x;939 $x = $this->x; 949 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'])); 950 941 if($link) … … 954 945 function GetX() 955 946 { 956 // Get x position947 // Get x position 957 948 return $this->x; 958 949 } … … 960 951 function SetX($x) 961 952 { 962 // Set x position953 // Set x position 963 954 if($x>=0) 964 $this->x =$x;965 else 966 $this->x =$this->w+$x;955 $this->x = $x; 956 else 957 $this->x = $this->w+$x; 967 958 } 968 959 969 960 function GetY() 970 961 { 971 // Get y position962 // Get y position 972 963 return $this->y; 973 964 } … … 975 966 function SetY($y) 976 967 { 977 // Set y position and reset x978 $this->x =$this->lMargin;968 // Set y position and reset x 969 $this->x = $this->lMargin; 979 970 if($y>=0) 980 $this->y =$y;981 else 982 $this->y =$this->h+$y;971 $this->y = $y; 972 else 973 $this->y = $this->h+$y; 983 974 } 984 975 985 976 function SetXY($x, $y) 986 977 { 987 // Set x and y positions978 // Set x and y positions 988 979 $this->SetY($y); 989 980 $this->SetX($x); … … 992 983 function Output($name='', $dest='') 993 984 { 994 // Output PDF to some destination985 // Output PDF to some destination 995 986 if($this->state<3) 996 987 $this->Close(); 997 $dest =strtoupper($dest);988 $dest = strtoupper($dest); 998 989 if($dest=='') 999 990 { 1000 991 if($name=='') 1001 992 { 1002 $name ='doc.pdf';1003 $dest ='I';993 $name = 'doc.pdf'; 994 $dest = 'I'; 1004 995 } 1005 996 else 1006 $dest ='F';997 $dest = 'F'; 1007 998 } 1008 999 switch($dest) 1009 1000 { 1010 1001 case 'I': 1011 //Send to standard output 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') 1002 // Send to standard output 1003 $this->_checkoutput(); 1004 if(PHP_SAPI!='cli') 1015 1005 { 1016 // We send to a browser1006 // We send to a browser 1017 1007 header('Content-Type: application/pdf'); 1018 if(headers_sent())1019 $this->Error('Some data has already been output, can\'t send PDF file');1020 header('Content-Length: '.strlen($this->buffer));1021 1008 header('Content-Disposition: inline; filename="'.$name.'"'); 1022 1009 header('Cache-Control: private, max-age=0, must-revalidate'); 1023 1010 header('Pragma: public'); 1024 ini_set('zlib.output_compression','0');1025 1011 } 1026 1012 echo $this->buffer; 1027 1013 break; 1028 1014 case 'D': 1029 //Download file 1030 if(ob_get_length()) 1031 $this->Error('Some data has already been output, can\'t send PDF file'); 1015 // Download file 1016 $this->_checkoutput(); 1032 1017 header('Content-Type: application/x-download'); 1033 if(headers_sent())1034 $this->Error('Some data has already been output, can\'t send PDF file');1035 header('Content-Length: '.strlen($this->buffer));1036 1018 header('Content-Disposition: attachment; filename="'.$name.'"'); 1037 1019 header('Cache-Control: private, max-age=0, must-revalidate'); 1038 1020 header('Pragma: public'); 1039 ini_set('zlib.output_compression','0');1040 1021 echo $this->buffer; 1041 1022 break; 1042 1023 case 'F': 1043 // Save to local file1044 $f =fopen($name,'wb');1024 // Save to local file 1025 $f = fopen($name,'wb'); 1045 1026 if(!$f) 1046 1027 $this->Error('Unable to create output file: '.$name); … … 1049 1030 break; 1050 1031 case 'S': 1051 // Return as a string1032 // Return as a string 1052 1033 return $this->buffer; 1053 1034 default: … … 1064 1045 function _dochecks() 1065 1046 { 1066 // Check availability of %F1047 // Check availability of %F 1067 1048 if(sprintf('%.1F',1.0)!='1.0') 1068 1049 $this->Error('This version of PHP is not supported'); 1069 // Check mbstring overloading1050 // Check mbstring overloading 1070 1051 if(ini_get('mbstring.func_overload') & 2) 1071 1052 $this->Error('mbstring overloading must be disabled'); 1072 // Disable runtime magic quotes1053 // Ensure runtime magic quotes are disabled 1073 1054 if(get_magic_quotes_runtime()) 1074 1055 @set_magic_quotes_runtime(0); 1075 1056 } 1076 1057 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) 1058 function _checkoutput() 1059 { 1060 if(PHP_SAPI!='cli') 1061 { 1062 if(headers_sent($file,$line)) 1063 $this->Error("Some data has already been output, can't send PDF file (output started at $file:$line)"); 1064 } 1065 if(ob_get_length()) 1066 { 1067 // The output buffer is not empty 1068 if(preg_match('/^(\xEF\xBB\xBF)?\s*$/',ob_get_contents())) 1069 { 1070 // It contains only a UTF-8 BOM and/or whitespace, let's clean it 1071 ob_clean(); 1072 } 1073 else 1074 $this->Error("Some data has already been output, can't send PDF file"); 1075 } 1076 } 1077 1078 function _getpagesize($size) 1079 { 1080 if(is_string($size)) 1081 { 1082 $size = strtolower($size); 1083 if(!isset($this->StdPageSizes[$size])) 1084 $this->Error('Unknown page size: '.$size); 1085 $a = $this->StdPageSizes[$size]; 1086 return array($a[0]/$this->k, $a[1]/$this->k); 1087 } 1088 else 1089 { 1090 if($size[0]>$size[1]) 1091 return array($size[1], $size[0]); 1092 else 1093 return $size; 1094 } 1095 } 1096 1097 function _beginpage($orientation, $size) 1094 1098 { 1095 1099 $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 size1100 $this->pages[$this->page] = ''; 1101 $this->state = 2; 1102 $this->x = $this->lMargin; 1103 $this->y = $this->tMargin; 1104 $this->FontFamily = ''; 1105 // Check page size and orientation 1102 1106 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 1107 $orientation = $this->DefOrientation; 1108 else 1109 $orientation = strtoupper($orientation[0]); 1110 if($size=='') 1111 $size = $this->DefPageSize; 1112 else 1113 $size = $this->_getpagesize($size); 1114 if($orientation!=$this->CurOrientation || $size[0]!=$this->CurPageSize[0] || $size[1]!=$this->CurPageSize[1]) 1115 { 1116 // New size or orientation 1116 1117 if($orientation=='P') 1117 1118 { 1118 $this->w =$format[0];1119 $this->h =$format[1];1119 $this->w = $size[0]; 1120 $this->h = $size[1]; 1120 1121 } 1121 1122 else 1122 1123 { 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->CurPage Format=$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);1124 $this->w = $size[1]; 1125 $this->h = $size[0]; 1126 } 1127 $this->wPt = $this->w*$this->k; 1128 $this->hPt = $this->h*$this->k; 1129 $this->PageBreakTrigger = $this->h-$this->bMargin; 1130 $this->CurOrientation = $orientation; 1131 $this->CurPageSize = $size; 1132 } 1133 if($orientation!=$this->DefOrientation || $size[0]!=$this->DefPageSize[0] || $size[1]!=$this->DefPageSize[1]) 1134 $this->PageSizes[$this->page] = array($this->wPt, $this->hPt); 1134 1135 } 1135 1136 1136 1137 function _endpage() 1137 1138 { 1138 $this->state=1; 1139 $this->state = 1; 1140 } 1141 1142 function _loadfont($font) 1143 { 1144 // Load a font definition file from the font directory 1145 include($this->fontpath.$font); 1146 $a = get_defined_vars(); 1147 if(!isset($a['name'])) 1148 $this->Error('Could not include font definition file'); 1149 return $a; 1139 1150 } 1140 1151 1141 1152 function _escape($s) 1142 1153 { 1143 // Escape special characters in strings1144 $s =str_replace('\\','\\\\',$s);1145 $s =str_replace('(','\\(',$s);1146 $s =str_replace(')','\\)',$s);1147 $s =str_replace("\r",'\\r',$s);1154 // Escape special characters in strings 1155 $s = str_replace('\\','\\\\',$s); 1156 $s = str_replace('(','\\(',$s); 1157 $s = str_replace(')','\\)',$s); 1158 $s = str_replace("\r",'\\r',$s); 1148 1159 return $s; 1149 1160 } … … 1151 1162 function _textstring($s) 1152 1163 { 1153 // Format a text string1164 // Format a text string 1154 1165 return '('.$this->_escape($s).')'; 1155 1166 } … … 1157 1168 function _UTF8toUTF16($s) 1158 1169 { 1159 // Convert UTF-8 to UTF-16BE with BOM1160 $res ="\xFE\xFF";1161 $nb =strlen($s);1162 $i =0;1170 // Convert UTF-8 to UTF-16BE with BOM 1171 $res = "\xFE\xFF"; 1172 $nb = strlen($s); 1173 $i = 0; 1163 1174 while($i<$nb) 1164 1175 { 1165 $c1 =ord($s[$i++]);1176 $c1 = ord($s[$i++]); 1166 1177 if($c1>=224) 1167 1178 { 1168 // 3-byte character1169 $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));1179 // 3-byte character 1180 $c2 = ord($s[$i++]); 1181 $c3 = ord($s[$i++]); 1182 $res .= chr((($c1 & 0x0F)<<4) + (($c2 & 0x3C)>>2)); 1183 $res .= chr((($c2 & 0x03)<<6) + ($c3 & 0x3F)); 1173 1184 } 1174 1185 elseif($c1>=192) 1175 1186 { 1176 // 2-byte character1177 $c2 =ord($s[$i++]);1178 $res .=chr(($c1 & 0x1C)>>2);1179 $res .=chr((($c1 & 0x03)<<6) + ($c2 & 0x3F));1187 // 2-byte character 1188 $c2 = ord($s[$i++]); 1189 $res .= chr(($c1 & 0x1C)>>2); 1190 $res .= chr((($c1 & 0x03)<<6) + ($c2 & 0x3F)); 1180 1191 } 1181 1192 else 1182 1193 { 1183 // Single-byte character1184 $res .="\0".chr($c1);1194 // Single-byte character 1195 $res .= "\0".chr($c1); 1185 1196 } 1186 1197 } … … 1190 1201 function _dounderline($x, $y, $txt) 1191 1202 { 1192 // Underline text1193 $up =$this->CurrentFont['up'];1194 $ut =$this->CurrentFont['ut'];1195 $w =$this->GetStringWidth($txt)+$this->ws*substr_count($txt,' ');1203 // Underline text 1204 $up = $this->CurrentFont['up']; 1205 $ut = $this->CurrentFont['ut']; 1206 $w = $this->GetStringWidth($txt)+$this->ws*substr_count($txt,' '); 1196 1207 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 1208 } … … 1199 1210 function _parsejpg($file) 1200 1211 { 1201 // Extract info from a JPEG file1202 $a =GetImageSize($file);1212 // Extract info from a JPEG file 1213 $a = getimagesize($file); 1203 1214 if(!$a) 1204 1215 $this->Error('Missing or incorrect image file: '.$file); … … 1206 1217 $this->Error('Not a JPEG file: '.$file); 1207 1218 if(!isset($a['channels']) || $a['channels']==3) 1208 $colspace ='DeviceRGB';1219 $colspace = 'DeviceRGB'; 1209 1220 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); 1221 $colspace = 'DeviceCMYK'; 1222 else 1223 $colspace = 'DeviceGray'; 1224 $bpc = isset($a['bits']) ? $a['bits'] : 8; 1225 $data = file_get_contents($file); 1220 1226 return array('w'=>$a[0], 'h'=>$a[1], 'cs'=>$colspace, 'bpc'=>$bpc, 'f'=>'DCTDecode', 'data'=>$data); 1221 1227 } … … 1223 1229 function _parsepng($file) 1224 1230 { 1225 // Extract info from a PNG file1226 $f =fopen($file,'rb');1231 // Extract info from a PNG file 1232 $f = fopen($file,'rb'); 1227 1233 if(!$f) 1228 1234 $this->Error('Can\'t open image file: '.$file); 1229 //Check signature 1235 $info = $this->_parsepngstream($f,$file); 1236 fclose($f); 1237 return $info; 1238 } 1239 1240 function _parsepngstream($f, $file) 1241 { 1242 // Check signature 1230 1243 if($this->_readstream($f,8)!=chr(137).'PNG'.chr(13).chr(10).chr(26).chr(10)) 1231 1244 $this->Error('Not a PNG file: '.$file); 1232 //Read header chunk 1245 1246 // Read header chunk 1233 1247 $this->_readstream($f,4); 1234 1248 if($this->_readstream($f,4)!='IHDR') 1235 1249 $this->Error('Incorrect PNG file: '.$file); 1236 $w =$this->_readint($f);1237 $h =$this->_readint($f);1238 $bpc =ord($this->_readstream($f,1));1250 $w = $this->_readint($f); 1251 $h = $this->_readint($f); 1252 $bpc = ord($this->_readstream($f,1)); 1239 1253 if($bpc>8) 1240 1254 $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';1255 $ct = ord($this->_readstream($f,1)); 1256 if($ct==0 || $ct==4) 1257 $colspace = 'DeviceGray'; 1258 elseif($ct==2 || $ct==6) 1259 $colspace = 'DeviceRGB'; 1246 1260 elseif($ct==3) 1247 $colspace ='Indexed';1248 else 1249 $this->Error(' Alpha channel not supported: '.$file);1261 $colspace = 'Indexed'; 1262 else 1263 $this->Error('Unknown color type: '.$file); 1250 1264 if(ord($this->_readstream($f,1))!=0) 1251 1265 $this->Error('Unknown compression method: '.$file); … … 1255 1269 $this->Error('Interlacing not supported: '.$file); 1256 1270 $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=''; 1271 $dp = '/Predictor 15 /Colors '.($colspace=='DeviceRGB' ? 3 : 1).' /BitsPerComponent '.$bpc.' /Columns '.$w; 1272 1273 // Scan chunks looking for palette, transparency and image data 1274 $pal = ''; 1275 $trns = ''; 1276 $data = ''; 1262 1277 do 1263 1278 { 1264 $n =$this->_readint($f);1265 $type =$this->_readstream($f,4);1279 $n = $this->_readint($f); 1280 $type = $this->_readstream($f,4); 1266 1281 if($type=='PLTE') 1267 1282 { 1268 // Read palette1269 $pal =$this->_readstream($f,$n);1283 // Read palette 1284 $pal = $this->_readstream($f,$n); 1270 1285 $this->_readstream($f,4); 1271 1286 } 1272 1287 elseif($type=='tRNS') 1273 1288 { 1274 // Read transparency info1275 $t =$this->_readstream($f,$n);1289 // Read transparency info 1290 $t = $this->_readstream($f,$n); 1276 1291 if($ct==0) 1277 $trns =array(ord(substr($t,1,1)));1292 $trns = array(ord(substr($t,1,1))); 1278 1293 elseif($ct==2) 1279 $trns =array(ord(substr($t,1,1)), ord(substr($t,3,1)), ord(substr($t,5,1)));1294 $trns = array(ord(substr($t,1,1)), ord(substr($t,3,1)), ord(substr($t,5,1))); 1280 1295 else 1281 1296 { 1282 $pos =strpos($t,chr(0));1297 $pos = strpos($t,chr(0)); 1283 1298 if($pos!==false) 1284 $trns =array($pos);1299 $trns = array($pos); 1285 1300 } 1286 1301 $this->_readstream($f,4); … … 1288 1303 elseif($type=='IDAT') 1289 1304 { 1290 // Read image data block1291 $data .=$this->_readstream($f,$n);1305 // Read image data block 1306 $data .= $this->_readstream($f,$n); 1292 1307 $this->_readstream($f,4); 1293 1308 } … … 1298 1313 } 1299 1314 while($n); 1315 1300 1316 if($colspace=='Indexed' && empty($pal)) 1301 1317 $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); 1318 $info = array('w'=>$w, 'h'=>$h, 'cs'=>$colspace, 'bpc'=>$bpc, 'f'=>'FlateDecode', 'dp'=>$dp, 'pal'=>$pal, 'trns'=>$trns); 1319 if($ct>=4) 1320 { 1321 // Extract alpha channel 1322 if(!function_exists('gzuncompress')) 1323 $this->Error('Zlib not available, can\'t handle alpha channel: '.$file); 1324 $data = gzuncompress($data); 1325 $color = ''; 1326 $alpha = ''; 1327 if($ct==4) 1328 { 1329 // Gray image 1330 $len = 2*$w; 1331 for($i=0;$i<$h;$i++) 1332 { 1333 $pos = (1+$len)*$i; 1334 $color .= $data[$pos]; 1335 $alpha .= $data[$pos]; 1336 $line = substr($data,$pos+1,$len); 1337 $color .= preg_replace('/(.)./s','$1',$line); 1338 $alpha .= preg_replace('/.(.)/s','$1',$line); 1339 } 1340 } 1341 else 1342 { 1343 // RGB image 1344 $len = 4*$w; 1345 for($i=0;$i<$h;$i++) 1346 { 1347 $pos = (1+$len)*$i; 1348 $color .= $data[$pos]; 1349 $alpha .= $data[$pos]; 1350 $line = substr($data,$pos+1,$len); 1351 $color .= preg_replace('/(.{3})./s','$1',$line); 1352 $alpha .= preg_replace('/.{3}(.)/s','$1',$line); 1353 } 1354 } 1355 unset($data); 1356 $data = gzcompress($color); 1357 $info['smask'] = gzcompress($alpha); 1358 if($this->PDFVersion<'1.4') 1359 $this->PDFVersion = '1.4'; 1360 } 1361 $info['data'] = $data; 1362 return $info; 1304 1363 } 1305 1364 1306 1365 function _readstream($f, $n) 1307 1366 { 1308 // Read n bytes from stream1309 $res ='';1367 // Read n bytes from stream 1368 $res = ''; 1310 1369 while($n>0 && !feof($f)) 1311 1370 { 1312 $s =fread($f,$n);1371 $s = fread($f,$n); 1313 1372 if($s===false) 1314 1373 $this->Error('Error while reading stream'); 1315 $n -=strlen($s);1316 $res .=$s;1374 $n -= strlen($s); 1375 $res .= $s; 1317 1376 } 1318 1377 if($n>0) … … 1323 1382 function _readint($f) 1324 1383 { 1325 // Read a 4-byte integer from stream1326 $a =unpack('Ni',$this->_readstream($f,4));1384 // Read a 4-byte integer from stream 1385 $a = unpack('Ni',$this->_readstream($f,4)); 1327 1386 return $a['i']; 1328 1387 } … … 1330 1389 function _parsegif($file) 1331 1390 { 1332 // Extract info from a GIF file (via PNG conversion)1391 // Extract info from a GIF file (via PNG conversion) 1333 1392 if(!function_exists('imagepng')) 1334 1393 $this->Error('GD extension is required for GIF support'); 1335 1394 if(!function_exists('imagecreatefromgif')) 1336 1395 $this->Error('GD has no GIF read support'); 1337 $im =imagecreatefromgif($file);1396 $im = imagecreatefromgif($file); 1338 1397 if(!$im) 1339 1398 $this->Error('Missing or incorrect image file: '.$file); 1340 1399 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); 1400 $f = @fopen('php://temp','rb+'); 1401 if($f) 1402 { 1403 // Perform conversion in memory 1404 ob_start(); 1405 imagepng($im); 1406 $data = ob_get_clean(); 1407 imagedestroy($im); 1408 fwrite($f,$data); 1409 rewind($f); 1410 $info = $this->_parsepngstream($f,$file); 1411 fclose($f); 1412 } 1413 else 1414 { 1415 // Use temporary file 1416 $tmp = tempnam('.','gif'); 1417 if(!$tmp) 1418 $this->Error('Unable to create a temporary file'); 1419 if(!imagepng($im,$tmp)) 1420 $this->Error('Error while saving to temporary file'); 1421 imagedestroy($im); 1422 $info = $this->_parsepng($tmp); 1423 unlink($tmp); 1424 } 1349 1425 return $info; 1350 1426 } … … 1352 1428 function _newobj() 1353 1429 { 1354 // Begin a new object1430 // Begin a new object 1355 1431 $this->n++; 1356 $this->offsets[$this->n] =strlen($this->buffer);1432 $this->offsets[$this->n] = strlen($this->buffer); 1357 1433 $this->_out($this->n.' 0 obj'); 1358 1434 } … … 1367 1443 function _out($s) 1368 1444 { 1369 // Add a line to the document1445 // Add a line to the document 1370 1446 if($this->state==2) 1371 $this->pages[$this->page] .=$s."\n";1372 else 1373 $this->buffer .=$s."\n";1447 $this->pages[$this->page] .= $s."\n"; 1448 else 1449 $this->buffer .= $s."\n"; 1374 1450 } 1375 1451 1376 1452 function _putpages() 1377 1453 { 1378 $nb =$this->page;1454 $nb = $this->page; 1379 1455 if(!empty($this->AliasNbPages)) 1380 1456 { 1381 // Replace number of pages1457 // Replace number of pages 1382 1458 for($n=1;$n<=$nb;$n++) 1383 $this->pages[$n] =str_replace($this->AliasNbPages,$nb,$this->pages[$n]);1459 $this->pages[$n] = str_replace($this->AliasNbPages,$nb,$this->pages[$n]); 1384 1460 } 1385 1461 if($this->DefOrientation=='P') 1386 1462 { 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;1394 } 1395 $filter =($this->compress) ? '/Filter /FlateDecode ' : '';1463 $wPt = $this->DefPageSize[0]*$this->k; 1464 $hPt = $this->DefPageSize[1]*$this->k; 1465 } 1466 else 1467 { 1468 $wPt = $this->DefPageSize[1]*$this->k; 1469 $hPt = $this->DefPageSize[0]*$this->k; 1470 } 1471 $filter = ($this->compress) ? '/Filter /FlateDecode ' : ''; 1396 1472 for($n=1;$n<=$nb;$n++) 1397 1473 { 1398 // Page1474 // Page 1399 1475 $this->_newobj(); 1400 1476 $this->_out('<</Type /Page'); … … 1405 1481 if(isset($this->PageLinks[$n])) 1406 1482 { 1407 // Links1408 $annots ='/Annots [';1483 // Links 1484 $annots = '/Annots ['; 1409 1485 foreach($this->PageLinks[$n] as $pl) 1410 1486 { 1411 $rect =sprintf('%.2F %.2F %.2F %.2F',$pl[0],$pl[1],$pl[0]+$pl[2],$pl[1]-$pl[3]);1412 $annots .='<</Type /Annot /Subtype /Link /Rect ['.$rect.'] /Border [0 0 0] ';1487 $rect = sprintf('%.2F %.2F %.2F %.2F',$pl[0],$pl[1],$pl[0]+$pl[2],$pl[1]-$pl[3]); 1488 $annots .= '<</Type /Annot /Subtype /Link /Rect ['.$rect.'] /Border [0 0 0] '; 1413 1489 if(is_string($pl[4])) 1414 $annots .='/A <</S /URI /URI '.$this->_textstring($pl[4]).'>>>>';1490 $annots .= '/A <</S /URI /URI '.$this->_textstring($pl[4]).'>>>>'; 1415 1491 else 1416 1492 { 1417 $l =$this->links[$pl[4]];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);1493 $l = $this->links[$pl[4]]; 1494 $h = isset($this->PageSizes[$l[0]]) ? $this->PageSizes[$l[0]][1] : $hPt; 1495 $annots .= sprintf('/Dest [%d 0 R /XYZ 0 %.2F null]>>',1+2*$l[0],$h-$l[1]*$this->k); 1420 1496 } 1421 1497 } 1422 1498 $this->_out($annots.']'); 1423 1499 } 1500 if($this->PDFVersion>'1.3') 1501 $this->_out('/Group <</Type /Group /S /Transparency /CS /DeviceRGB>>'); 1424 1502 $this->_out('/Contents '.($this->n+1).' 0 R>>'); 1425 1503 $this->_out('endobj'); 1426 // Page content1427 $p =($this->compress) ? gzcompress($this->pages[$n]) : $this->pages[$n];1504 // Page content 1505 $p = ($this->compress) ? gzcompress($this->pages[$n]) : $this->pages[$n]; 1428 1506 $this->_newobj(); 1429 1507 $this->_out('<<'.$filter.'/Length '.strlen($p).'>>'); … … 1431 1509 $this->_out('endobj'); 1432 1510 } 1433 // Pages root1434 $this->offsets[1] =strlen($this->buffer);1511 // Pages root 1512 $this->offsets[1] = strlen($this->buffer); 1435 1513 $this->_out('1 0 obj'); 1436 1514 $this->_out('<</Type /Pages'); 1437 $kids ='/Kids [';1515 $kids = '/Kids ['; 1438 1516 for($i=0;$i<$nb;$i++) 1439 $kids .=(3+2*$i).' 0 R ';1517 $kids .= (3+2*$i).' 0 R '; 1440 1518 $this->_out($kids.']'); 1441 1519 $this->_out('/Count '.$nb); … … 1447 1525 function _putfonts() 1448 1526 { 1449 $nf =$this->n;1527 $nf = $this->n; 1450 1528 foreach($this->diffs as $diff) 1451 1529 { 1452 // Encodings1530 // Encodings 1453 1531 $this->_newobj(); 1454 1532 $this->_out('<</Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences ['.$diff.']>>'); … … 1457 1535 foreach($this->FontFiles as $file=>$info) 1458 1536 { 1459 // Font file embedding1537 // Font file embedding 1460 1538 $this->_newobj(); 1461 $this->FontFiles[$file]['n']=$this->n; 1462 $font=''; 1463 $f=fopen($this->_getfontpath().$file,'rb',1); 1464 if(!$f) 1465 $this->Error('Font file not found'); 1466 while(!feof($f)) 1467 $font.=fread($f,8192); 1468 fclose($f); 1469 $compressed=(substr($file,-2)=='.z'); 1539 $this->FontFiles[$file]['n'] = $this->n; 1540 $font = file_get_contents($this->fontpath.$file,true); 1541 if(!$font) 1542 $this->Error('Font file not found: '.$file); 1543 $compressed = (substr($file,-2)=='.z'); 1470 1544 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 } 1545 $font = substr($font,6,$info['length1']).substr($font,6+$info['length1']+6,$info['length2']); 1484 1546 $this->_out('<</Length '.strlen($font)); 1485 1547 if($compressed) … … 1494 1556 foreach($this->fonts as $k=>$font) 1495 1557 { 1496 // Font objects1497 $this->fonts[$k]['n'] =$this->n+1;1498 $type =$font['type'];1499 $name =$font['name'];1500 if($type==' core')1501 { 1502 // Standardfont1558 // Font objects 1559 $this->fonts[$k]['n'] = $this->n+1; 1560 $type = $font['type']; 1561 $name = $font['name']; 1562 if($type=='Core') 1563 { 1564 // Core font 1503 1565 $this->_newobj(); 1504 1566 $this->_out('<</Type /Font'); … … 1512 1574 elseif($type=='Type1' || $type=='TrueType') 1513 1575 { 1514 // Additional Type1 or TrueType font1576 // Additional Type1 or TrueType/OpenType font 1515 1577 $this->_newobj(); 1516 1578 $this->_out('<</Type /Font'); … … 1520 1582 $this->_out('/Widths '.($this->n+1).' 0 R'); 1521 1583 $this->_out('/FontDescriptor '.($this->n+2).' 0 R'); 1522 if($font['enc']) 1523 { 1524 if(isset($font['diff'])) 1525 $this->_out('/Encoding '.($nf+$font['diff']).' 0 R'); 1526 else 1527 $this->_out('/Encoding /WinAnsiEncoding'); 1528 } 1584 if(isset($font['diffn'])) 1585 $this->_out('/Encoding '.($nf+$font['diffn']).' 0 R'); 1586 else 1587 $this->_out('/Encoding /WinAnsiEncoding'); 1529 1588 $this->_out('>>'); 1530 1589 $this->_out('endobj'); 1531 // Widths1590 // Widths 1532 1591 $this->_newobj(); 1533 $cw =&$font['cw'];1534 $s ='[';1592 $cw = &$font['cw']; 1593 $s = '['; 1535 1594 for($i=32;$i<=255;$i++) 1536 $s .=$cw[chr($i)].' ';1595 $s .= $cw[chr($i)].' '; 1537 1596 $this->_out($s.']'); 1538 1597 $this->_out('endobj'); 1539 // Descriptor1598 // Descriptor 1540 1599 $this->_newobj(); 1541 $s ='<</Type /FontDescriptor /FontName /'.$name;1600 $s = '<</Type /FontDescriptor /FontName /'.$name; 1542 1601 foreach($font['desc'] as $k=>$v) 1543 $s.=' /'.$k.' '.$v; 1544 $file=$font['file']; 1545 if($file) 1546 $s.=' /FontFile'.($type=='Type1' ? '' : '2').' '.$this->FontFiles[$file]['n'].' 0 R'; 1602 $s .= ' /'.$k.' '.$v; 1603 if(!empty($font['file'])) 1604 $s .= ' /FontFile'.($type=='Type1' ? '' : '2').' '.$this->FontFiles[$font['file']]['n'].' 0 R'; 1547 1605 $this->_out($s.'>>'); 1548 1606 $this->_out('endobj'); … … 1550 1608 else 1551 1609 { 1552 // Allow for additional types1553 $mtd ='_put'.strtolower($type);1610 // Allow for additional types 1611 $mtd = '_put'.strtolower($type); 1554 1612 if(!method_exists($this,$mtd)) 1555 1613 $this->Error('Unsupported font type: '.$type); … … 1561 1619 function _putimages() 1562 1620 { 1563 $filter=($this->compress) ? '/Filter /FlateDecode ' : ''; 1564 reset($this->images); 1565 while(list($file,$info)=each($this->images)) 1566 { 1621 foreach(array_keys($this->images) as $file) 1622 { 1623 $this->_putimage($this->images[$file]); 1624 unset($this->images[$file]['data']); 1625 unset($this->images[$file]['smask']); 1626 } 1627 } 1628 1629 function _putimage(&$info) 1630 { 1631 $this->_newobj(); 1632 $info['n'] = $this->n; 1633 $this->_out('<</Type /XObject'); 1634 $this->_out('/Subtype /Image'); 1635 $this->_out('/Width '.$info['w']); 1636 $this->_out('/Height '.$info['h']); 1637 if($info['cs']=='Indexed') 1638 $this->_out('/ColorSpace [/Indexed /DeviceRGB '.(strlen($info['pal'])/3-1).' '.($this->n+1).' 0 R]'); 1639 else 1640 { 1641 $this->_out('/ColorSpace /'.$info['cs']); 1642 if($info['cs']=='DeviceCMYK') 1643 $this->_out('/Decode [1 0 1 0 1 0 1 0]'); 1644 } 1645 $this->_out('/BitsPerComponent '.$info['bpc']); 1646 if(isset($info['f'])) 1647 $this->_out('/Filter /'.$info['f']); 1648 if(isset($info['dp'])) 1649 $this->_out('/DecodeParms <<'.$info['dp'].'>>'); 1650 if(isset($info['trns']) && is_array($info['trns'])) 1651 { 1652 $trns = ''; 1653 for($i=0;$i<count($info['trns']);$i++) 1654 $trns .= $info['trns'][$i].' '.$info['trns'][$i].' '; 1655 $this->_out('/Mask ['.$trns.']'); 1656 } 1657 if(isset($info['smask'])) 1658 $this->_out('/SMask '.($this->n+1).' 0 R'); 1659 $this->_out('/Length '.strlen($info['data']).'>>'); 1660 $this->_putstream($info['data']); 1661 $this->_out('endobj'); 1662 // Soft mask 1663 if(isset($info['smask'])) 1664 { 1665 $dp = '/Predictor 15 /Colors 1 /BitsPerComponent 8 /Columns '.$info['w']; 1666 $smask = array('w'=>$info['w'], 'h'=>$info['h'], 'cs'=>'DeviceGray', 'bpc'=>8, 'f'=>$info['f'], 'dp'=>$dp, 'data'=>$info['smask']); 1667 $this->_putimage($smask); 1668 } 1669 // Palette 1670 if($info['cs']=='Indexed') 1671 { 1672 $filter = ($this->compress) ? '/Filter /FlateDecode ' : ''; 1673 $pal = ($this->compress) ? gzcompress($info['pal']) : $info['pal']; 1567 1674 $this->_newobj(); 1568 $this->images[$file]['n']=$this->n; 1569 $this->_out('<</Type /XObject'); 1570 $this->_out('/Subtype /Image'); 1571 $this->_out('/Width '.$info['w']); 1572 $this->_out('/Height '.$info['h']); 1573 if($info['cs']=='Indexed') 1574 $this->_out('/ColorSpace [/Indexed /DeviceRGB '.(strlen($info['pal'])/3-1).' '.($this->n+1).' 0 R]'); 1575 else 1576 { 1577 $this->_out('/ColorSpace /'.$info['cs']); 1578 if($info['cs']=='DeviceCMYK') 1579 $this->_out('/Decode [1 0 1 0 1 0 1 0]'); 1580 } 1581 $this->_out('/BitsPerComponent '.$info['bpc']); 1582 if(isset($info['f'])) 1583 $this->_out('/Filter /'.$info['f']); 1584 if(isset($info['parms'])) 1585 $this->_out($info['parms']); 1586 if(isset($info['trns']) && is_array($info['trns'])) 1587 { 1588 $trns=''; 1589 for($i=0;$i<count($info['trns']);$i++) 1590 $trns.=$info['trns'][$i].' '.$info['trns'][$i].' '; 1591 $this->_out('/Mask ['.$trns.']'); 1592 } 1593 $this->_out('/Length '.strlen($info['data']).'>>'); 1594 $this->_putstream($info['data']); 1595 unset($this->images[$file]['data']); 1675 $this->_out('<<'.$filter.'/Length '.strlen($pal).'>>'); 1676 $this->_putstream($pal); 1596 1677 $this->_out('endobj'); 1597 //Palette1598 if($info['cs']=='Indexed')1599 {1600 $this->_newobj();1601 $pal=($this->compress) ? gzcompress($info['pal']) : $info['pal'];1602 $this->_out('<<'.$filter.'/Length '.strlen($pal).'>>');1603 $this->_putstream($pal);1604 $this->_out('endobj');1605 }1606 1678 } 1607 1679 } … … 1629 1701 $this->_putfonts(); 1630 1702 $this->_putimages(); 1631 // Resource dictionary1632 $this->offsets[2] =strlen($this->buffer);1703 // Resource dictionary 1704 $this->offsets[2] = strlen($this->buffer); 1633 1705 $this->_out('2 0 obj'); 1634 1706 $this->_out('<<'); … … 1665 1737 $this->_out('/OpenAction [3 0 R /XYZ null null 1]'); 1666 1738 elseif(!is_string($this->ZoomMode)) 1667 $this->_out('/OpenAction [3 0 R /XYZ null null '. ($this->ZoomMode/100).']');1739 $this->_out('/OpenAction [3 0 R /XYZ null null '.sprintf('%.2F',$this->ZoomMode/100).']'); 1668 1740 if($this->LayoutMode=='single') 1669 1741 $this->_out('/PageLayout /SinglePage'); … … 1691 1763 $this->_putpages(); 1692 1764 $this->_putresources(); 1693 // Info1765 // Info 1694 1766 $this->_newobj(); 1695 1767 $this->_out('<<'); … … 1697 1769 $this->_out('>>'); 1698 1770 $this->_out('endobj'); 1699 // Catalog1771 // Catalog 1700 1772 $this->_newobj(); 1701 1773 $this->_out('<<'); … … 1703 1775 $this->_out('>>'); 1704 1776 $this->_out('endobj'); 1705 // Cross-ref1706 $o =strlen($this->buffer);1777 // Cross-ref 1778 $o = strlen($this->buffer); 1707 1779 $this->_out('xref'); 1708 1780 $this->_out('0 '.($this->n+1)); … … 1710 1782 for($i=1;$i<=$this->n;$i++) 1711 1783 $this->_out(sprintf('%010d 00000 n ',$this->offsets[$i])); 1712 // Trailer1784 // Trailer 1713 1785 $this->_out('trailer'); 1714 1786 $this->_out('<<'); … … 1718 1790 $this->_out($o); 1719 1791 $this->_out('%%EOF'); 1720 $this->state =3;1721 } 1722 // End of class1723 } 1724 1725 // Handle special IE contype request1792 $this->state = 3; 1793 } 1794 // End of class 1795 } 1796 1797 // Handle special IE contype request 1726 1798 if(isset($_SERVER['HTTP_USER_AGENT']) && $_SERVER['HTTP_USER_AGENT']=='contype') 1727 1799 {
Note: See TracChangeset
for help on using the changeset viewer.
