Location: PHPKode > projects > PM Report for PostgreSQL > pm_report/generate_pdf.php
<?
define('FPDF_FONTPATH','font/');
require('fpdf.php');
class PDF extends FPDF {
	function Header() {
		if($_SESSION[no_header] != "yes") {
			if($_SESSION[print_logo] != "") {
				if($_SESSION[pdf_layout] == "P") {
					$this->Image($_SESSION[print_logo],10,10,'',10);
				} elseif($_SESSION[pdf_layout] == "L") {
					$this->Image($_SESSION[print_logo],10,10,'',10);
				}
				$this->Ln(15);
			}
		}
		$this->SetFont('Arial','B','10');
		$this->Cell(0,0,$_SESSION[title],0,1,'C');
		if($_SESSION[pdf_date] == "yes") {
			$todays_date = date("m/d/Y", time());
			$this->Cell(0,0,$todays_date,0,1,'R');
		}
		$this->Ln(5);
		$row = explode(";", $_SESSION[header_data]);
		$rownums = count($row);
		$w = explode(";", $_SESSION[longest_values]);
		for($i = 0;$i < $rownums;$i++) {
			$total_width += $w[$i];
		}
		if($_SESSION[pdf_layout] == "P") {
			$width_check = 190-$total_width;
			if($width_check > 0) {
				$indent = round((190-$total_width)/2);
			} else {
				$indent = 0;
			}
		} elseif($_SESSION[pdf_layout] == "L") {
			$width_check = 275-$total_width;
			if($width_check > 0) {
				$indent = round((275-$total_width)/2);
			} else {
				$indent = 0;
			}
		}
		if($_SESSION[no_header] != "yes") {
			$this->SetFillColor(0,0,0);
			$this->SetDrawColor(0);
			$this->SetTextColor(255);
			$this->SetLineWidth(.3);
			$this->SetFont('Arial','B','8');
			if($indent != 0) {
				$this->Cell($indent,5,"",0,0,'L',0);
			}
			for($i = 0;$i < $rownums;$i++) {
				$row[$i] = str_replace("_", " ", $row[$i]);
				$row[$i] = ucwords($row[$i]);
				$this->Cell($w[$i],5,$row[$i],1,0,'C',1);
			}
		}
		$this->Ln();
	}
	function Footer() {
		$this->SetY(-15);
		$this->SetFont('Arial','B','8');
		$this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
	}
	function LoadData($file) {
		$lines=file($file);
		$numres = count($lines);
		$_SESSION[num_of_rows] = $numres;
		$data=array();
		foreach($lines as $line)
		$data[]=explode(';',chop($line));
		return $data;
	}
	function ColorTable($header,$data) {
		$this->SetFillColor(200);
		$this->SetDrawColor(0);
		$this->SetTextColor(0);
		$this->SetFont('Arial','','8');
		$fill=0;
		foreach($data as $row) {
			$rownums = count($row);
			if($graph_data_type == "totals") {
				$_SESSION[num_of_rows] = $rownums;
			}
			$w = explode(";", $_SESSION[longest_values]);
			$alignment = explode(";", $_SESSION[pdf_alignment]);
			for($i = 0;$i < $rownums;$i++) {
				$total_width += $w[$i];
			}
			if($_SESSION[pdf_layout] == "P") {
				$width_check = 190-$total_width;
				if($width_check > 0) {
					$indent = round((190-$total_width)/2);
				} else {
					$indent = 0;
				}
			} elseif($_SESSION[pdf_layout] == "L") {
				$width_check = 275-$total_width;
				if($width_check > 0) {
					$indent = round((275-$total_width)/2);
				} else {
					$indent = 0;
				}
			}
			if($indent != 0) {
				$this->Cell($indent,5,"",0,0,'L',0);
			}
			for($i = 0;$i < $rownums;$i++) {
				if($alignment[$i] == "R") {
					$this->Cell($w[$i],5,$row[$i],1,0,$alignment[$i],$fill);
				} else {
					$this->Cell($w[$i],5,$row[$i],1,0,$alignment[$i],$fill);
				}
			}
			$this->Ln();
			$fill=!$fill;
			$total_width = 0;
		}
		if($_SESSION[any_totals] == "yes") {
			for($i = 0;$i < $rownums;$i++) {
				$total_width += $w[$i];
			}
			$this->Cell($indent,5,"",0,0,'L',0);
			$this->SetFillColor(0);
			$this->SetDrawColor(0);
			$this->SetTextColor(255);
			$this->Cell($total_width,5,"Column Totals",1,0,C,1);
			$this->Ln();
			$this->Cell($indent,5,"",0,0,'L',0);
			$totals = explode(";", $_SESSION[total_pdf_array]);
			$this->SetFillColor(255);
			$this->SetDrawColor(0);
			$this->SetTextColor(0);
			for($i = 0;$i < $rownums;$i++) {
					$this->Cell($w[$i],5,$totals[$i],1,0,R,0);
			}
		}
		$this->Ln(10);
	}
}
$pdf=new PDF();
$_SESSION[title] = $_POST[title];
$_SESSION[pdf_layout] = $_POST[pdf_layout];
$_SESSION[pdf_date] = $_POST[pdf_date];
$_SESSION[header_data] = $_POST[header_data];
$_SESSION[longest_values] = $_POST[longest_values];
$_SESSION[pdf_alignment] = $_POST[pdf_alignment];
$_SESSION[any_totals] = $_POST[any_totals];
$_SESSION[total_pdf_array] = $_POST[total_pdf_array];
$_SESSION[create_graph] = $_POST[create_graph];
$_SESSION[pdf_graph] = $_POST[graph_type];
$_SESSION[graph_data_type] = $_POST[graph_data_type];
$_SESSION[pdf_graph_values] = $_POST[pdf_graph_values];
$_SESSION[pdf_graph_title] = $_POST[pdf_graph_title];
$_SESSION[print_logo] = $_POST[print_logo];
$pdf->AddPage($_SESSION[pdf_layout]);
$pdf->AliasNbPages();
$pdf->SetFont('Arial','',8);
$data=$pdf->LoadData('EXPORT_REPORT.csv');
$pdf->ColorTable($header,$data);
if($_SESSION[create_graph] == "yes") {
	$_SESSION[no_header] = "yes";
	$pdf->AddPage($_SESSION[pdf_layout]);
	$pdf->AliasNbPages();
	$pdf->SetFont('Arial','',8);

	require("chart.php");
	$data = explode(",", $pdf_graph_values);
	if($_SESSION[pdf_layout] == "P") {
		$chart = new chart(535, 720);
	} elseif($_SESSION[pdf_layout] == "L") {
		$chart = new chart(785, 485);
	}
	if($_SESSION[pdf_graph_title] != "") {
		$chart->set_title("$_SESSION[pdf_graph_title]");
	}
	if($_SESSION[pdf_graph] == "line_graph") {
		$chart->plot($data, false, "black");
	} elseif($_SESSION[pdf_graph] == "bar_graph") {
		$chart->plot($data, false, "black", "impulse");
	} elseif($_SESSION[pdf_graph] == "box_graph") {
		$chart->plot($data, false, "gray", "box", "black");
	}
	$chart->stroke();
	$pdf->Ln();
	if($_SESSION[pdf_layout] == "P") {
		$pdf->Image("pdf_image.png",10,20);
	} elseif($_SESSION[pdf_layout] == "L") {
		$pdf->Image("pdf_image.png",10,20);
	}
}
$pdf->Output();
?>
Return current item: PM Report for PostgreSQL