How to convert HTML to PDF in PHP

In this article we learn how to convert HTML code With CSS to PDF using PHP ,For HTML code to PDF conversion we use TC PDF library.

HTML to PDF conversion is always a problem for PHP Programmers and all the time they search for suitable solutions so after reviewing this article you will not take more than 10 minutes to configure HTML to PDF.

Download TC PDF library.

HTML Code

<!DOCTYPE html>
<html lang="en">
<head>
  <title>HTML to PDF Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">

</head>
<body>

<div style="text-align:center;" >
  <h2>HTML to PDF Example</h2> 
  
  <table border="1" cellspacing="0" cellpadding="20" align="center" >
    <thead>
      <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>Email</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>John</td>
        <td>Doe</td>
        <td>example@example.com</td>
      </tr>
      <tr>
        <td>Mary</td>
        <td>Moe</td>
        <td>example@example.com</td>
      </tr>
      <tr>
        <td>July</td>
        <td>Dooley</td>
        <td>example@example.com</td>
      </tr>
    </tbody>
  </table>
</div>	
	
<div style="text-align:center;" >
<form method="post" >
<textarea name="pdf_data" style="display:none;" >
<div style="text-align:center;" >
  <h2>HTML to PDF Example</h2>    
  
  <table border="1" cellspacing="0" cellpadding="20" align="center" >
    <thead>
      <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>Email</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>John</td>
        <td>Doe</td>
        <td>john@example.com</td>
      </tr>
      <tr>
        <td>Mary</td>
        <td>Moe</td>
        <td>mary@example.com</td>
      </tr>
      <tr>
        <td>July</td>
        <td>Dooley</td>
        <td>july@example.com</td>
      </tr>
    </tbody>
  </table>
</div>

</textarea>
<button type="submit" name="download" class="btn btn-success" value="submit" >Download PDF</button>

</form>
</div>

</body>
</html>

PHP Code

<?php 
	function pdf($htmldata){
        ini_set('max_execution_time', -1);
		include"TCPDF-master/tcpdf.php";
		// create new PDF document
		$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
		// set default monospaced font
		$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
		// set margins
		$pdf->SetMargins(PDF_MARGIN_LEFT, "", PDF_MARGIN_RIGHT);
		// remove default header/footer
		$pdf->setPrintHeader(false);
		$pdf->setPrintFooter(false);
        // set image scale factor
		$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

		// set some language dependent data:
		$lg = Array();
		$lg['a_meta_charset'] = 'UTF-8';
		$lg['a_meta_dir'] = 'auto';
		$lg['a_meta_language'] = 'fa';
		$lg['w_page'] = 'page';

		// set some language-dependent strings (optional)
		$pdf->setLanguageArray($lg);
        // set font
		$pdf->SetFont('dejavusans', '', 12);

		// add a page
		$pdf->AddPage();
        $pdf->WriteHTML($htmldata, true, 0, true, 0);
		$pdf->Output('invoice.pdf', 'D');
	}
	
	if(isset($_REQUEST['download'])){
		
		$pdf_data = $_REQUEST['pdf_data'];
		pdf($pdf_data);
	}
?>

Complete page source here.

<?php 
	function pdf($htmldata){
        ini_set('max_execution_time', -1);
		include"TCPDF-master/tcpdf.php";
		// create new PDF document
		$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
		// set default monospaced font
		$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
		// set margins
		$pdf->SetMargins(PDF_MARGIN_LEFT, "", PDF_MARGIN_RIGHT);
		// remove default header/footer
		$pdf->setPrintHeader(false);
		$pdf->setPrintFooter(false);
        // set image scale factor
		$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

		// set some language dependent data:
		$lg = Array();
		$lg['a_meta_charset'] = 'UTF-8';
		$lg['a_meta_dir'] = 'auto';
		$lg['a_meta_language'] = 'fa';
		$lg['w_page'] = 'page';

		// set some language-dependent strings (optional)
		$pdf->setLanguageArray($lg);
        // set font
		$pdf->SetFont('dejavusans', '', 12);

		// add a page
		$pdf->AddPage();
        $pdf->WriteHTML($htmldata, true, 0, true, 0);
		$pdf->Output('invoice.pdf', 'D');
	}
	
	if(isset($_REQUEST['download'])){
		
		$pdf_data = $_REQUEST['pdf_data'];
		pdf($pdf_data);
	}
?>

<!DOCTYPE html>
<html lang="en">
<head>
  <title>HTML to PDF Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">

</head>
<body>

<div style="text-align:center;" >
  <h2>HTML to PDF Example</h2> 
  
  <table border="1" cellspacing="0" cellpadding="20" align="center" >
    <thead>
      <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>Email</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>John</td>
        <td>Doe</td>
        <td>example@example.com</td>
      </tr>
      <tr>
        <td>Mary</td>
        <td>Moe</td>
        <td>example@example.com</td>
      </tr>
      <tr>
        <td>July</td>
        <td>Dooley</td>
        <td>example@example.com</td>
      </tr>
    </tbody>
  </table>
</div>	
	
<div style="text-align:center;" >
<form method="post" >
<textarea name="pdf_data" style="display:none;" >
<div style="text-align:center;" >
  <h2>HTML to PDF Example</h2>    
  
  <table border="1" cellspacing="0" cellpadding="20" align="center" >
    <thead>
      <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>Email</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>John</td>
        <td>Doe</td>
        <td>john@example.com</td>
      </tr>
      <tr>
        <td>Mary</td>
        <td>Moe</td>
        <td>mary@example.com</td>
      </tr>
      <tr>
        <td>July</td>
        <td>Dooley</td>
        <td>july@example.com</td>
      </tr>
    </tbody>
  </table>
</div>

</textarea>
<button type="submit" name="download" class="btn btn-success" value="submit" >Download PDF</button>

</form>
</div>

</body>
</html>

Leave a Reply

Your email address will not be published. Required fields are marked *