Step1- For converting html to pdf first we have to download dompdf, then we have to extract it and put it into /app/Vendor.
Step2- Then we have to import this dompdf folder and its file dompdf_config.inc.php by using below code in our controller.
App::import('Vendor', 'dompdf',array('file'=>'dompdf/dompdf_config.inc.php'));
step3- Then we have to create view which contain link on which we click, it will converts html to pdf.
Our view contain below code:-
<?php echo $this->Html->link('DomPdf',array('controller'=>'Users','action'=>'dompdf'));
?>
step4- Then we have to create function in our controller which contain the logic for html to pdf conversion.
public function dompdf(){
$dompdf = new DOMPDF();
$result = $this->User->find('all');
$html1= "";
foreach ($result as $value) {
$html1 = $html1."<tr><td>".$value['User']['id']."</td>
<td>".$value['User']['username']."</td>
<td>".$value['User']['phone']."</td>
<td>".$value['User']['email']."</td>
<td>".$value['User']['gender']."</td></tr>";
}
$html = '<html>
<body>
<p>Users List:</p>
<table border=1>
<tr>
<th>ID</th>
<th>Name</th>
<th>Phone No:</th>
<th>Email Id</th>
<th>Pic</th>
</tr>'.$html1.'
</table>
</body>
</html>
';
$dompdf->load_html($html);
$dompdf->render();
$output = $dompdf->output();
file_put_contents("file.pdf", $output);
}
In above code we our fetching data from our database then converting that data into html then converting it into pdf.
1 Comment(s)