Convert HTML to PDF using php
- First of all we need the supporting file so please download TCPDF
- Just create pdf.php file and copy the below code.
- The supporting file locate as the same folder or you just modify the link in above code as require_once(dirname(__FILE__).'/tcpdf/tcpdf.php');.
<?php
$url=parse_url($_SERVER['HTTP_REFERER']);
// Include the main TCPDF library (search for installation path).
require_once(dirname(__FILE__).'/tcpdf/tcpdf.php');
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Sakthivel');
$pdf->SetTitle('talentcoders.blogspot.com Download pdf');
$pdf->SetSubject('Downloadable pdf');
$pdf->SetKeywords('PDF for invoice');
// set default header data
/*$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 001', PDF_HEADER_STRING, array(0,64,255), array(0,64,128));*/
/*$pdf->setFooterData(array(0,64,0), array(0,64,128));*/
// 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));
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
// set margins
$pdf->SetMargins(10, 10, 10);
$pdf->SetHeaderMargin(0);
$pdf->SetFooterMargin(0);
// 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 default font subsetting mode
$pdf->setFontSubsetting(true);
// Set font
// dejavusans is a UTF-8 Unicode font, if you only need to
// print standard ASCII chars, you can use core fonts like
// helvetica or times to reduce file size.
$pdf->SetFont('dejavusans', '', 12, '', true);
// Add a page
// This method has several options, check the source code documentation for more information.
/*$pdf->setTextShadow(array('enabled'=>true, 'depth_w'=>0.2, 'depth_h'=>0.2, 'color'=>array(196,196,196), 'opacity'=>1, 'blend_mode'=>'Normal'));*/
// Set some content to print
$pdf->AddPage();
$html = '
<div style="width: 100%;">
<h2 style="text-align: center; margin-bottom:20px;">INVOICE</h2>
<hr/>
<div style="padding: 20px;">
<table cellpadding="4">
<tr>
<td><b>Invoice</b></td>
<td style="text-align:right;"><b>Order # 12345</b></td>
</tr>
<tr style="margin-top:30px;">
<td><b>To: </b>'.$download[0]['project_address'].'</td>
<td>
<tr>
<td style="text-align:right;"><b>Invoice No: </b></td>
</tr>
<tr>
<td style="text-align:right;"><b>Date: </b>'.$download[0]['timesheet_app_on'].'</td>
</tr>
<tr>
<td style="text-align:right;"><b>Project ID: </b>'.$download[0]['emp_project_id'].'</td>
</tr>
<tr>
<td style="text-align:right;"><b>Project Name: </b>'.$download[0]['project_name'].'</td>
</tr>
<tr>
<td style="text-align:right;"><b>Purchase Order NO: </b>'.$download[0]['emp_purchase_no'].'</td>
</tr>
</td>
</tr>
</table>
<address>
<b>ATTN:</b>
Sathish Kumar<br>
</address>
</div>
<div style="font-size: 20px; font-weight: 600; text-align: center;">INVOCE FOR THE MONTH OF SEPTEMPER 2015</div>
<h3 style="font-size: 16px;"><b>I.Service Income</b></h3>
<table style="width:100%; border:1px solid #000; border-collapse: collapse; padding-bottom: 20px;" cellpadding="5" border="1">
<thead>
<tr>
<td><b>SI.NO</b></td>
<td><b>Resource Name</b></td>
<td class="text-center"><b>Designation</b></td>
<td class="text-center"><b>Man Days</b></td>
<td class="text-right"><b>Rate(RS)</b></td>
<td class="text-right"><b>Total</b></td>
</tr>
</thead>
<tbody>
';
$i=1;
$total1=0;
foreach($download as $detail)
{
$html.='<tr>
<td cellspacing="1">'.$i.'</td>
<td><div name="emp_fname">'.$detail['emp_fname'].'</div></td>
<td class="text-center"><div name="emp_role">'.$detail['emp_role'].'</div></td>
<td class="text-right"><div name="working_days">'.$detail['working_days'].'</div></td>
<td class="text-right"><div name="emp_salary">'.$detail['emp_salary'].'</div></td>
<td class="text-right">'.$total[$detail['timesheet_id']].'</td>
</tr>
';
$i++;
$total1+=$total[$detail['timesheet_id']];
}
$html.='
<tr>
<td style="border-right:0px solid #fff;"></td>
<td style="border-right:0px solid #fff;"><div name="emp_fname"></div></td>
<td style="border-right:0px solid #fff;"><div name="emp_role"></div></td>
<td style="border-right:0px solid #fff;"><div name="working_days"></div></td>
<td style="border-right:1px solid #000;"><div name="emp_salary"><b>Grand Total</b></div></td>
<td>'.$total1.'</td>
</tr>
</tbody>
</table>
<div style="margin-top: 30px;">
<div style="font-size: 20px; text-align: left; ">PAYMENT DETAILS</div>
<table style="font-size: 15px;">
<tr>
<td>NAME OF THE ACCOUNT:</td>
<td>Company Name</td>
</tr>
<tr>
<td>BANK:</td>
<td>ICICI BANK</td>
</tr>
<tr>
<td class="col-xs-5">BRANCH NAME:</td>
<td class="col-xs-6">VV MOHALLA BRANCH, K.D.ROAD, MYSURU</td>
</tr>
<tr>
<td>ACCOUNT NO:</td>
<td>XXXXXXXXXXX</td>
</tr>
<tr>
<td>IFSC CODE:</td>
<td>XXXXXXXXXXXX</td>
</tr>
</table>
</div>
</table>
';
$pdf->writeHTML($html, true, 0, true, 0, '');
ob_end_clean();
echo $pdf->Output('Invoice '.date('YmdHis').'_report.pdf','D');
//============================================================+
// END OF FILE
//============================================================+
That's all Enjoy!
