Location: PHPKode > projects > Template Parser Engine > TPE/pdf.php
<?
/*
pdf.php Erstellt automatisch eine PDF-Datei aus einem Template
Copyright (C) 2004 Thomas Meinusch

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc.,\ 59 Temple Place, Suite 330, Boston, MA
02111-1307, USA.
*/

#################################################################

/*
define('FPDF_FONTPATH','font/');
$pdf->SetAuthor('TM');
$pdf->SetCreator('TPE');
*/

define('FPDF','/usr/share/php/fpdf/');
require_once('cls/clsTags.php');
require_once('cls/clsColor.php');
require(FPDF.'fpdf.php');

class PDF extends FPDF {
	var $headerset;
	var $head;
	var $foot;
	var $theader;
	var $tbody;
	var $trows;
	var $trwrap;
	function Header(){
		if($this->head){
			$this=pagearea($this,$this->head);
			$this->SetY($this->head->TagPara['height']);
		}
	}

	function Footer(){
		if($this->foot){
			$this=pagearea($this,$this->foot);
		}
	}
}

// MUSS SEQUENTIEL laufen!!!
function tpe2pdf($pdfsyntax, $pdfpara=""){
	if(!$pdfpara['type']) $pdfpara['type']="Portrait";
	if(!$pdfpara['unit']) $pdfpara['unit']="mm";
	if(!$pdfpara['page']) $pdfpara['page']="A4";
	if(!$pdfpara['border']) $pdfpara["border"]="0.2";
	$pdf=new PDF($pdfpara['type'],$pdfpara['unit'],$pdfpara['page']);
	if($pdfpara['margin-left']) $pdf->SetLeftMargin($pdfpara['margin-left']);
	if($pdfpara['margin-right']) $pdf->SetLeftMargin($pdfpara['margin-right']);
	if($pdfpara['margin-top']) $pdf->SetLeftMargin($pdfpara['margin-top']);
	if($pdfpara['margin-bottom']) $pdf->SetLeftMargin($pdfpara['margin-bottom']);
	$pdf->AddPage();
	if($pdfpara['wrap']){
		if($pdfpara['wrap']=="nowrap"){
			$pdf->SetAutoPageBreak(false);
		}else{
			$pdf->SetAutoPageBreak(true,$pdfpara['wrap']);
		}
	}	
	$pdf= pages($pdf,$pdfsyntax,$pdfpara);
	$pdf->AliasNbPages();


	if(isset($pdfpara['target'])){
		$pdf->Output($pdfpara['target']);
	}else{
		$pdf->Output();
	}
}

function pagearea($pdf, $tag){
		$temp="<pos><font>";
		if($tag->Text){
			$Text=str_replace("{page}",$pdf->PageNo(),$tag->Text);
			$pdf=pages($pdf,$temp.$Text,$tag->TagPara,true);
		}
		return $pdf;
}
function styletext($pdf,$height,$text,$href){
	$pdfsyntax=$text;
	while($tag=new Tag($pdfsyntax)){
		if($tag->NAME && $x=strpos($pdfsyntax,$tag->Header)){
			$pdf->Write($height,substr($pdfsyntax,0,$x));
			$pdfsyntax=substr($pdfsyntax,$x);
		}
		preg_match('/[biu]{1,3}/i',$tag->NAME, $matches);
		if($matches[0]){
				$pdf->SetFont('',strtoupper($matches[0]));
				$pdf=styletext($pdf,$height,$tag->Text,$href);
				$pdf->SetFont('','');
		}
		if($tag->NAME== 'a'){
				$pdf->Write($height,$tag->Text,$href);
				break;
		}
		$pdfsyntax=str_replace($tag->CONTENT,'',$pdfsyntax);
	}
	if(! $tag->NAME) $pdf->Write($height,$pdfsyntax,$href);
	return $pdf;

}

