<?php
/**
* @author Bettina Schwarzer, Fritz-Haber-Institut
* @copyright 11/2012
*
* Klasse PDF zur Telefonliste
*/
include ($_SERVER['DOCUMENT_ROOT']."/fhiiqm/tool/fpdf.php");
class PDF extends FPDF
{
var $y0, $y, $xa, $width, $height;
// var $wid = array(55,25,40,15,40); // Breite: Spalten 'new'
var $wid = array(65,15,40,15,40); // Breite: Spalten 'new'
var $fancy = 1;
var $anzcol = 2;
var $col = 0;
//Author Patrick Benny: Fit text to cell (http://www.fpdf.org)
//*********************************************************************
//Cell with horizontal scaling if text is too wide
function CellFit($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link='', $scale=false, $force=true)
{
//Get string width
$str_width=$this->GetStringWidth($txt);
// Division by 0 avoid, 21.11.2012, bs
if ($str_width == 0) $str_width = 0.1;
//Calculate ratio to fit cell
if($w==0)
$w = $this->w-$this->rMargin-$this->x;
$ratio = ($w-$this->cMargin*2)/$str_width;
$fit = ($ratio < 1 || ($ratio > 1 && $force));
if ($fit)
{
if ($scale)
{
//Calculate horizontal scaling
$horiz_scale=$ratio*100.0;
//Set horizontal scaling
$this->_out(sprintf('BT %.2F Tz ET',$horiz_scale));
}
else
{
//Calculate character spacing in points
$char_space=($w-$this->cMargin*2-$str_width)/max($this->MBGetStringLength($txt)-1,1)*$this->k;
//Set character spacing
$this->_out(sprintf('BT %.2F Tc ET',$char_space));
}
//Override user alignment (since text will fill up cell)
$align='';
}
//Pass on to Cell method
$this->Cell($w,$h,$txt,$border,$ln,$align,$fill,$link);
//Reset character spacing/horizontal scaling
if ($fit)
$this->_out('BT '.($scale ? '100 Tz' : '0 Tc').' ET');
}
//Cell with horizontal scaling only if necessary
function CellFitScale($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link='')
{
$this->CellFit($w,$h,$txt,$border,$ln,$align,$fill,$link,true,false);
}
//Cell with horizontal scaling always
function CellFitScaleForce($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link='')
{
$this->CellFit($w,$h,$txt,$border,$ln,$align,$fill,$link,true,true);
}
//Cell with character spacing only if necessary
function CellFitSpace($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link='')
{
$this->CellFit($w,$h,$txt,$border,$ln,$align,$fill,$link,false,false);
}
//Cell with character spacing always
function CellFitSpaceForce($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link='')
{
//Same as calling CellFit directly
$this->CellFit($w,$h,$txt,$border,$ln,$align,$fill,$link,false,true);
}
//Patch to also work with CJK double-byte text
function MBGetStringLength($s)
{
if($this->CurrentFont['type']=='Type0')
{
$len = 0;
$nbbytes = strlen($s);
for ($i = 0; $i < $nbbytes; $i++)
{
if (ord($s[$i])<128)
$len++;
else
{
$len++;
$i++;
}
}
return $len;
}
else
return strlen($s);
}
//*********************************************************************
//Kopfzeile
function Header()
{
global $header;
//$this->Cell(20);
//Titel
//Arial fett 15
$this->SetFont('helvetica','BI',11);
$this->Cell(55,10,'Fritz-Haber-Institut',0,0,'L');
$this->SetFont('helvetica','B',11);
$this->Cell(40,10,'Telefonverzeichnis',0,0,'L');
if ($this->fancy && !$this->anzcol)
{
$this->SetTextColor(255,0,0);
$this->Cell(0,10,'(intern)',0,0,'L');
}
else
$this->Cell(0,10,' ',0,0,'L');
//Logo
$this->Image($_SERVER['DOCUMENT_ROOT']."/fhiiqm/img/fhilogotransp.png",170,8,25);
//Zeilenumbruch
$this->Ln(16);
if ($this->fancy)
{
//Colors, line width and bold font
$this->SetFont('helvetica','',8);
$this->SetFillColor(104,172,191);
$this->SetTextColor(0,0,102);
$this->SetDrawColor(255,255,255);
$this->SetLineWidth(.0);
$this->SetFont('','B');
//Tabellen-Header immer im Kopf
// $w=array(55,40,30,15,40);
for($i=0;$i<count($header);$i++)
$this->Cell($this->wid[$i],5,$header[$i],1,0,'L',1);
$this->Ln();
}
}
//Fusszeile
function Footer()
{
//Position 1,5 cm von unten
$this->SetY(-15);
//Arial kursiv 8
$this->SetFont('Arial','I',8);
//Datum der Liste
$heute = new DateTime();
$this->Cell(0,10,$heute->format('d.m.Y H:i:s'),0,0,'L');
//Seitenzahl
$this->Cell(0,10,'Seite '.$this->PageNo().'/{nb}',0,0,'R');
}
//Colored table
function FancyTable($header,$data)
{
//Colors, line width and bold font
$this->SetFillColor(104,172,191);
$this->SetTextColor(0,0,102);
$this->SetDrawColor(255,255,255);
$this->SetLineWidth(.0);
$this->SetFont('','B');
//Header notwendig fuer Tabelle, Hoehe = 0!
// $w=array(55,40,30,15,40); // Breite der Zellen
for($i=0;$i<count($header);$i++)
$this->Cell($this->wid[$i],0,'',1,0,'L',1);
$this->Ln();
//Color and font restoration
// $this->SetFillColor(222,222,222);
$this->SetFillColor(249,249,249);
$this->SetTextColor(0);
$this->SetFont('');
//Data
$fill=0;
$height = 4;
$fill = !$fill;
// $breite = ($pdf->w - $pdf->lMargin - $pdf->rMargin) / 3;
foreach($data as $row)
{
if ($row->persknr != $vpnr)
{
$fill=!$fill;
if (substr($row->name,0,1) != $vfirst)
{
$this->SetFont('','B',9);
$kap = strtoupper(substr($row->name,0,1));
// $this->Ln(5); //$this->AddPage();
$this->CellFitScale(175,$height,$kap,0,0,'L',$fill);
$this->Ln();
$this->SetFont('','',8);
}
$titel = ($row->titel)? ", $row->titel" : "" ;
$vname = ($row->vorname)? ", $row->vorname" : "";
$name = $row->name . $titel . $vname;
$this->CellFitScale($this->wid[0],$height,$name,0,0,'L',$fill);
$this->CellFitScale($this->wid[1],$height,$row->abt_name,'LR',0,'L',$fill);
if ($row->telefon_typ == "fax") $typ = " FAX"; else $typ = "";
if (!$row->public) $this->SetTextColor(255,0,0); else $this->SetTextColor(0,0,136);
$this->CellFitScale($this->wid[2],$height,$row->telefon_nr.$typ,'LR',0,'L',$fill);
$this->SetTextColor(0);
$this->CellFitScale($this->wid[3],$height,$row->geb_ID . " " . $row->raum_nr,'LR',0,'L',$fill);
$this->CellFitScale($this->wid[4],$height,$row->email,'LR',0,'L',$fill);
$this->Ln();
}
else
{
$this->Cell($this->wid[0],$height,'','LR',0,'L',$fill);
if ($vabt != $row->abt_name) $abt = $row->abt_name;
else $abt = '';
$this->CellFitScale($this->wid[1],$height,$abt,'LR',0,'L',$fill);
if ($row->telefon_typ == "fax") $typ = " FAX"; else $typ = "";
if (!$row->public) $this->SetTextColor(255,0,0); else $this->SetTextColor(0,0,136);
$this->CellFitScale($this->wid[2],$height,$row->telefon_nr.$typ,'LR',0,'L',$fill);
$this->SetTextColor(0);
$this->CellFitScale($this->wid[3],$height,'','LR',0,'L',$fill);
$this->Cell($this->wid[4],$height,'','LR',0,'L',$fill);
$this->Ln();
}
$vpnr = $row->persknr;
$vabt = $row->abt_name;
$vfirst = substr($row->name,0,1);
}
}
function FancyTableOldMySQL($header,$data)
{
//Colors, line width and bold font
$this->SetFillColor(104,172,191);
$this->SetTextColor(0,0,102);
$this->SetDrawColor(255,255,255);
$this->SetLineWidth(.0);
$this->SetFont('','B');
//Header notwendig fuer Tabelle, Hoehe = 0!
$w=array(55,40,30,15,40);
for($i=0;$i<count($header);$i++)
$this->Cell($w[$i],0,'',1,0,'L',1);
$this->Ln();
//Color and font restoration
// $this->SetFillColor(222,222,222);
$this->SetFillColor(238,238,238);
$this->SetTextColor(0);
$this->SetFont('');
//Data
$fill=0;
$fill = !$fill;
while ($row = mysql_fetch_assoc($data))
{
if ($row["persknr"] != $vpnr)
{
$fill=!$fill;
$titel = ($row["titel"])? ", ".$row["titel"] : "" ;
$vname = ($row["vorname"])? ",". $row["vorname"] : "";
$name = $row["name"] . $titel . $vname;
$this->CellFitScale($w[0],4.5,$name,0,0,'L',$fill);
$this->CellFitScale($w[1],4.5,$row["abt_name"],'LR',0,'L',$fill);
if ($row["telefon_typ"] == "fax") $typ = " FAX"; else $typ = "";
if (!$row["public"]) $this->SetTextColor(255,0,0); else $this->SetTextColor(0,0,136);
$this->CellFitScale($w[2],4.5,$row["telefon_nr"].$typ,'LR',0,'L',$fill);
$this->SetTextColor(0);
$this->CellFitScale($w[3],4.5,$row["geb_ID"] . " " . $row["raum_nr"],'LR',0,'L',$fill);
$this->CellFitScale($w[4],4.5,$row["email"],'LR',0,'L',$fill);
$this->Ln();
}
else
{
$newline = 0;
$this->Cell($w[0],4.5,'','LR',0,'L',$fill);
if ($vabt != $row["abt_name"]) $abt = $row["abt_name"];
else $abt = '';
$this->CellFitScale($w[1],4.5,$abt,'LR',0,'L',$fill);
if ($row["telefon_typ"] == "fax") $typ = " FAX"; else $typ = "";
if (!$row["public"]) $this->SetTextColor(255,0,0); else $this->SetTextColor(0,0,136);
$this->CellFitScale($w[2],4.5,$row["telefon_nr"].$typ,'LR',0,'L',$fill);
$this->SetTextColor(0);
$this->CellFitScale($w[3],4.5,$row["geb_ID"] . " " . $row["raum_nr"],'LR',0,'L',$fill);
$this->Cell($w[4],4.5,'','LR',0,'L',$fill);
$this->Ln();
}
$vpnr = $row["persknr"];
$vabt = $row["abt_name"];
}
}
function Normal($data)
{
$this->fancy = 1;
$w = array(55,40,35,40);
$this->SetFillColor(249,249,249);
$fill = 0;
$fill = !$fill;
foreach ($data as $row)
{
if ($row->persknr != $vpnr)
{
if ($vpnr)
{
// ausgeben vorheriges Objekt
$tel = implode(", ",$atel);
$this->SetFont('times','',9);
$this->CellFitScale($w[0],$this->height,$name,0,0,'L',$fill);
$this->CellFitScale($w[1],$this->height,$tel,0,0,'L',$fill);
$this->CellFitScale($w[2],$this->height,$abtort,0,0,'L',$fill);
$this->CellFitScale($w[3],$this->height,$mail,0,0,'L',$fill);
$this->Ln();
}
$fill=!$fill;
if (substr($row->name,0,1) != $vfirst)
{
$this->SetFont('','B',11);
$kap = strtoupper(substr($row->name,0,1));
$this->CellFitScale(170,$this->height,$kap,0,0,'L',$fill);
$this->Ln();
$this->SetFont('','',9);
}
$atel = array();
$titel = ($row->titel)? ", ".$row->titel : "" ;
$vname = ($row->vorname)? " ". $row->vorname : "";
$name = $row->name . $titel . $vname;
$raum = ($row->raum_nr)? ", ".$row->raum_nr : "";
$abtort = $row->abt_name . " " . $row->geb_ID . $raum;
$mail = $row->email;
$fax =($row->telefon_typ == 'fax')? " FAX" : "";
$atel[] = $row->telefon_nr . $fax;
}
else
{
$fax =($row->telefon_typ == 'fax')? " FAX" : "";
$atel[] = $row->telefon_nr . $fax;
}
$vfirst = substr($row->name,0,1);
$vpnr = $row->persknr;
}
// ausgeben letztes Objekt
$tel = implode(";",$atel);
$this->SetFont('times','',9);
$this->CellFitScale($w[0],$this->height,$name,0,0,'L',$fill);
$this->CellFitScale($w[1],$this->height,$tel,0,0,'L',$fill);
$this->CellFitScale($w[2],$this->height,$abtort,0,0,'L',$fill);
$this->CellFitScale($w[3],$this->height,$mail,0,0,'L',$fill);
}
// mehrere Spalten
function SetCol($col,$width)
{
// Move position to a column
$this->col = $col;
$this->width = $width;
// echo "1x = " . $this->GetX() . ", col = $this->col" . ", width = $this->width<br />\n";
if ($this->xa <= $this->lMargin || ($this->anzcol == 3 && $this->xa <= $this->lMargin + $this->width + 5*$this->col))
$this->xa = $this->lMargin + 5*$this->col + ($this->col*$this->width);
else $this->xa = $this->lMargin;
// $this->SetLeftMargin(25);
$this->SetX($this->xa);
// echo "2x = " . $this->GetX() . ", col = $this->col" . ", width = $this->width<br />\n";
}
function AcceptPageBreak()
{
if (!$this->fancy) // nur bei mehrspaltiger Darstellung
{
if ($this->GetY()+$this->height > $this->PageBreakTrigger)
if($this->col < $this->anzcol-1)
{
// Go to next column
$this->SetCol($this->col+1,$this->width);
$this->SetY($this->y0);
$this->y = $this->y0;
// echo "apb_x = " . $this->GetX() . ", col = $this->col" . ", width = $this->width<br />\n";
return false;
}
else
{
// Go back to first column and issue page break
$this->AddPage($this->CurOrientation);
$this->SetCol(0,$this->width);
$this->SetY($this->y0);
$this->y = $this->y0;
return true;
}
else
return false;
}
else
return $this->AutoPageBreak;
}
function Small($data,$colwidth,$height)
{
$this->fancy = 0;
$this->y0 = $this->GetY();
$this->y = $this->y0;
$this->width = $colwidth;
$this->height = $height;
$this->SetFillColor(249,249,249);
$fill = 0;
$fill = !$fill;
foreach ($data as $row)
{
// echo "3x = " . $this->GetX() . ", y = " . $this->GetY() . ", width = $colwidth<br />\n";
$fill=!$fill;
if (substr($row->name,0,1) != $vfirst)
{
$this->SetFont('','B',11);
$kap = strtoupper(substr($row->name,0,1));
$this->SetX($this->xa);
$this->CellFitScale($colwidth,$height,$kap,0,0,'L',$fill);
$this->y = $this->y+$height;
$this->SetY($this->y);
$this->AcceptPageBreak();
}
$zeil1 = $row->name . ", " . $row->titel . " " . $row->vorname . " ";
$zeil2 = $row->abt_name . " " . $row->geb_ID. " ". $row->raum_nr;
$zeil3 = $row->telefon_nr;
$this->SetFont('times','',9);
$this->SetX($this->xa);
if ($this->anzcol == 2)
$this->CellFitScale($colwidth/2,$height,$zeil1,0,0,'L',$fill);
else
$this->CellFitScale($colwidth*0.75,$height,$zeil1,0,0,'L',$fill);
if ($this->anzcol == 2)
{
$this->SetFont('','',8);
$this->CellFitScale($colwidth/4,$height,$zeil2,0,0,'L',$fill);
}
$this->SetFont('','',9);
$this->CellFitScale($colwidth/4,$height,$zeil3,0,0,'R',$fill);
$this->y = $this->y+$height;
$this->SetY($this->y);
$vfirst = substr($row->name,0,1);
$this->AcceptPageBreak();
// echo "4x = " . $this->GetX() . ", y = " . $this->GetY() . ", width = $colwidth<br />\n";
}
}
}
?>