Monday 12 May 2014

Generating PDF in PHP using mPDF

When you work on large inventory projects in php, there may araise situation where you have to generate pdf dynamically for reports and all. So here Iam explaining about a simple php to PDF library named mpdf.
you can download it from here.
Now please download the latest version of the mpdf library and place it in your root folder of your project.
Here with the help of mpdf  you can generate pdf according to various styles using css. You can include css also using this library.

Now i will provide a sample code for generating report using mpdf.


<?php

// fetching data from database.. You can use your own methods
include("db.php");
$id=$_GET['id'];
$quo=new Quotation();
$orders=$quo->getuserquotations($id);
$orderdet=$quo->getdetails($id);
$prd=new Product();/
$amount=0;

//we have to save our design in this $html variable
$html = '
<h1>Report Detail</h1>
<p style="color:green"> Description here </p>
</br>';
$html.='<p><strong>Attn </strong>: Mr/Ms &nbsp;';
$html.=$orderdet[0]['name'];
$html.='&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>Email</strong>:&nbsp;';
$html.=$orderdet[0]['email'];
$html.='</p>';
$html.='<p><strong>Mobile</strong>:  &nbsp;';
$html.=$orderdet[0]['mobile'];
$html.='&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;<strong>Tel</strong>:&nbsp;';
$html.=$orderdet[0]['phone'];
$html.='</p>';
$html.='<p><strong>Fax</strong>:  &nbsp;';
$html.=$orderdet[0]['fax'];
$html.='&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;<strong>Date</strong>:&nbsp;';
$html.=$orderdet[0]['date_booked'];
$html.='</p>';

$html.='<table border="1"><tbody>
<tr> <td><strong>Sr No. </strong></td><td><strong>Product Image </strong></td><td><strong>Description </strong></td><td><strong>QTY </strong></td> <td><strong>Rate </strong></td> <td><strong>Amount </strong></td> </tr>';
for($i=0;$i<count($orders);$i++){
$productid= $orders[$i]['prod_id']; 
     $proddet=$prd->getdetails($productid);

     $tt= $orders[$i]['qty']*$orders[$i]['price']; 
     $amount=$amount+$tt;
$html.='<tr><td>';
$html.=$i+1;
$html.='</td><td><img src="../upload/';
$html.=$proddet[0]['product_img'];
$html.='" width="150px"></td><td>';
$html.=$proddet[0]['description'];
$html.='</td><td>';
$html.=$orders[$i]['qty'];
$html.='</td><td>';
$html.=$orders[$i]['price'];
$html.='</td><td>';
$html.=$tt;
$html.='</td></tr>';
}

$html.='<tr><td></td> <td></td> <td></td> <td></td> <td>Total:</td> <td><span style="color:red">'.$amount.'</span>/-</td> </tr>';


$html.='
</tbody></table>
<p>&nbsp;</p>
';
// $html variabl ends here...

//now its time to include our library and generate report
include("mpdf.php");

$mpdf=new mPDF('c','A4','','',32,25,27,25,16,13); 

$mpdf->SetDisplayMode('fullpage');

$mpdf->list_indent_first_level = 0; // 1 or 0 - whether to indent the first level of a list

// LOAD a stylesheet
$stylesheet = file_get_contents('mpdfstyletables.css');
$mpdf->WriteHTML($stylesheet,1); // The parameter 1 tells that this is css/style only and no body/html/text
$mpdf->SetJS('this.print();'); // will print the pdf
$mpdf->WriteHTML($html,2);
$ext="pdf";
$savename="quote-".substr(md5(rand(1111,9999)),0,8).".".$ext;
$mpdf->Output($savename,'I'); // will display pdf to you in browser
exit;
?>
If you use

$mpdf->Output($savename,'D');

it will download the pdf to your computer.

If you use
$mpdf->Output($savename,'F');

it will download pdf to your local folder..you can set the path $savename variable

If you use
$mpdf->Output($savename,'I');
it will display pdf in your browset window itself..

No comments:

Post a Comment