Powered By Blogger

Sunday, February 2, 2014

MySQL database data export to pdf using PHP

download fpdf.php library from website and import with this






<?php
require('fpdf.php');
$d=date('d_m_Y');

class PDF extends FPDF
{

function Header()
{
    //Logo
$name="Export PDF";
    $this->SetFont('Arial','B',15);
    //Move to the right
    $this->Cell(180);
    //Title
$this->SetFont('Arial','B',12);
    //Line break
    $this->Ln(20);
}

//Page footer
function Footer()
{
 
}

//Load data
function LoadData($file)
{
//Read file lines
$lines=file($file);
$data=array();
foreach($lines as $line)
$data[]=explode(';',chop($line));
return $data;
}

//Simple table
function BasicTable($header,$data)
{

$this->SetFillColor(205,205,205);
$this->SetTextColor(0,0,0);
$this->SetDrawColor(239,85,35);

$w=array(40,30,40,40,40,10,10,10,15,15,15,15,15);

//Header
for($i=0;$i<count($header);$i++)
$this->Cell($w[$i],7,$header[$i],1,0,'C',true);
$this->Ln();
//Data
foreach ($data as $eachResult)
{ //width
$this->Cell(40,6,$eachResult["date"],1);
$this->Cell(30,6,$eachResult["order_id"],1);
$this->Cell(40,6,$eachResult["total"],1);
$this->Cell(40,6,$eachResult["tax"],1);
$this->Cell(40,6,$eachResult["shipping"],1);
$this->Ln();

}
}

//Better table
}

$pdf=new PDF();
$header=array('date','order_id','total','tax','shipping');
//Data loading
//*** Load MySQL Data ***//


$strSQL = "SELECT date,COUNT(`order_id`) AS `order_id`, SUM(`total`) AS `total`, SUM(`tax`) AS `tax`, SUM(`shipping`) AS `shipping` from `order` WHERE date >= '".$from."' AND date <= '".$to."' GROUP BY YEAR(`date`) ";
$objQuery = mysql_query($strSQL);
$resultData = array();
for ($i=0;$i<mysql_num_rows($objQuery);$i++) {
$result = mysql_fetch_array($objQuery);
array_push($resultData,$result);
}
//************************//

function forme()

{
$d=date('d_m_Y');
echo "<a target='blank' href=".$d.".pdf>Report export to PDF</a>";
}


$pdf->SetFont('Arial','',12);

//*** Table 1 ***//
$pdf->AddPage();
$pdf->Ln(10);
$pdf->BasicTable($header,$resultData);
$pdf->Image('images/logo.jpg',10,10,-88);
$pdf->Text( 10, 35,'Order Report');
forme();
$pdf->Output("$d.pdf","F");

?>

No comments:

Post a Comment