HTML to PDF using TCPDF in Cakephp 2
Hello friends, today I am writing this blog which will let you know how to convert HTML to PDF using Cakephp 2.x. We are going to use TCPF library to achieve this.
Lets begin with downloading TCPDF from Github , Click here to download TCPDF.
After downloading it, extract TCPDF zip folder. Now move TCPDF folder to /app/Vendor directory. Now rename folder to tcpdf and create xtcpdf.php file in /app/Vendor/ directory and enter these lines of code:
<?php
App::import('Vendor','tcpdf/tcpdf');
class XTCPDF extends TCPDF{
}
Now create default.ctp layout in this directory: app/View/Layouts/pdf and write the following code:
<?php
header("Content-type: application/pdf");
echo $content_for_layout;
Now in your controller say UsersController write the following function:
public function generate_pdf(){
$users = $this->User->find('all');
$this->set(compact('invoice'));
$this->layout = '/pdf/default';
$this->render('/Pdf/pdfview');
}
Now create pdfview.ctp file in /app/View/Pdf/ directory and write the following code in it and save the file.
<?php
App::import('Vendor','xtcpdf');
$pdf = new XTCPDF('L', PDF_UNIT, 'A4', true, 'UTF-8', false);
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Amuk Saxena');
$pdf->SetTitle('Invoice');
$pdf->SetSubject('Invoice Receipt');
$pdf->SetKeywords('Invoice');
//$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, 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);
$pdf->AddPage();
// create some HTML content
$subtable = '<table cellspacing="5" width="90%">
<tr>
<td border="1" bgcolor="#eee" align="center" border-color="#bbb">Invoice #</td>
<td border="1" align="center">101138</td>
</tr>
<tr>
<td border="1" bgcolor="#eee" align="center" border-color="#bbb">Date</td>
<td border="1" align="center">'.$invoice['date'].'</td>
</tr>
<tr>
<td border="1" bgcolor="#eee" align="center" border-color="#bbb">Amount</td>
<td border="1" align="center">INR '.$invoice['amount'].'</td>
</tr>
</table>';
$newtable='<table cellspacing="5" cellpadding="4">
<tr>
<th border="1" bgcolor="#eee" align="center"><h4>Item</h4></th>
<th border="1" bgcolor="#eee" align="center"><h4>Category</h4></th>
<th border="1" bgcolor="#eee" align="center"><h4>Sub-Category</h4></th>
<th border="1" bgcolor="#eee" align="center"><h4>Duration</h4></th>
<th border="1" bgcolor="#eee" align="center"><h4>Price</h4></th>
</tr>
<tr>
<td border="1" align="center">Subscription Plan</td>
<td border="1" align="center">'.$invoice['category'].'</td>
<td border="1" align="center">'.$invoice['subcategory'].'</td>
<td border="1" align="center">'.$invoice['duration'].' Years</td>
<td border="1" align="center">INR '.$invoice['amount'].'</td>
</tr>
</table>';
$card = ($invoice['paymentmode'] == "debit_card") ? "Debit Card" : ($invoice['paymentmode'] == "credit_card") ? "Credit Card" : "PayPal" ;
$payment_mode='<table cellspacing="5" cellpadding="4" align="center" width="90%">
<tr>
<th border="1" bgcolor="#eee">Payment Mode</th>
<td border="1">'.$card.'</td>
</tr>
<tr>
<th border="1" bgcolor="#eee">Date</th>
<td border="1">'.$invoice['date'].'</td>
</tr>
<tr>
<th border="1" bgcolor="#eee">Total Amount</th>
<td border="1">INR '.$invoice['amount'].'</td>
</tr>
</table>';
$html = '<h1 bgcolor="#00000;" color="white" align="center">'.$invoice['title'].'</h1>
<table cellspacing="3" cellpadding="4">
<tr>
<th>
'.$invoice['first_name'].' '.$invoice['last_name'].'<br>
Unique Reference No. : '.$invoice['reference_number'].'<br>
'.$invoice['building_name'].'<br>
'.$invoice['street_name'].'<br>
'.$invoice['city'].' '.$invoice['postal_code'].' , '.$invoice['country'].'<br>
Phone : '.$invoice['mobile_no'].'<br>
Email : '.$invoice['email'].'
</th>
<th align="center"><img alt="" src="'.Configure::read('SITE_URL').'/img/logo.png"></th>
</tr>
<tr>
<td><h1>Transaction Id : '.$invoice['transaction_id'].'</h1></td>
<td align="right">'.$subtable.'</td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td colspan="2">'.$newtable.'</td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td>'.$payment_mode.'</td>
</tr>
</table>
<table align="center">
<tr><td></td></tr>
<tr><td align="center">For any queries and concern please feel free to contact us <a href="#">info@experconsultant.com</a></td></tr>
<tr><td></td></tr>
<tr><td align="center"><a href="'.Configure::read('SITE_URL').' ">www.domain.com</a></td></tr>
</table>
';
$pdf->writeHTML($html, true, false, true, false, '');
$pdf->lastPage();
$pdf->Output(APP . 'webroot/files/pdf' . DS . .'amuk.pdf', 'F');
You can modify HTML accordingly , for more details you can see the examples by clicking on this link: www.tcpdf.org/examples.php
Before executing the code, firstly create the following directory: /app/files/pdf/ or you can create directory in webroot folder as well and modify your code accordingly.
$pdf->Output(APP. 'files/pdf'.Ds.'amuk.pdf','F');
Here F means that it will save the file to disk.
If you want to download your created pdf file, then you need to write the following code in Controller:
public function download_pdf() {
$this->viewClass = 'Media';
$params = array(
'id' => 'test.pdf',
'name' => 'your_test' ,
'download' => true,
'extension' => 'pdf',
'path' => APP . 'files/pdf' . DS
);
$this->set($params);
}
If you don't want to download the pdf file, instead you want to show the pdf file then you need to write the following code:
public function show_pdf() {
$this->viewClass = 'Media';
$params = array(
'id' => 'test.pdf',
'name' => 'your_test' ,
'download' => false,
'extension' => 'pdf',
'path' => APP . 'files/pdf' . DS
);
$this->set($params);
}
Here download is set to FALSE in order to show the pdf.
Now you can create, view and download the pdf using following URL:
www.yourdomain.com/users/create_pdf, www.yourdomain.com/users/show_pdf, www.yourdomain.com/users/download_pdf
Thanks for reading.
0 Comment(s)