While creating large projects like school portals,realestate portals.etc. , we need to generate reports in pdf format.So In this Post Iam explaing how to generate pdf reports....
For this we have to download pdf library named TCPDF.
download it from http://www.tcpdf.org/
Include it in your project.
I will explain it with an example..
In my project i have to give attendance report of students in pdf.
Step 1: Fetched data from database and generated a text file.
Step 2: Created PDF file from that text file.
Code for generating Text file
<?php
$student=$_SESSION['loggedstudent'];
$atd=new Attendance();
$stddet=$atd->getstudentthreaddetails($student);
$ses=new Sess();
$nam="Report-";
$nam.=substr(md5(rand(1111,9999)),0,8);
$nam.=strtotime(date("Y-m-d H:i:s"));
$k=0;
$file=fopen("reports/".$nam.".txt","w+");
fclose($file);
for($i=0;$i<count($stddet);$i++){
$threadid=$stddet[$i]['thread_id'];
$det=$atd->getthreaddetails($threadid);
$k=$k+1;
$file=fopen("reports/".$nam.".txt","a");
$data1=$det[0]['date_attendance'].";";
fwrite($file, $data1);
fclose($file);
$dat= $det[0]['date_attendance'];
if(isset($_SESSION['atti'][$dat])){
$_SESSION['atti'][$dat]['sess'.$i]=$det[0]['session'];
}else{
$_SESSION['atti'][$dat]=$det[0]['session'];
}
$ds=explode('-',$dat);
$month=$ds[1];
$file=fopen("reports/".$nam.".txt","a");
switch ($month) {
case '1':
$data="January".";";
break;
case '2':
$data="February".";";
case '3':
$data="March".";";
break;
case '4':
$data="April".";";
case '5':
$data="May".";";
break;
case '6':
$data="June".";";
break;
case '7':
$data="July".";";
break;
case '8':
$data="August".";";
break;
case '9':
$data="September".";";
break;
case '10':
$data="October".";";
break;
case '11':
$data="November".";";
break;
case '12':
$data="December".";";
break;
}
fwrite($file, $data);
fclose($file);
$file=fopen("reports/".$nam.".txt","a");
$sesid= $det[0]['session_id'];
$sesdet=$ses->getcommunitydetails($sesid);
$data2=$sesdet[0]['session_name'].PHP_EOL;
fwrite($file, $data2);
fclose($file);
}
$cou= count($_SESSION['atti']);
unset($_SESSION['atti']);
$file=fopen("reports/".$nam.".txt","a");
$data4="Leaves(Sessions):".$k.";";
fwrite($file, $data4);
fclose($file);
$file=fopen("reports/".$nam.".txt","a");
$data12="Leaves(Days):".$cou.";";
fwrite($file, $data12);
fclose($file);
header("Location:attendancetopdf.php?fl=".$nam."&std=".$student."&hash=".md5(rand(0,5000)));
?>
2) Creating PDF file
<?php
$nam=$_GET['fl'];
$std=$_GET['std'];
ob_start();
session_start();
include("autoload.php");
$db = new MySql();
$db->connect();
$st=new Student();
$stddet=$st->getdetails($std);
$cr=new Branch();
$yr=new Year();
$div=new Division();
$ac=new Academic();
$fac=new Faculty();
$crseid= $stddet[0]['branch'];
$crdet=$cr->getcommunitydetails($crseid);
$branchname= $crdet[0]['branch'];
$crseid= $stddet[0]['year'];
$crdet=$yr->getcommunitydetails($crseid);
$yearname= $crdet[0]['year'];
$crseid= $stddet[0]['div'];
$crdet=$div->getcommunitydetails($crseid);
$divisionname= $crdet[0]['div'];
$crseid= $stddet[0]['academic'];
$crdet=$ac->getcommunitydetails($crseid);
$academicname= $crdet[0]['year'];
// Include the main TCPDF library (search for installation path).
require_once('tcpdf_include.php');
// extend TCPF with custom functions
class MYPDF extends TCPDF {
// Load table data from file
public function LoadData($file) {
// Read file lines
$lines = file($file);
$data = array();
foreach($lines as $line) {
$data[] = explode(';', chop($line));
}
return $data;
}
// Colored table
public function ColoredTable($header,$data) {
// Colors, line width and bold font
$this->SetFillColor(255, 0, 0);
$this->SetTextColor(255);
$this->SetDrawColor(128, 0, 0);
$this->SetLineWidth(0.3);
$this->SetFont('', 'B');
// Header
$w = array(40, 35, 40, 45);
$num_headers = count($header);
for($i = 0; $i < $num_headers; ++$i) {
$this->Cell($w[$i], 7, $header[$i], 1, 0, 'C', 1);
}
$this->Ln();
// Color and font restoration
$this->SetFillColor(224, 235, 255);
$this->SetTextColor(0);
$this->SetFont('');
// Data
$fill = 0;
foreach($data as $row) {
$this->Cell($w[0], 6, $row[0], 'LR', 0, 'L', $fill);
$this->Cell($w[1], 6, $row[1], 'LR', 0, 'L', $fill);
$this->Cell($w[2], 6, $row[2], 'LR', 0, 'R', $fill);
$this->Ln();
$fill=!$fill;
}
$this->Cell(array_sum($w), 0, '', 'T');
}
}
// create new PDF document
$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('');
$pdf->SetTitle('Attendance Report');
$pdf->SetSubject('Report');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
$headertitle="Attendence Report";
$headerstring.="Name: ".$stddet[0]['fname']." ".$stddet[0]['lname'].PHP_EOL;
$headerstring.="Reg no: ".$stddet[0]['regno'].PHP_EOL;
$headerstring.="Roll no: ".$stddet[0]['rollno'].PHP_EOL;
$headerstring.="Email: ".$stddet[0]['email'].PHP_EOL;
$headerstring.="Mobile: ".$stddet[0]['contact_mobile'].PHP_EOL;
$headerstring.="Branch: ".$branchname.PHP_EOL;
$headerstring.="Year: ".$yearname.PHP_EOL;
$headerstring.="Division: ".$divisionname.PHP_EOL;
// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, $headertitle,$headerstring);
// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$marg="55px";
// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, $marg, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
// set some language-dependent strings (optional)
if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
require_once(dirname(__FILE__).'/lang/eng.php');
$pdf->setLanguageArray($l);
}
// ---------------------------------------------------------
// set font
$pdf->SetFont('helvetica', '', 12);
// add a page
$pdf->AddPage();
// column titles
$header = array('Leave date', 'Month', 'Session');
// data loading
$data = $pdf->LoadData('reports/'.$nam.'.txt');
// print colored table
$pdf->ColoredTable($header, $data);
// ---------------------------------------------------------
// close and output PDF document
$pdf->Output('attendancetopdf.pdf', 'I');
//============================================================+
// END OF FILE
//============================================================+
unlink('reports/'.$nam.'.txt');// deleting local file
?>
Now generated pdf file will be generated in new window.
For this we have to download pdf library named TCPDF.
download it from http://www.tcpdf.org/
Include it in your project.
I will explain it with an example..
In my project i have to give attendance report of students in pdf.
Step 1: Fetched data from database and generated a text file.
Step 2: Created PDF file from that text file.
Code for generating Text file
<?php
$student=$_SESSION['loggedstudent'];
$atd=new Attendance();
$stddet=$atd->getstudentthreaddetails($student);
$ses=new Sess();
$nam="Report-";
$nam.=substr(md5(rand(1111,9999)),0,8);
$nam.=strtotime(date("Y-m-d H:i:s"));
$k=0;
$file=fopen("reports/".$nam.".txt","w+");
fclose($file);
for($i=0;$i<count($stddet);$i++){
$threadid=$stddet[$i]['thread_id'];
$det=$atd->getthreaddetails($threadid);
$k=$k+1;
$file=fopen("reports/".$nam.".txt","a");
$data1=$det[0]['date_attendance'].";";
fwrite($file, $data1);
fclose($file);
$dat= $det[0]['date_attendance'];
if(isset($_SESSION['atti'][$dat])){
$_SESSION['atti'][$dat]['sess'.$i]=$det[0]['session'];
}else{
$_SESSION['atti'][$dat]=$det[0]['session'];
}
$ds=explode('-',$dat);
$month=$ds[1];
$file=fopen("reports/".$nam.".txt","a");
switch ($month) {
case '1':
$data="January".";";
break;
case '2':
$data="February".";";
case '3':
$data="March".";";
break;
case '4':
$data="April".";";
case '5':
$data="May".";";
break;
case '6':
$data="June".";";
break;
case '7':
$data="July".";";
break;
case '8':
$data="August".";";
break;
case '9':
$data="September".";";
break;
case '10':
$data="October".";";
break;
case '11':
$data="November".";";
break;
case '12':
$data="December".";";
break;
}
fwrite($file, $data);
fclose($file);
$file=fopen("reports/".$nam.".txt","a");
$sesid= $det[0]['session_id'];
$sesdet=$ses->getcommunitydetails($sesid);
$data2=$sesdet[0]['session_name'].PHP_EOL;
fwrite($file, $data2);
fclose($file);
}
$cou= count($_SESSION['atti']);
unset($_SESSION['atti']);
$file=fopen("reports/".$nam.".txt","a");
$data4="Leaves(Sessions):".$k.";";
fwrite($file, $data4);
fclose($file);
$file=fopen("reports/".$nam.".txt","a");
$data12="Leaves(Days):".$cou.";";
fwrite($file, $data12);
fclose($file);
header("Location:attendancetopdf.php?fl=".$nam."&std=".$student."&hash=".md5(rand(0,5000)));
?>
2) Creating PDF file
<?php
$nam=$_GET['fl'];
$std=$_GET['std'];
ob_start();
session_start();
include("autoload.php");
$db = new MySql();
$db->connect();
$st=new Student();
$stddet=$st->getdetails($std);
$cr=new Branch();
$yr=new Year();
$div=new Division();
$ac=new Academic();
$fac=new Faculty();
$crseid= $stddet[0]['branch'];
$crdet=$cr->getcommunitydetails($crseid);
$branchname= $crdet[0]['branch'];
$crseid= $stddet[0]['year'];
$crdet=$yr->getcommunitydetails($crseid);
$yearname= $crdet[0]['year'];
$crseid= $stddet[0]['div'];
$crdet=$div->getcommunitydetails($crseid);
$divisionname= $crdet[0]['div'];
$crseid= $stddet[0]['academic'];
$crdet=$ac->getcommunitydetails($crseid);
$academicname= $crdet[0]['year'];
// Include the main TCPDF library (search for installation path).
require_once('tcpdf_include.php');
// extend TCPF with custom functions
class MYPDF extends TCPDF {
// Load table data from file
public function LoadData($file) {
// Read file lines
$lines = file($file);
$data = array();
foreach($lines as $line) {
$data[] = explode(';', chop($line));
}
return $data;
}
// Colored table
public function ColoredTable($header,$data) {
// Colors, line width and bold font
$this->SetFillColor(255, 0, 0);
$this->SetTextColor(255);
$this->SetDrawColor(128, 0, 0);
$this->SetLineWidth(0.3);
$this->SetFont('', 'B');
// Header
$w = array(40, 35, 40, 45);
$num_headers = count($header);
for($i = 0; $i < $num_headers; ++$i) {
$this->Cell($w[$i], 7, $header[$i], 1, 0, 'C', 1);
}
$this->Ln();
// Color and font restoration
$this->SetFillColor(224, 235, 255);
$this->SetTextColor(0);
$this->SetFont('');
// Data
$fill = 0;
foreach($data as $row) {
$this->Cell($w[0], 6, $row[0], 'LR', 0, 'L', $fill);
$this->Cell($w[1], 6, $row[1], 'LR', 0, 'L', $fill);
$this->Cell($w[2], 6, $row[2], 'LR', 0, 'R', $fill);
$this->Ln();
$fill=!$fill;
}
$this->Cell(array_sum($w), 0, '', 'T');
}
}
// create new PDF document
$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('');
$pdf->SetTitle('Attendance Report');
$pdf->SetSubject('Report');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
$headertitle="Attendence Report";
$headerstring.="Name: ".$stddet[0]['fname']." ".$stddet[0]['lname'].PHP_EOL;
$headerstring.="Reg no: ".$stddet[0]['regno'].PHP_EOL;
$headerstring.="Roll no: ".$stddet[0]['rollno'].PHP_EOL;
$headerstring.="Email: ".$stddet[0]['email'].PHP_EOL;
$headerstring.="Mobile: ".$stddet[0]['contact_mobile'].PHP_EOL;
$headerstring.="Branch: ".$branchname.PHP_EOL;
$headerstring.="Year: ".$yearname.PHP_EOL;
$headerstring.="Division: ".$divisionname.PHP_EOL;
// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, $headertitle,$headerstring);
// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$marg="55px";
// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, $marg, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
// set some language-dependent strings (optional)
if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
require_once(dirname(__FILE__).'/lang/eng.php');
$pdf->setLanguageArray($l);
}
// ---------------------------------------------------------
// set font
$pdf->SetFont('helvetica', '', 12);
// add a page
$pdf->AddPage();
// column titles
$header = array('Leave date', 'Month', 'Session');
// data loading
$data = $pdf->LoadData('reports/'.$nam.'.txt');
// print colored table
$pdf->ColoredTable($header, $data);
// ---------------------------------------------------------
// close and output PDF document
$pdf->Output('attendancetopdf.pdf', 'I');
//============================================================+
// END OF FILE
//============================================================+
unlink('reports/'.$nam.'.txt');// deleting local file
?>
Now generated pdf file will be generated in new window.
No comments:
Post a Comment