function pages($pdf,$pdfsyntax,$pdfpara="", $follow=false){
	$wrap=array('right','left','break');
	$align=array('R' => 'right', 'L' => 'left','C' => 'center', 'J' => 'justification');
	$bullet=array('disc' => chr(149), 'square' => chr(187), 'circle' =>chr(176));
	$link=array();
	$tr="";

	$row=0;

	// $oldsyntax="";
	while($tag=new Tag($pdfsyntax)){
	/*
		if($oldsyntax==$pdfsyntax){
			echo $tag->NAME;
			break;
		}
		$oldsyntax=$pdfsyntax;
	*/
		if(! $pdf->headerset) {
			if($pdf->head) {
				$pdf->headerset=1;
				$pdf=pagearea($pdf,$pdf->head);
				$pdf->SetY($pdf->head->TagPara['height']);
			}
		}

		if(!in_array($tag->NAME, array('foot','head')) && $tag->Text) $tag->Text=str_replace("{page}",$pdf->PageNo(),$tag->Text);
		$tag->Text=str_replace("{euro}",chr(128),$tag->Text);
		$tag->Text=str_replace("ยค",chr(128),$tag->Text);
		if((is_array($pdfpara)) && $follow){
			foreach(array_keys($pdfpara) as $key){
				if(!isset($tag->TagPara[$key])){
					//if(!in_array($key,array('top','bottom')) || in_array($tag->NAME, array('pos', 'th','td','thead','tr'))){
					//if(!in_array($key,array('top','bottom')) || !in_array($tag->NAME, array('pos'))){
						$tag->TagPara[$key]=$pdfpara[$key];
					//}
				}
			}
		}

		if($tag->NAME=="hr" && $tag->TagPara["color"]){
			$tag->TagPara["border-color"]=$tag->TagPara["color"];
			unset($tag->TagPara["color"]);
		}
		if(isset($tag->TagPara["color"])){
			$c= new Color($tag->TagPara["color"]);
			$pdf->SetTextColor($c->RED,$c->GREEN,$c->BLUE);
		}
		if(isset($tag->TagPara["border-color"])){
			if(!isset($tag->TagPara["border"])) $tag->TagPara["border"]=$pdfpara["border"];
			if(!isset($tag->TagPara["border"])) $tag->TagPara["border"]=".1";
			$c= new Color($tag->TagPara["border-color"]);
			$pdf->SetDrawColor($c->RED,$c->GREEN,$c->BLUE);
		}
		if($tag->TagPara["background-color"]){
			if(!isset($tag->TagPara["background"])) $tag->TagPara["background"]=1;
			$c= new Color($tag->TagPara["background-color"]);
			$pdf->SetFillColor($c->RED,$c->GREEN,$c->BLUE);
		}

		if(isset($tag->TagPara["title"])){
			$pdf->SetTitle($tag->TagPara["title"]);
			$pdf->SetSubject($tag->TagPara["title"]);
		}
		if(isset($tag->TagPara["href"])){
			$temp=$tag->TagPara["href"];
			if(substr($temp,0,1)=="#"){
				$temp=substr($temp,1);
				$link[$temp]=$pdf->AddLink();
				$href=$link[$temp];
			}else{
				$href=$tag->TagPara["href"];
			}
		}else{
			$href="";
		}

		if(in_array($tag->NAME, array('hr','rect','td','th'))){
			if(isset($tag->TagPara["border"])) $pdf->SetLineWidth($tag->TagPara["border"]);
		}else{
			if(is_array($pdfpara) && isset($pdfpara["border"])) $pdf->SetLineWidth($pdfpara["border"]);
		}
		foreach(array('startx','endx','left') as $key){
			if($tag->TagPara[$key]<0) $tag->TagPara[$key]+=$pdf->w;
		}
		foreach(array('starty','endy','top') as $key){
			if($tag->TagPara[$key]<0) $tag->TagPara[$key]=$tag->TagPara[$key]+$pdf->h;
		}

		switch($tag->NAME){
			case 'head':
				$pdf->head=$tag;
				break;
			case 'foot':
				$pdf->foot=$tag;
				$pdf->SetAutoPageBreak(true, $tag->TagPara["height"]);
				break;
			case 'ul':
					if($tag->TagPara["top"]) $pdf->setY($tag->TagPara["top"]);
					if($tag->TagPara["value"]){
						$tag->Text=str_replace("{".$pdfpara["id"]."}",$li - 1,$tag->Text);
					}
					$pdf=pages($pdf,$tag->Text,$tag->TagPara,true);
				break;
			case 'table':
				$pdf=pages($pdf,"<pos>".$tag->Text,$tag->TagPara,true);
				break;
			case 'thead':
				$pdf->theader=$tag;
				if(isset($tag->TagPara["top"])) unset($tag->TagPara["top"]);
				$pdf=pages($pdf,"<font>".$pdf->theader->Text,$pdfpara,true);
				break;
			case 'tbody':
				$pdf->tbody=$tag;
				$pdf=pages($pdf,"<font>".$tag->Text,$pdfpara,true);
				break;
			case 'tr':
				if(isset($tag->TagPara["top"])) unset($tag->TagPara["top"]);
				if($tag->TagPara["left"]) $pdf->SetX($tag->TagPara["left"]);
				$pdf->trows=$tag->TagPara["rows"];
				$pdf->trwrap=$tag->TagPara["wrap"];
				$pdf=pages($pdf,"<font>".$tag->Text,$tag->TagPara,true);
//				$pdf->trows="";
				$pdf->trwrap="";
				break;
			case 'th':
			case 'td':
				if($pdf->GetY()+$tag->TagPara["height"]>$pdf->PageBreakTrigger){
			 		$pdf->AddPage($pdf->CurOrientation);
					if($pdf->theader){
						$pdfpara->TagPara["top"]=10;
						$pdf=pages($pdf,"<font>".$pdf->theader->Text,$pdfpara,true);
						if($tag->TagPara["left"]) $pdf->SetX($tag->TagPara["left"]);
						$pdf=pages($pdf,"<font>",$pdfpara,true);
					}
				}

				//styletext($pdf,$tag->Text);


				if($tag->NAME=="th"){
					$row++;
					$thwrap=$tag->TagPara["wrap"];
					if($pdf->trows && $row % $pdf->trows){
						$thwrap="";
					}

					$cellwidth=0+$tag->TagPara["width"];
					if(! $cellwidth>0 && $pdf->trows) $cellwidth=($pdf->w-$pdf->lMargin-$pdf->rMargin)/$pdf->trows;
					$pdf->Cell($cellwidth, $tag->TagPara["height"],
						$tag->Text,
						($tag->TagPara["border"]>0),
						array_search($thwrap, $wrap),
						array_search($tag->TagPara["align"],$align),
						$tag->TagPara["background"],
						$tag->TagPara["href"]);
				}else{
					$pdf->MultiCell($tag->TagPara["width"], $tag->TagPara["height"],
						$tag->Text,
						($tag->TagPara["border"]>0),
						array_search($tag->TagPara["align"],$align),
						$tag->TagPara["background"]);
				}
				break;

			case 'li':
				# 1 | a | A | i | I
				# disc | square | circle
				# =chr
				if(! $li) $li=$tag->TagPara["start"];
				if(! $li) $li=1;

				if($tag->TagPara["left"]) $pdf->setX($tag->TagPara["left"]);
				$blt=$tag->TagPara["type"];
				if(in_array($blt,array_keys($bullet))){
					$blt=$bullet[$blt];
				}
				if(in_array($tag->TagPara["type"], array('numeric','roman','Roman','alpha','Alpha'))){
					$blt=str_replace("{".$tag->TagPara["id"]."}",$li,$tag->TagPara["value"]);
				}
				if(substr($blt,0,3)=="chr") $blt=chr(substr($blt,3));
				$blt_width = $pdf->GetStringWidth($blt)+$pdf->cMargin*2;
				$pdf->Cell($blt_width,$tag->TagPara["height"],$blt,0,'',$tag->TagPara["background"]);
				$pdf->MultiCell($tag->TagPara["width"]-$blt_width,$tag->TagPara["height"],$tag->Text,$tag->TagPara["border"],$tag->TagPara["align"],$tag->TagPara["background"]);
				$li++;
				break;
			case 'newpage':
				if($tag->TagPara['margin-left']) $pdf->SetLeftMargin($tag->TagPara['margin-left']);
				if($tag->TagPara['margin-right']) $pdf->SetLeftMargin($tag->TagPara['margin-right']);
				if($tag->TagPara['margin-top']) $pdf->SetLeftMargin($tag->TagPara['margin-top']);
				if($tag->TagPara['margin-bottom']) $pdf->SetLeftMargin($tag->TagPara['margin-bottom']);
				$pdf->AddPage($tag->TagPara["type"]);
				if($tag->TagPara['wrap']){
					if($tag->TagPara['wrap']=="nowrap"){
						$pdf->SetAutoPageBreak(false);
					}else{
						$pdf->SetAutoPageBreak(true,$tag->TagPara['wrap']);
					}
				}	
				break;

			case 'br':
				$pdf->Ln($tag->TagPara["size"]);
				break;

			case 'pos':
				unset($posx);
				unset($posy);
				if($tag->TagPara["left"] || $tag->TagPara["right"]) $posx=$tag->TagPara["left"]-$tag->TagPara["right"];
				if($tag->TagPara["top"] || $tag->TagPara["bottom"]) $posy=$tag->TagPara["top"]-$tag->TagPara["bottom"];
				if(isset($posx) && isset($posy)){
					$pdf->SetXY($posx,$posy);
				}else if($posy){
					$pdf->SetY($posy);
				}else if($posx){
					$pdf->SetX($posx);
				}
				break;
			case 'font':
				$pdf->SetFont($tag->TagPara["face"],$tag->TagPara["style"],$tag->TagPara["size"]);
//				$pdf=pages($pdf,"<font>".$tag->Text,$pdfpara,true);
//				$pdf->SetFont($pdfpara["face"],$pdfpara["style"],$pdfpara["size"]);
				break;
			case 'p':
# auf Cell umstellen!
#					$numSpaces += substr_count( $tag->Text, ' ' );
#					echo $numSpaces."<br>", $pdf->GetStringWidth( $tag->Text )."<br>";
#				if ( $numSpaces )
#				echo $numSpaces, $w;
				$pdf=styletext($pdf,$tag->TagPara["height"],$tag->Text,$tag->TagPara["href"]);
				$pdf->SetY($pdf->y+$tag->TagPara["height"]);
				break;
			case 'a':
				if($href){
					$pdf->Write(5,$tag->Text,$href);
				}else if($tag->TagPara['name']){
					$pdf->SetLink($link[$tag->TagPara['name']]);
				}
				break;
			case 'img':
				$pdf->Image(realpath(getPath($tag->TagPara["dir"])."/".$tag->TagPara["src"]),
					$tag->TagPara["left"],$tag->TagPara["top"],
					$tag->TagPara["width"],$tag->TagPara["height"],$tag->TagPara["type"],$href);
				break;
			case 'hr':
				$pdf->Line($tag->TagPara["startx"],$tag->TagPara["starty"],$tag->TagPara["endx"],$tag->TagPara["endy"]);
				break;
			case 'rect':
				$style="";
				if($tag->TagPara["border"]) $style.="D";
				if($tag->TagPara["background"]) $style.="F";
				$pdf->Rect($tag->TagPara["startx"],$tag->TagPara["starty"],$tag->TagPara["endx"],$tag->TagPara["endy"],$style);
				break;
		}
		$pdfsyntax= substr($pdfsyntax,strpos($pdfsyntax,"<")+strlen($tag->CONTENT));
		if(isset($tag->TagPara["color"])){
			$c= new Color($pdfpara["color"]);
			$pdf->SetTextColor($c->RED,$c->GREEN,$c->BLUE);
		}
		if(isset($tag->TagPara["border-color"])){
			$c= new Color($pdfpara["border-color"]);
			$pdf->SetDrawColor($c->RED,$c->GREEN,$c->BLUE);
		}
		if($tag->TagPara["background-color"]){
			$c= new Color($pdfpara["background-color"]);
			$pdf->SetFillColor($c->RED,$c->GREEN,$c->BLUE);
		}
	}
	return $pdf;
}

?>
Return current item: Template Parser Engine