X
    Categories: PHP

How to convert html to pdf in php using dompdf?

I have received the request from my reader for HTML to PDF conversion using PHP. Here I have used DOMPDF for PHP HTML to PDF conversion.

It is very easy and simple to convert HTML to PDF conversion.

Please follow below steps:

STEP 1:

Download DOMPDF library file

STEP 2 :

Include DOMPDF library file into your index.php file

require_once (‘dompdf_config.inc.php’);

 

STEP 3:

Save your HTML coding in one variable

eg :

$pdf_content='
			<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
			<html xmlns="http://www.w3.org/1999/xhtml">
			<head>
			<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
			</head>
		
			<style type="text/css">							
				#pdf_header, #pdf_container{ border: 1px solid #CCCCCC; padding:10px; }				
				#pdf_header{ margin:10px auto 0px; border-bottom:none; }				
				table{ width:580px; }				
				#pdf_container{margin:0px auto; }
				.rpt_title{ background:#99CCFF; }															
			</style>
							
			<body>
			<div id="pdf_header" >
			<table border="0" cellspacing="1" cellpadding="2">
			<tr id="hdRow">
				<td width="20%"><img src="space_age_header.jpg" style="width:250px" ></td>				
				<td width="30%" align="center">Sample File</td>
				<td width="30%" align="left">Marimuthu<br>User Code : 179865420</td>
			</tr>
			</table>
			</div>
			<div id="pdf_container" >
			<table border="0" cellspacing="1" cellpadding="2">
			<tr align="center" bgcolor="pink" style="color:#FFF"><td colspan="3"><b>Your Statement Summery</b></td> </tr>
			<tr bgcolor="#006" style="color:#FFF"><td colspan="3" align="left">Your Heading.</td></tr>
	 		</table>
			<table> <tr> <td> Name </td><td> Department</td><td>Total </td><td>Grade </td> </tr>
			<tr> <td> Marimuthu </td><td> Admin</td><td>250 </td><td>A </td> </tr>			
	 		</div></body></html>'
			;

 

STEP 4 :

Create object for your DOMPDF($dompdf=new DOMPDF();)
Call your HTML content to DOMPDF object.($dompdf->load_html($pdf_content);)
Render your PHP as PDF by $dompdf->render();
Convert HTML as PDF by $dompdf->output();
Store your PDF in path by file_put_contents(‘path’, $dompdf->output());


or simple use the below codings.

$name = date("Ymd").rand().'.pdf';
	$reportPDF=createPDF(12, $pdf_content, 'example', $name );
	function createPDF($pdf_userid, $pdf_content, $pdf_For, $filename){
	
	$path='example/';
	/*$rndNumber=rand();
	$filename=$pdf_userid.date("Ymd").$rndNumber.'.pdf';*/
	$dompdf=new DOMPDF();
	$dompdf->load_html($pdf_content);
	$dompdf->render();
	$output = $dompdf->output();
	file_put_contents($path.$filename, $output);
	return $filename;		
	}	
	echo '<a href="example/'.$name.'" > Download as PDF </a>';

 

 

Thanks!. If you like this article please don’t forget to share and comment.

 

Marimuthu:

View Comments