| [ Index ] |
|
Code source de e107 0.7.8 |
1 <?php 2 3 //extend fpdf class from package with custom functions 4 class e107PDF extends UFPDF{ 5 6 //variables of html parser 7 var $B; 8 var $I; 9 var $U; 10 var $HREF; 11 var $CENTER=''; 12 var $ALIGN=''; 13 var $IMG; 14 var $SRC; 15 var $WIDTH; 16 var $HEIGHT; 17 var $fontList; 18 var $issetfont; 19 var $issetcolor; 20 var $iminfo=array(0,0); 21 22 function e107PDF($orientation='P',$unit='mm',$format='A4'){ 23 global $pdfpref; 24 //Call parent constructor 25 $this->UFPDF($orientation,$unit,$format); 26 //Initialization 27 $this->B=0; 28 $this->I=0; 29 $this->U=0; 30 $this->BLOCKQUOTE=''; 31 $this->HREF=''; 32 $this->CENTER=''; 33 $this->ALIGN=''; 34 $this->IMG=''; 35 $this->SRC=''; 36 $this->WIDTH=''; 37 $this->HEIGHT=''; 38 $this->fontlist=array("arial","times","courier","helvetica","symbol"); 39 40 $this->issetfont=false; 41 $this->issetcolor=false; 42 } 43 44 //default preferences if none present 45 function getDefaultPDFPrefs(){ 46 $pdfpref['pdf_margin_left'] = '25'; 47 $pdfpref['pdf_margin_right'] = '15'; 48 $pdfpref['pdf_margin_top'] = '15'; 49 $pdfpref['pdf_font_family'] = 'arial'; 50 $pdfpref['pdf_font_size'] = '8'; 51 $pdfpref['pdf_font_size_sitename'] = '14'; 52 $pdfpref['pdf_font_size_page_url'] = '8'; 53 $pdfpref['pdf_font_size_page_number'] = '8'; 54 $pdfpref['pdf_show_logo'] = true; 55 $pdfpref['pdf_show_sitename'] = false; 56 $pdfpref['pdf_show_page_url'] = true; 57 $pdfpref['pdf_show_page_number'] = true; 58 $pdfpref['pdf_error_reporting'] = true; 59 return $pdfpref; 60 } 61 //get preferences from db 62 function getPDFPrefs(){ 63 global $sql, $eArrayStorage; 64 65 if(!is_object($eArrayStorage)){ 66 e107_require_once(e_HANDLER.'arraystorage_class.php'); 67 $eArrayStorage = new ArrayData(); 68 } 69 70 if(!is_object($sql)){ $sql = new db; } 71 $num_rows = $sql -> db_Select("core", "*", "e107_name='pdf' "); 72 if($num_rows == 0){ 73 $tmp = $this->getDefaultPDFPrefs(); 74 $tmp2 = $eArrayStorage->WriteArray($tmp); 75 $sql -> db_Insert("core", "'pdf', '".$tmp2."' "); 76 $sql -> db_Select("core", "*", "e107_name='pdf' "); 77 } 78 $row = $sql -> db_Fetch(); 79 $pdfpref = $eArrayStorage->ReadArray($row['e107_value']); 80 return $pdfpref; 81 } 82 83 function toPDF($text){ 84 $search = array(''', ''', '$', '"'); 85 $replace = array("'", "'", '$', '"'); 86 $text = str_replace($search, $replace, $text); 87 return $text; 88 } 89 90 function toPDFTitle($text){ 91 $search = array(":", "*", "?", '"', '<', '>', '|'); 92 $replace = array('-', '-', '-', '-', '-', '-', '-'); 93 $text = str_replace($search, $replace, $text); 94 return $text; 95 } 96 97 /* 98 The makePDF function does all the real parsing and composing 99 input argument $text needs to be an array containing the following: 100 $text = array($text, $creator, $author, $title, $subject, $keywords, $url); 101 */ 102 function makePDF($text){ 103 global $tp, $pdfpref; 104 105 //call get preferences 106 $pdfpref = $this->getPDFPrefs(); 107 108 //define logo and source pageurl (before the parser!) 109 if(is_readable(THEME."images/logopdf.png")){ 110 $logo = THEME."images/logopdf.png"; 111 }else{ 112 $logo = e_IMAGE."logo.png"; 113 } 114 define('PDFLOGO', $logo); //define logo to add in header 115 define('PDFPAGEURL', $text[6]); //define page url to add in header 116 117 //parse the data 118 $text[3] = $this->toPDF($text[3]); //replace some in the title 119 $text[3] = $this->toPDFTitle($text[3]); //replace some in the title 120 foreach($text as $k=>$v){ 121 $text[$k] = $tp->toHTML($v, TRUE); 122 } 123 124 //set some variables 125 $this->SetMargins($pdfpref['pdf_margin_left'],$pdfpref['pdf_margin_top'],$pdfpref['pdf_margin_right']); 126 //$this->SetAutoPageBreak(true,25); 127 128 //start creating the pdf and adding the data 129 $this->AliasNbPages(); //calculate current page + number of pages 130 $this->AddPage(); //start page 131 $this->SetFont($pdfpref['pdf_font_family'],'',$pdfpref['pdf_font_size']); //set font 132 $this->WriteHTML($text[0], true); //write text 133 $this->SetCreator($text[1]); //name of creator 134 $this->SetAuthor($text[2]); //name of author 135 $this->SetTitle($text[3]); //title 136 $this->SetSubject($text[4]); //subject 137 $this->SetKeywords($text[5]); //space/comma separated 138 $file = $text[3].".pdf"; //name of the file 139 $this->Output($file, 'D'); //Save PDF to file (D = output to download window) 140 return; 141 } 142 143 144 //create a header; this will be added on each page 145 function Header(){ 146 global $pdfpref; 147 148 $this->SetY(15); 149 $y0 = $this->GetY(); 150 if($pdfpref['pdf_show_logo']){ 151 $this->SetFont($pdfpref['pdf_font_family'],'',$pdfpref['pdf_font_size']); 152 $this->PutImage(PDFLOGO, '1'); 153 $x1 = $this->GetX(); 154 $y1 = $this->GetY(); 155 156 $image_wh = getimagesize(PDFLOGO); 157 $newx = $x1 + ($image_wh[0]/$this->k); 158 $newy = ($image_wh[1]/$this->k); 159 160 $a=$this->GetStringWidth(SITENAME); 161 $b=$this->GetStringWidth(PDFPAGEURL); 162 if($a>$b){$c=$a;}else{$c=$b;} 163 if($x1+$newx+$c > 210){ 164 $this->SetX($this->lMargin); 165 $this->SetY($y1+2); 166 }else{ 167 if($pdfpref['pdf_show_sitename']){ 168 $m = 5; 169 } 170 if($pdfpref['pdf_show_page_url']){ 171 $m += 5; 172 } 173 if($pdfpref['pdf_show_page_number']){ 174 $m += 5; 175 } 176 $y = $this->GetY(); 177 $this->SetY($y-$m); 178 } 179 } 180 $cellwidth = 210-$this->lMargin-$this->rMargin; 181 $align = "R"; 182 if($pdfpref['pdf_show_sitename']){ 183 $this->SetFont($pdfpref['pdf_font_family'],'B',$pdfpref['pdf_font_size_sitename']); 184 $this->Cell($cellwidth,5,SITENAME,0,1,$align); 185 } 186 if($pdfpref['pdf_show_page_url']){ 187 $this->SetFont($pdfpref['pdf_font_family'],'I',$pdfpref['pdf_font_size_page_url']); 188 $this->Cell($cellwidth,5,PDFPAGEURL,0,1,$align,'',PDFPAGEURL); 189 } 190 if($pdfpref['pdf_show_page_number']){ 191 $this->SetFont($pdfpref['pdf_font_family'],'I',$pdfpref['pdf_font_size_page_number']); 192 $this->Cell($cellwidth,5,PDF_LAN_19.' '.$this->PageNo().'/{nb}',0,1,$align); 193 } 194 $y = $this->GetY()+2; 195 $this->Line($this->lMargin, $y, 210-$this->rMargin, $y); 196 $this->Ln(10); 197 $this->SetX($this->lMargin); 198 $this->SetFont($pdfpref['pdf_font_family'],'',$pdfpref['pdf_font_size']); 199 } 200 201 function txtentities($html){ 202 $html = str_replace("\r\n", "\\n", $html); 203 $html = str_replace("\r", "", $html); 204 $trans_tbl = get_html_translation_table (HTML_ENTITIES); 205 $trans_tbl = array_flip ($trans_tbl); 206 return strtr ($html, $trans_tbl); 207 } 208 209 //function hex2dec 210 //returns an associative array (keys: R,G,B) from 211 //a hex html code (e.g. #3FE5AA) 212 function hex2dec($couleur = "#000000"){ 213 $R = substr($couleur, 1, 2); 214 $rouge = hexdec($R); 215 $V = substr($couleur, 3, 2); 216 $vert = hexdec($V); 217 $B = substr($couleur, 5, 2); 218 $bleu = hexdec($B); 219 $tbl_couleur = array(); 220 $tbl_couleur['R']=$rouge; 221 $tbl_couleur['G']=$vert; 222 $tbl_couleur['B']=$bleu; 223 return $tbl_couleur; 224 } 225 226 function WriteHTML($html,$scale){ 227 228 $search = array("\n", "<br />", "<hr />", '»', 'º', '·', '™', '©', '€', '[', '&#091;', ' ', '‘', '’', ' />', '(', ')', '{', '}', '[', ']'); 229 $replace = array(" ", "<br>", "<hr>", '»', 'º', '·', 'â„¢', '©', '', '[', '[', ' ', "'", "'", '>', '(', ')', '{', '}', '[',']' ); 230 //replace carriage returns by spaces, and some html variants 231 $html=str_replace($search, $replace, $html); 232 $a=preg_split('/<(.*)>/U',$html,-1,PREG_SPLIT_DELIM_CAPTURE); //explodes the string 233 234 foreach($a as $i=>$e) 235 { 236 if($i%2==0) 237 { 238 //Text 239 if($this->HREF){ 240 $this->PutLink($this->HREF,$e); 241 $this->HREF=''; 242 }elseif($this->IMG){ 243 //correct url 244 if(is_readable($this->SRC)){ 245 $file = $this->SRC; 246 $pos=strrpos($file,'.'); 247 $type=substr($file,$pos+1); 248 $type=strtolower($type); 249 //for now only jpg, jpeg and png are supported 250 if($type=='jpg' || $type=='jpeg' || $type=='png'){ 251 252 $url = str_replace("../", "", $this->SRC); 253 $imgsearch = array(e_IMAGE, e_THEME, e_PLUGIN, e_FILE, e_HANDLER); 254 //e_BASE and e_ADMIN are not taken into account ! 255 foreach($imgsearch as $p){ 256 $p = str_replace("../", "", $p); 257 $l = strpos($url, $p); 258 if ($l !== false) { 259 $url = SITEURL.$url; 260 } 261 } 262 $this->Ln(2); 263 $this->PutImage($url,$scale); 264 $this->Ln(2); 265 $this->SetX($this->lMargin); 266 } 267 } 268 $this->IMG=''; 269 $this->SRC=''; 270 $this->WIDTH=''; 271 $this->HEIGHT=''; 272 273 }elseif($this->CENTER){ 274 $this->Cell(0,5,$e,0,1,'C'); 275 }elseif($this->ALIGN == 'center'){ 276 $this->Cell(0,5,$e,0,1,'C'); 277 }elseif($this->ALIGN == 'right'){ 278 $this->Cell(0,5,$e,0,1,'R'); 279 }elseif($this->ALIGN == 'left'){ 280 $this->Cell(0,5,$e,0,1,'L'); 281 }elseif($this->BLOCKQUOTE == 'BLOCKQUOTE'){ 282 $this->SetFont('Courier','',11); 283 $this->SetStyle('B',true); 284 $this->SetStyle('I',true); 285 $this->Cell(0,5,$e,1,1,'L'); 286 $this->SetStyle('B',false); 287 $this->SetStyle('I',false); 288 if ($this->issetcolor==true) { 289 $this->SetTextColor(0); 290 $this->issetcolor=false; 291 } 292 $this->SetFont($pdfpref['pdf_font_family'],'',$pdfpref['pdf_font_size']); 293 }else{ 294 $this->Write(5,stripslashes($this->txtentities($e))); 295 } 296 } 297 else 298 { 299 //Tag 300 if($e{0}=='/'){ 301 $this->CloseTag(strtoupper(substr($e,1))); 302 }else{ 303 //Extract attributes 304 $a2=explode(' ',$e); 305 $tag=strtoupper(array_shift($a2)); 306 $attr=array(); 307 foreach($a2 as $v){ 308 if(ereg('^([^=]*)=["\']?([^"\']*)["\']?$',$v,$a3)){ 309 $attr[strtoupper($a3[1])]=$a3[2]; 310 } 311 } 312 $this->OpenTag($tag,$attr,$scale); 313 } 314 } 315 } 316 } 317 318 function OpenTag($tag,$attr,$scale){ 319 $tag = strtoupper($tag); 320 //Opening tag 321 322 switch($tag){ 323 case 'STRONG': 324 $this->SetStyle('B',true); 325 break; 326 case 'EM': 327 $this->SetStyle('I',true); 328 break; 329 case 'B': 330 case 'I': 331 case 'U': 332 $this->SetStyle($tag,true); 333 break; 334 case 'A': 335 $this->HREF=$attr['HREF']; 336 break; 337 case 'P': 338 $this->ALIGN=$attr['ALIGN']; 339 break; 340 case 'SPAN': 341 if(isset($attr['STYLE'])){ 342 if($attr['STYLE'] == 'text-decoration:underline'){ 343 $this->SetStyle('U',true); 344 } 345 if(strstr($attr['STYLE'], 'color:')){ 346 $attr['COLOR'] = substr($attr['STYLE'],6); 347 $coul=$this->hex2dec($attr['COLOR']); 348 $this->SetTextColor($coul['R'],$coul['G'],$coul['B']); 349 $this->issetcolor=true; 350 } 351 if(strstr($attr['STYLE'], 'font-size:')){ 352 $attr['FONTSIZE'] = intval(substr($attr['STYLE'],10)); 353 $this->SetFont('','',$attr['FONTSIZE']); 354 $this->issetfont=true; 355 } 356 break; 357 } 358 case 'DIV': 359 if($attr['STYLE'] == 'text-align:center'){ 360 $this->ALIGN='center'; 361 } 362 if($attr['STYLE'] == 'text-align:left'){ 363 $this->ALIGN='left'; 364 } 365 if($attr['STYLE'] == 'text-align:right'){ 366 $this->ALIGN='right'; 367 } 368 if($attr['CLASS'] == 'indent'){ 369 // $this->BLOCKQUOTE='BLOCKQUOTE'; 370 } 371 break; 372 case 'IMG': 373 $this->IMG=true; 374 $this->SRC=$attr['SRC']; 375 $this->WIDTH=$attr['WIDTH']; 376 $this->HEIGHT=$attr['HEIGHT']; 377 break; 378 case 'TR': 379 break; 380 case 'TD': 381 break; 382 case 'CODE': 383 case 'BLOCKQUOTE': 384 case 'PRE': 385 $this->Ln(5); 386 $this->SetFont('Courier','',11); 387 $this->issetcolor=true; 388 $this->issetfont=true; 389 $this->SetStyle('B',true); 390 $this->SetStyle('I',true); 391 break; 392 case 'LI': 393 $this->Write(5,' » '); 394 break; 395 case 'BR': 396 $this->Ln(5); 397 break; 398 case 'HR': 399 if( $attr['WIDTH'] != '' ) $Width = $attr['WIDTH']; 400 else $Width = $this->w - $this->lMargin-$this->rMargin; 401 $this->Ln(2); 402 $x = $this->GetX(); 403 $y = $this->GetY(); 404 $this->SetLineWidth(0.4); 405 $this->Line($x,$y,$x+$Width,$y); 406 $this->SetLineWidth(0.2); 407 $this->Ln(2); 408 break; 409 case 'FONT': 410 if (isset($attr['COLOR']) && $attr['COLOR']!='') { 411 $coul=$this->hex2dec($attr['COLOR']); 412 $this->SetTextColor($coul['R'],$coul['G'],$coul['B']); 413 $this->issetcolor=true; 414 } 415 if (isset($attr['FACE']) && in_array(strtolower($attr['FACE']), $this->fontlist)) { 416 $this->SetFont(strtolower($attr['FACE'])); 417 $this->issetfont=true; 418 } 419 break; 420 case 'H1': 421 $this->Ln(5); 422 $this->SetFontSize(22); 423 $this->issetfont=true; 424 break; 425 case 'H2': 426 $this->Ln(5); 427 $this->SetFontSize(18); 428 $this->issetfont=true; 429 $this->SetStyle('U',true); 430 break; 431 case 'H3': 432 $this->Ln(5); 433 $this->SetFontSize(16); 434 $this->issetfont=true; 435 $this->SetStyle('U',true); 436 break; 437 case 'H4': 438 $this->Ln(5); 439 $this->SetFontSize(14); 440 $this->issetfont=true; 441 $this->SetStyle('B',true); 442 break; 443 444 } 445 } 446 447 function CloseTag($tag){ 448 global $pdfpref; 449 450 if ($this->issetcolor==true) { 451 $this->SetTextColor(0); 452 } 453 if ($this->issetfont==true) { 454 $this->SetFont($pdfpref['pdf_font_family'],'',$pdfpref['pdf_font_size']); 455 $this->issetfont=false; 456 } 457 458 $tag = strtoupper($tag); 459 //Closing tag 460 if($tag=='SPAN'){ 461 $tag='U'; 462 if ($this->issetcolor==true) { 463 $this->SetTextColor(0); 464 } 465 if ($this->issetfont==true) { 466 $this->SetFont($pdfpref['pdf_font_family'],'',$pdfpref['pdf_font_size']); 467 $this->issetfont=false; 468 } 469 } 470 if($tag=='DIV'){ 471 $tag='DIV'; 472 $this->ALIGN=''; 473 $this->BLOCKQUOTE=''; 474 } 475 if($tag=='STRONG'){ 476 $tag='B'; 477 } 478 if($tag=='EM'){ 479 $tag='I'; 480 } 481 if($tag=='B' or $tag=='I' or $tag=='U'){ 482 $this->SetStyle($tag,false); 483 } 484 if($tag=='A'){ 485 $this->HREF=''; 486 } 487 if($tag=='P'){ 488 $this->ALIGN=''; 489 } 490 if($tag=='IMG'){ 491 $this->IMG=''; 492 $this->SRC=''; 493 $this->WIDTH=''; 494 $this->HEIGHT=''; 495 } 496 if($tag=='LI'){ 497 $this->Ln(5); 498 } 499 if($tag=='TD'){ 500 $this->Write(5,' '); 501 } 502 if($tag=='TR' || $tag=='BLOCKQUOTE' || $tag=='CODE' || $tag=='PRE'){ 503 $this->SetStyle('B',false); 504 $this->SetStyle('I',false); 505 $this->Ln(5); 506 if ($this->issetcolor==true) { 507 $this->SetTextColor(0); 508 $this->issetcolor=false; 509 } 510 if ($this->issetfont==true) { 511 $this->SetFont($pdfpref['pdf_font_family'],'',$pdfpref['pdf_font_size']); 512 $this->issetfont=false; 513 } 514 } 515 if($tag=='FONT'){ 516 if ($this->issetcolor==true) { 517 $this->SetTextColor(0); 518 $this->issetcolor=false; 519 } 520 if ($this->issetfont==true) { 521 $this->SetFont($pdfpref['pdf_font_family'],'',$pdfpref['pdf_font_size']); 522 $this->issetfont=false; 523 } 524 } 525 if ($tag=='H1' || $tag=='H2' || $tag=='H3' || $tag=='H4'){ 526 $this->H1=''; 527 $this->H2=''; 528 $this->H3=''; 529 $this->H4=''; 530 $this->SetStyle('B',false); 531 $this->SetStyle('U',false); 532 $this->Ln(5); 533 if($this->issetfont==true){ 534 $this->SetFont($pdfpref['pdf_font_family'],'',$pdfpref['pdf_font_size']); 535 $this->issetfont=false; 536 } 537 } 538 } 539 540 function SetStyle($tag,$enable){ 541 //Modify style and select corresponding font 542 $this->$tag+=($enable ? 1 : -1); 543 $style=''; 544 foreach(array('B','I','U') as $s) 545 if($this->$s>0) 546 $style.=$s; 547 $this->SetFont('',$style); 548 } 549 550 function PutLink($URL,$txt){ 551 //remove leading 'http://' 552 if(strpos($URL, "http://")!==false){ 553 $URL = substr($URL, strpos($URL, "http://")+strlen('http://') ); 554 } 555 //Put a hyperlink 556 $this->SetTextColor(0,0,255); 557 $this->SetStyle('U',true); 558 $this->Write(5,$txt,$URL); 559 $this->SetStyle('U',false); 560 $this->SetTextColor(0); 561 } 562 563 function px2mm($px){ 564 return $px*25.4/72; 565 } 566 567 //put the image in pdf with scaling... 568 //width and height-options inside the IMG-Tag are ignored, 569 //we get the image info directly from PHP... 570 //$scale is the global scaling factor, passing through from WriteHTML() 571 //(c)2004/03/12 by St@neCold 572 function PutImage($url,$scale) 573 { 574 if($scale<0) $scale=0; 575 //$scale<=0: put NO image inside the pdf! 576 if($scale>0){ 577 $xsflag=0; 578 $ysflag=0; 579 $yhflag=0; 580 $xscale=1; 581 $yscale=1; 582 //get image info 583 $oposy=$this->GetY(); 584 $iminfo=@getimagesize($url); 585 if($iminfo){ 586 $iw=$scale * $this->px2mm($iminfo[0]); 587 $ih=$scale * $this->px2mm($iminfo[1]); 588 $iw = ($iw)?$iw:1; 589 $ih = ($ih)?$ih:1; 590 $nw=$iw; 591 $nh=$ih; 592 //resizing in x-direction 593 $xsflag=0; 594 if($iw>150) { 595 $xscale=150 / $iw; 596 $yscale=$xscale; 597 $nw=$xscale * $iw; 598 $nh=$xscale * $ih; 599 $xsflag=1; 600 } 601 //now eventually resizing in y-direction 602 $ysflag=0; 603 if(($oposy+$nh)>250){ 604 $yscale=(250-$oposy)/$ih; 605 $nw=$yscale * $iw; 606 $nh=$yscale * $ih; 607 $ysflag=1; 608 } 609 //uups, if the scaling factor of resized image is < 0.33 610 //remark: without(!) the global factor $scale! 611 //that's hard -> on the next page please... 612 $yhflag=0; 613 if($yscale<0.33 and ($xsflag==1 or $ysflag==1)) { 614 $nw=$xscale * $iw; 615 $nh=$xscale * $ih; 616 $ysflag==0; 617 $xsflag==1; 618 $yhflag=1; 619 } 620 if($yhflag==1) $this->AddPage(); 621 $oposy=$this->GetY(); 622 $this->Image($url, $this->GetX(), $this->GetY(), $nw, $nh); 623 $this->SetY($oposy+$nh); 624 //if($yhflag==0 and $ysflag==1) $this->AddPage(); 625 } 626 } 627 } 628 629 } 630 631 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Sun Apr 1 01:23:32 2007 | par Balluche grâce à PHPXref 0.7 |