[ Index ]
 

Code source de vtiger CRM 5.0.2

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/modules/Reports/ -> CreatePDF.php (source)

   1  <?php
   2  /*********************************************************************************
   3  ** The contents of this file are subject to the vtiger CRM Public License Version 1.0
   4   * ("License"); You may not use this file except in compliance with the License
   5   * The Original Code is:  vtiger CRM Open Source
   6   * The Initial Developer of the Original Code is vtiger.
   7   * Portions created by vtiger are Copyright (C) vtiger.
   8   * All Rights Reserved.
   9  *
  10   ********************************************************************************/
  11  require_once ("modules/Reports/ReportRun.php");
  12  require_once ("modules/Reports/Reports.php");
  13  require ('include/fpdf/fpdf.php');
  14  
  15  //a hex html code (e.g. #3FE5AA)
  16  function hex2dec($couleur = "#000000"){
  17      $R = substr($couleur, 1, 2);
  18      $rouge = hexdec($R);
  19      $V = substr($couleur, 3, 2);
  20      $vert = hexdec($V);
  21      $B = substr($couleur, 5, 2);
  22      $bleu = hexdec($B);
  23      $tbl_couleur = array();
  24      $tbl_couleur['R']=$rouge;
  25      $tbl_couleur['G']=$vert;
  26      $tbl_couleur['B']=$bleu;
  27      return $tbl_couleur;
  28  }
  29  
  30  //conversion pixel -> millimeter in 72 dpi
  31  function px2mm($px){
  32      return $px*25.4/72;
  33  }
  34  
  35  function txtentities($html){
  36      $trans = get_html_translation_table(HTML_ENTITIES);
  37      $trans = array_flip($trans);
  38      return strtr($html, $trans);
  39  }
  40  ////////////////////////////////////
  41  
  42  class Html2PDF extends FPDF
  43  {
  44  //variables of html parser
  45  var $B;
  46  var $I;
  47  var $U;
  48  var $HREF;
  49  var $fontList;
  50  var $issetfont;
  51  var $issetcolor;
  52  
  53  function Html2PDF($orientation='P',$unit='mm',$format='A4')
  54  {
  55      //Call parent constructor
  56      $this->FPDF($orientation,$unit,$format);
  57      //Initialization
  58      $this->B=0;
  59      $this->I=0;
  60      $this->U=0;
  61      $this->HREF='';
  62  
  63      $this->tableborder=0;
  64      $this->tdbegin=false;
  65      $this->tdwidth=0;
  66      $this->tdheight=0;
  67      $this->tdalign="L";
  68      $this->tdbgcolor=false;
  69  
  70      $this->oldx=0;
  71      $this->oldy=0;
  72  
  73      $this->fontlist=array("arial","times","courier","helvetica","symbol");
  74      $this->issetfont=false;
  75      $this->issetcolor=false;
  76  }
  77  
  78  //////////////////////////////////////
  79  //html parser
  80  
  81  function WriteHTML($html)
  82  {
  83      $html=strip_tags($html,"<b><u><i><a><img><p><br><strong><em><font><tr><blockquote><hr><td><tr><table><sup>"); //remove all unsupported tags
  84      $html=str_replace("\n",'',$html); //replace carriage returns by spaces
  85      $html=str_replace("\t",'',$html); //replace carriage returns by spaces
  86      $a=preg_split('/<(.*)>/U',$html,-1,PREG_SPLIT_DELIM_CAPTURE); //explodes the string
  87      foreach($a as $i=>$e)
  88      {
  89          if($i%2==0)
  90          {
  91              //Text
  92              if($this->HREF)
  93                  $this->PutLink($this->HREF,$e);
  94              elseif($this->tdbegin) {
  95                  if(trim($e)!='' and $e!="&nbsp;") {
  96                      $this->Cell($this->tdwidth,$this->tdheight,$e,$this->tableborder,'',$this->tdalign,$this->tdbgcolor);
  97                  }
  98                  elseif($e=="&nbsp;") {
  99                      $this->Cell($this->tdwidth,$this->tdheight,'',$this->tableborder,'',$this->tdalign,$this->tdbgcolor);
 100                  }
 101              }
 102              else
 103                  $this->Write(5,stripslashes(txtentities($e)));
 104          }
 105          else
 106          {
 107              //Tag
 108              if($e{0}=='/')
 109                  $this->CloseTag(strtoupper(substr($e,1)));
 110              else
 111              {
 112                  //Extract attributes
 113                  $a2=explode(' ',$e);
 114                  $tag=strtoupper(array_shift($a2));
 115                  $attr=array();
 116                  foreach($a2 as $v)
 117                      if(ereg('^([^=]*)=["\']?([^"\']*)["\']?$',$v,$a3))
 118                          $attr[strtoupper($a3[1])]=$a3[2];
 119                  $this->OpenTag($tag,$attr);
 120              }
 121          }
 122      }
 123  }
 124  
 125  function OpenTag($tag,$attr)
 126  {
 127      //Opening tag
 128      switch($tag){
 129  
 130          case 'SUP':
 131              if($attr['SUP'] != '') {    
 132                  //Set current font to: Bold, 6pt     
 133                  $this->SetFont('','',6);
 134                  //Start 125cm plus width of cell to the right of left margin         
 135                  //Superscript "1"
 136                  $this->Cell(2,2,$attr['SUP'],0,0,'L');
 137              }
 138              break;
 139  
 140          case 'TABLE': // TABLE-BEGIN
 141              if( $attr['BORDER'] != '' ) $this->tableborder=$attr['BORDER'];
 142              else $this->tableborder=0;
 143              break;
 144          case 'TR': //TR-BEGIN
 145              break;
 146          case 'TD': // TD-BEGIN
 147              if( $attr['WIDTH'] != '' ) $this->tdwidth=($attr['WIDTH']/4);
 148              else $this->tdwidth=40; // SET to your own widt if you need bigger fixed cells
 149              if( $attr['HEIGHT'] != '') $this->tdheight=($attr['HEIGHT']/6);
 150              else $this->tdheight=6; // SET to your own height if you need bigger fixed cells
 151              if( $attr['ALIGN'] != '' ) {
 152                  $align=$attr['ALIGN'];        
 153                  if($align=="LEFT") $this->tdalign="L";
 154                  if($align=="CENTER") $this->tdalign="C";
 155                  if($align=="RIGHT") $this->tdalign="R";
 156              }
 157              else $this->tdalign="L"; // SET to your own
 158              if( $attr['BGCOLOR'] != '' ) {
 159                  $coul=hex2dec($attr['BGCOLOR']);
 160                      $this->SetFillColor($coul['R'],$coul['G'],$coul['B']);
 161                      $this->tdbgcolor=true;
 162                  }
 163              $this->tdbegin=true;
 164              break;
 165  
 166          case 'HR':
 167              if( $attr['WIDTH'] != '' )
 168                  $Width = $attr['WIDTH'];
 169              else
 170                  $Width = $this->w - $this->lMargin-$this->rMargin;
 171              $x = $this->GetX();
 172              $y = $this->GetY();
 173              $this->SetLineWidth(0.2);
 174              $this->Line($x,$y,$x+$Width,$y);
 175              $this->SetLineWidth(0.2);
 176              $this->Ln(1);
 177              break;
 178          case 'STRONG':
 179              $this->SetStyle('B',true);
 180              break;
 181          case 'EM':
 182              $this->SetStyle('I',true);
 183              break;
 184          case 'B':
 185          case 'I':
 186          case 'U':
 187              $this->SetStyle($tag,true);
 188              break;
 189          case 'A':
 190              $this->HREF=$attr['HREF'];
 191              break;
 192          case 'IMG':
 193              if(isset($attr['SRC']) and (isset($attr['WIDTH']) or isset($attr['HEIGHT']))) {
 194                  if(!isset($attr['WIDTH']))
 195                      $attr['WIDTH'] = 0;
 196                  if(!isset($attr['HEIGHT']))
 197                      $attr['HEIGHT'] = 0;
 198                  $this->Image($attr['SRC'], $this->GetX(), $this->GetY(), px2mm($attr['WIDTH']), px2mm($attr['HEIGHT']));
 199              }
 200              break;
 201          //case 'TR':
 202          case 'BLOCKQUOTE':
 203          case 'BR':
 204              $this->Ln(5);
 205              break;
 206          case 'P':
 207              $this->Ln(10);
 208              break;
 209          case 'FONT':
 210              if (isset($attr['COLOR']) and $attr['COLOR']!='') {
 211                  $coul=hex2dec($attr['COLOR']);
 212                  $this->SetTextColor($coul['R'],$coul['G'],$coul['B']);
 213                  $this->issetcolor=true;
 214              }
 215              if (isset($attr['FACE']) and in_array(strtolower($attr['FACE']), $this->fontlist)) {
 216                  $this->SetFont(strtolower($attr['FACE']));
 217                  $this->issetfont=true;
 218              }
 219              if (isset($attr['FACE']) and in_array(strtolower($attr['FACE']), $this->fontlist) and isset($attr['SIZE']) and $attr['SIZE']!='') {
 220                  $this->SetFont(strtolower($attr['FACE']),'',$attr['SIZE']);
 221                  $this->issetfont=true;
 222              }
 223              break;
 224      }
 225  }
 226  
 227  function CloseTag($tag)
 228  {
 229      //Closing tag
 230      if($tag=='SUP') {
 231      }
 232  
 233      if($tag=='TD') { // TD-END
 234          $this->tdbegin=false;
 235          $this->tdwidth=0;
 236          $this->tdheight=0;
 237          $this->tdalign="L";
 238          $this->tdbgcolor=false;
 239      }
 240      if($tag=='TR') { // TR-END
 241          $this->Ln();
 242      }
 243      if($tag=='TABLE') { // TABLE-END
 244          $this->tableborder=0;
 245      }
 246  
 247      if($tag=='STRONG')
 248          $tag='B';
 249      if($tag=='EM')
 250          $tag='I';
 251      if($tag=='B' or $tag=='I' or $tag=='U')
 252          $this->SetStyle($tag,false);
 253      if($tag=='A')
 254          $this->HREF='';
 255      if($tag=='FONT'){
 256          if ($this->issetcolor==true) {
 257              $this->SetTextColor(0);
 258          }
 259          if ($this->issetfont) {
 260              $this->SetFont('arial');
 261              $this->issetfont=false;
 262          }
 263      }
 264  }
 265  
 266  function SetStyle($tag,$enable)
 267  {
 268      //Modify style and select corresponding font
 269      $this->$tag+=($enable ? 1 : -1);
 270      $style='';
 271      foreach(array('B','I','U') as $s)
 272          if($this->$s>0)
 273              $style.=$s;
 274      $this->SetFont('',$style);
 275  }
 276  
 277  function PutLink($URL,$txt)
 278  {
 279      //Put a hyperlink
 280      $this->SetTextColor(0,0,255);
 281      $this->SetStyle('U',true);
 282      $this->Write(5,$txt,$URL);
 283      $this->SetStyle('U',false);
 284      $this->SetTextColor(0);
 285  }
 286  
 287  }//end of class
 288  
 289  $reportid = $_REQUEST["record"];
 290  $oReport = new Reports($reportid);
 291  //Code given by C‚sar Rodr¡guez for Rwport Filter
 292  $filtercolumn = $_REQUEST["stdDateFilterField"];
 293  $filter = $_REQUEST["stdDateFilter"];
 294  $oReportRun = new ReportRun($reportid);
 295  $filterlist = $oReportRun->RunTimeFilter($filtercolumn,$filter,$_REQUEST["startdate"],$_REQUEST["enddate"]);
 296  
 297  $arr_val = $oReportRun->GenerateReport("PDF",$filterlist);
 298  
 299  if(isset($arr_val))
 300  {
 301          $columnlength = count($arr_val[0]);
 302  }
 303  
 304  if($columnlength > 0 && $columnlength <= 4)
 305  {
 306          $pdf = new Html2PDF('P','mm','A4');
 307  }elseif($columnlength >= 5 && $columnlength < 8)
 308  {
 309          $pdf = new Html2PDF('L','mm','A4');
 310  }elseif($columnlength >= 8 && $columnlength <= 12)
 311  {
 312          $pdf = new Html2PDF('P','mm','A3');
 313  }elseif($columnlength > 12)
 314  {
 315          $pdf = new Html2PDF('L','mm','A3');
 316  }
 317  
 318  $pdf->AddPage();
 319  
 320  $pdf->SetFillColor(224,235,255);
 321  $pdf->SetTextColor(0);
 322  $pdf->SetFont('Arial','B',14);
 323  $pdf->Cell(($pdf->columnlength*50),10,$oReport->reportname,0,0,'C',0);
 324  $pdf->Ln();
 325  
 326  $pdf->SetFont('Arial','',10);
 327  
 328  if(isset($arr_val))
 329  {
 330      
 331      foreach($arr_val as $wkey=>$warray_value)
 332          {
 333                  foreach($warray_value as $whd=>$wvalue)
 334                  {
 335              if(strlen($wvalue) < strlen($whd))
 336              {
 337                  $w_inner_array[] = strlen($whd);    
 338              }else
 339              {
 340                  $w_inner_array[] = strlen($wvalue);
 341              }
 342                  }
 343          $warr_val[] = $w_inner_array;
 344          unset($w_inner_array);
 345          }
 346  
 347      foreach($warr_val[0] as $fkey=>$fvalue)
 348      {
 349          foreach($warr_val as $wkey=>$wvalue)
 350          {
 351              
 352              $f_inner_array[] = $warr_val[$wkey][$fkey];
 353          }
 354          sort($f_inner_array,1);
 355          $farr_val[] = $f_inner_array;
 356          unset($f_inner_array);
 357      }
 358      
 359      foreach($farr_val as $skkey=>$skvalue)
 360      {
 361          if($skvalue[count($arr_val)-1] == 1)
 362          {
 363              $col_width[] = ($skvalue[count($arr_val)-1] * 100);
 364          }
 365          {
 366              $col_width[] = ($skvalue[count($arr_val)-1] * 10) + 10 ;    
 367          }
 368      }
 369  
 370      $count = 0;
 371      foreach($arr_val[0] as $key=>$value)
 372      {
 373          $headerHTML .= '<td width="'.$col_width[$count].'" bgcolor="#DDDDDD">'.$oReportRun->getLstringforReportHeaders($key).'</td>';
 374          $count = $count + 1;
 375      }
 376      
 377      foreach($arr_val as $key=>$array_value)
 378      {
 379          $valueHTML = "";
 380          $count = 0;
 381          foreach($array_value as $hd=>$value)
 382          {
 383              $valueHTML .= '<td width="'.$col_width[$count].'">'.$value.'</td>';
 384              $count = $count + 1;
 385          }
 386          $dataHTML .= '<tr>'.$valueHTML.'</tr>';
 387      }
 388  
 389  }
 390  
 391  $html='<table border="1">
 392  <tr><b>
 393      '.$headerHTML.'
 394  </b>
 395  </tr>
 396  '.$dataHTML.'
 397  </table>';
 398  
 399  $pdf->WriteHTML($html);
 400  $pdf->Output('Reports.pdf','D');
 401  exit();
 402  ?>


Généré le : Sun Feb 25 10:22:19 2007 par Balluche grâce à PHPXref 0.7