fhiiqm/test/fpdf_create.php
changeset 33 f1a8785434e4
equal deleted inserted replaced
32:c2d325791e24 33:f1a8785434e4
       
     1 <?php
       
     2 
       
     3 /**
       
     4  * @author Bettina Schwarzer, Fritz-Haber-Institut
       
     5  * @copyright 11/2012
       
     6  * 
       
     7  * pdf-Dokument erstellen
       
     8  */
       
     9 
       
    10     error_reporting(E_ALL ^ E_NOTICE);
       
    11     
       
    12     include ($_SERVER['DOCUMENT_ROOT']."/fhiiqm/tool/fpdf.php");
       
    13     class PDF extends FPDF
       
    14     {
       
    15         //Cell with horizontal scaling if text is too wide
       
    16         function CellFit($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link='', $scale=false, $force=true)
       
    17         {
       
    18             //Get string width
       
    19             $str_width=$this->GetStringWidth($txt);
       
    20             // Division by 0 avoid, 21.11.2012, bs
       
    21             if ($str_width == 0) $str_width = 0.1;
       
    22     
       
    23             //Calculate ratio to fit cell
       
    24             if($w==0)
       
    25                 $w = $this->w-$this->rMargin-$this->x;
       
    26             $ratio = ($w-$this->cMargin*2)/$str_width;
       
    27     
       
    28             $fit = ($ratio < 1 || ($ratio > 1 && $force));
       
    29             if ($fit)
       
    30             {
       
    31                 if ($scale)
       
    32                 {
       
    33                     //Calculate horizontal scaling
       
    34                     $horiz_scale=$ratio*100.0;
       
    35                     //Set horizontal scaling
       
    36                     $this->_out(sprintf('BT %.2F Tz ET',$horiz_scale));
       
    37                 }
       
    38                 else
       
    39                 {
       
    40                     //Calculate character spacing in points
       
    41                     $char_space=($w-$this->cMargin*2-$str_width)/max($this->MBGetStringLength($txt)-1,1)*$this->k;
       
    42                     //Set character spacing
       
    43                     $this->_out(sprintf('BT %.2F Tc ET',$char_space));
       
    44                 }
       
    45                 //Override user alignment (since text will fill up cell)
       
    46                 $align='';
       
    47             }
       
    48     
       
    49             //Pass on to Cell method
       
    50             $this->Cell($w,$h,$txt,$border,$ln,$align,$fill,$link);
       
    51     
       
    52             //Reset character spacing/horizontal scaling
       
    53             if ($fit)
       
    54                 $this->_out('BT '.($scale ? '100 Tz' : '0 Tc').' ET');
       
    55         }
       
    56     
       
    57         //Cell with horizontal scaling only if necessary
       
    58         function CellFitScale($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link='')
       
    59         {
       
    60             $this->CellFit($w,$h,$txt,$border,$ln,$align,$fill,$link,true,false);
       
    61         }
       
    62     
       
    63         //Cell with horizontal scaling always
       
    64         function CellFitScaleForce($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link='')
       
    65         {
       
    66             $this->CellFit($w,$h,$txt,$border,$ln,$align,$fill,$link,true,true);
       
    67         }
       
    68     
       
    69         //Cell with character spacing only if necessary
       
    70         function CellFitSpace($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link='')
       
    71         {
       
    72             $this->CellFit($w,$h,$txt,$border,$ln,$align,$fill,$link,false,false);
       
    73         }
       
    74     
       
    75         //Cell with character spacing always
       
    76         function CellFitSpaceForce($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link='')
       
    77         {
       
    78             //Same as calling CellFit directly
       
    79             $this->CellFit($w,$h,$txt,$border,$ln,$align,$fill,$link,false,true);
       
    80         }
       
    81     
       
    82         //Patch to also work with CJK double-byte text
       
    83         function MBGetStringLength($s)
       
    84         {
       
    85             if($this->CurrentFont['type']=='Type0')
       
    86             {
       
    87                 $len = 0;
       
    88                 $nbbytes = strlen($s);
       
    89                 for ($i = 0; $i < $nbbytes; $i++)
       
    90                 {
       
    91                     if (ord($s[$i])<128)
       
    92                         $len++;
       
    93                     else
       
    94                     {
       
    95                         $len++;
       
    96                         $i++;
       
    97                     }
       
    98                 }
       
    99                 return $len;
       
   100             }
       
   101             else
       
   102                 return strlen($s);
       
   103         }
       
   104 
       
   105         //Kopfzeile
       
   106         function Header()
       
   107         {
       
   108             global $header;
       
   109             //$this->Cell(20);
       
   110             //Titel
       
   111             //Arial fett 15
       
   112             $this->SetFont('Arial','BI',11);
       
   113             $this->Cell(55,10,'Fritz-Haber-Institut',0,0,'L');
       
   114             $this->SetFont('Arial','B',11);
       
   115             $this->Cell(40,10,'Telefonverzeichnis',0,0,'L');
       
   116             $this->SetTextColor(255,0,0);
       
   117             $this->Cell(0,10,'(intern)',0,0,'L');
       
   118             //Logo
       
   119             $this->Image($_SERVER['DOCUMENT_ROOT']."/fhiiqm/img/fhilogotransp.png",170,8,25);
       
   120             //Zeilenumbruch
       
   121             $this->Ln(20);
       
   122             //Colors, line width and bold font
       
   123             $this->SetFont('helvetica','',8);
       
   124             $this->SetFillColor(104,172,191);
       
   125             $this->SetTextColor(0,0,102);
       
   126             $this->SetDrawColor(255,255,255);
       
   127             $this->SetLineWidth(.0);
       
   128             $this->SetFont('','B');
       
   129             //Tabellen-Header immer im Kopf
       
   130             $w=array(55,40,30,15,40);
       
   131             for($i=0;$i<count($header);$i++)
       
   132                 $this->Cell($w[$i],5,$header[$i],1,0,'L',1);
       
   133             $this->Ln();
       
   134         }
       
   135         
       
   136         //Fusszeile
       
   137         function Footer()
       
   138         {
       
   139             //Position 1,5 cm von unten
       
   140             $this->SetY(-15);
       
   141             //Arial kursiv 8
       
   142             $this->SetFont('Arial','I',8);
       
   143             //Datum der Liste
       
   144             $heute = new DateTime();
       
   145             $this->Cell(0,10,$heute->format('d.m.Y H:i:s'),0,0,'L');
       
   146             //Seitenzahl
       
   147             $this->Cell(0,10,'Seite '.$this->PageNo().'/{nb}',0,0,'R');
       
   148         }
       
   149         //Colored table
       
   150         function FancyTable($header,$data)
       
   151         {
       
   152 
       
   153             //Colors, line width and bold font
       
   154             $this->SetFillColor(104,172,191);
       
   155             $this->SetTextColor(0,0,102);
       
   156             $this->SetDrawColor(255,255,255);
       
   157             $this->SetLineWidth(.0);
       
   158             $this->SetFont('','B');
       
   159 
       
   160             //Header notwendig fuer Tabelle, Hoehe = 0!
       
   161             $w=array(55,40,30,15,40);
       
   162             for($i=0;$i<count($header);$i++)
       
   163                 $this->Cell($w[$i],0,'',1,0,'L',1);
       
   164             $this->Ln();
       
   165             //Color and font restoration
       
   166 //            $this->SetFillColor(222,222,222);
       
   167             $this->SetFillColor(238,238,238);
       
   168             $this->SetTextColor(0);
       
   169             $this->SetFont('');
       
   170             //Data
       
   171             $fill=0;
       
   172             $fill = !$fill;
       
   173             foreach($data as $row)
       
   174             {
       
   175     			if ($row->persknr != $vpnr)
       
   176                 {
       
   177 /*                    if ($newline)
       
   178                     {
       
   179                         $this->Cell(array_sum($w),4,'',0,'',$fill);
       
   180                         $this->Ln();
       
   181                         $newline = 0;
       
   182                     } 
       
   183 */  
       
   184                     $fill=!$fill;
       
   185                     $titel = ($row->titel)? ", $row->titel" : "" ;
       
   186                     $vname = ($row->vorname)? ", $row->vorname" : "";
       
   187                     $name = $row->name . $titel . $vname;
       
   188 /*                    $y = $this->GetY();
       
   189                     if (strlen($row->name . $titel . $vname) > 40)
       
   190                     {
       
   191                         echo "name = $name<br />";
       
   192                         $name = $row->name . $titel . "\n" . substr($vname,2);
       
   193                         $this->MultiCell($w[0],4,$name,0,'L',$fill);
       
   194                         $this -> SetXY($w[0]+$this->lMargin,$y);
       
   195                         $newline = 1;
       
   196 */
       
   197 /*                    }
       
   198                     else
       
   199                         $this->Cell($w[0],4,$name,'LR',0,'L',$fill);
       
   200 */                        
       
   201                     $this->CellFitScale($w[0],4.5,$name,0,0,'L',$fill);
       
   202                     $this->CellFitScale($w[1],4.5,$row->abt_name,'LR',0,'L',$fill);
       
   203                     if ($row->telefon_typ == "fax") $typ = " FAX"; else $typ = "";
       
   204                     if (!$row->public) $this->SetTextColor(255,0,0); else $this->SetTextColor(0,0,136); 
       
   205                     $this->CellFitScale($w[2],4.5,$row->telefon_nr.$typ,'LR',0,'L',$fill);
       
   206                     $this->SetTextColor(0);
       
   207                     $this->CellFitScale($w[3],4.5,$row->geb_ID . " " . $row->raum_nr,'LR',0,'L',$fill);
       
   208                     $this->CellFitScale($w[4],4.5,$row->email,'LR',0,'L',$fill);
       
   209                     $this->Ln();
       
   210                 }
       
   211                 else
       
   212                 {
       
   213                     $newline = 0;
       
   214                     $this->Cell($w[0],4.5,'','LR',0,'L',$fill);
       
   215                     if ($vabt != $row->abt_name) $abt = $row->abt_name;
       
   216                     else $abt = '';    
       
   217                     $this->CellFitScale($w[1],4.5,$abt,'LR',0,'L',$fill);
       
   218                     if ($row->telefon_typ == "fax") $typ = " FAX"; else $typ = "";
       
   219                     if (!$row->public) $this->SetTextColor(255,0,0); else $this->SetTextColor(0,0,136); 
       
   220                     $this->CellFitScale($w[2],4.5,$row->telefon_nr.$typ,'LR',0,'L',$fill);
       
   221                     $this->SetTextColor(0);
       
   222                     $this->CellFitScale($w[3],4.5,$row->geb_ID . " " . $row->raum_nr,'LR',0,'L',$fill);
       
   223                     $this->Cell($w[4],4.5,'','LR',0,'L',$fill);
       
   224                     $this->Ln();
       
   225                     
       
   226                 }
       
   227                 $vpnr = $row->persknr;
       
   228                 $vabt = $row->abt_name;
       
   229             }
       
   230 //            $this->Cell(array_sum($w),0,'','T');
       
   231         }
       
   232     }
       
   233 
       
   234 //    $pdf = new FPDF();
       
   235     $header=array('Name','Abteilung','Tel/Fax','Raum','E-Mail');
       
   236     $pdf = new PDF();
       
   237     $pdf->SetLeftMargin(25);
       
   238     $pdf->AddPage();
       
   239     $pdf->AliasNbPages();
       
   240 /*
       
   241     $pdf->SetFont('Arial','B',16);
       
   242     $pdf->Cell(40,10,'Hallo Du!',0,1);
       
   243     $pdf->SetFont('courier','i',12);
       
   244     $pdf->Cell(100,10,'Dieser Text ist mit fpdf erstellt worden.',0,1);
       
   245 */    
       
   246     // Telefonliste
       
   247 	include_once($_SERVER['DOCUMENT_ROOT'] ."/fhiiqm/inc/dbconnect.inc.php");
       
   248 //    include_once ($_SERVER['DOCUMENT_ROOT'] ."/fhiiqm/inc/func_lib.inc.php");
       
   249 
       
   250 	$dbc = new dbconnection();
       
   251     $sql = "SELECT persknr, name, vorname, titel, abt_name, telefon_nr, telefon_typ, public, geb_ID, raum_nr, email FROM v_tel_list 
       
   252             ORDER BY 2,3,5,6";
       
   253     $result = $dbc ->queryObjectArray($sql);
       
   254     $pdf->SetFont('helvetica','',9);
       
   255 
       
   256     $pdf->FancyTable($header,$result);
       
   257     $pdf->Output();
       
   258 
       
   259 ?>