How to convert HTML to PDF using TCPDF in PHP

Create HTML to PDF using TCpdf PHP

TCPDF is an open source library to generate pdf files. Go to TCPDF Official website and check all of the available features and examples.

In this article i will explain how to generate pdf files from html in PHP using the TCPDF library. Download the ZIP file and extract into your project directory.


Configuration file located in tcpdf/configtcpdf_config.php. You can change the general configuration of the TCPDF library.

// Path for PDF fonts.
define ('K_PATH_FONTS', K_PATH_MAIN.'fonts/');
// Default images directory for example used for logos
define ('K_PATH_IMAGES', K_PATH_MAIN.'../images/');
// Default image logo used be the default Header() method.
define ('PDF_HEADER_LOGO', 'logo.png');
// Header logo image width in user units.
define ('PDF_HEADER_LOGO_WIDTH', 20);

First include TCPDF library into your PHP File.


Custom Header and Footer:

You can create your custom Header and Footer in PDF file.


// set document information
// set default header data
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
$pdf->SetHeaderData('media/google_play.png', PDF_HEADER_LOGO_WIDTH, '', '', array(0,0,0), array(255, 255, 255));

// set default footer data

$pdf->setFooterData(array(0,64,0), array(0,64,128));

$pdf->SetMargins(20, 0, 20, true);

Adding new pages:

Adds a new page to the PDF. P or PORTRAIT (default), L or LANDSCAPE.


Html & CSS:

TCPDF support CSS code to the HTML text then it will transform this to the corresponding PDF.

$html = <<<EOF
  table, th, td {
  border: 1px solid black;
    td {
        border: 2px solid blue;
        background-color: #ffffee;
    td.second {
        border: 2px dashed green;
    html, body{
	font-family: 'PT Sans', sans-serif;
    font-size: 100%;
  	overflow-x: hidden;
	background: #fff;
body a{
	transition: 0.5s all ease;
	-webkit-transition: 0.5s all ease;
	-moz-transition: 0.5s all ease;
	-o-transition: 0.5s all ease;
	-ms-transition: 0.5s all ease;

<table class="first" cellpadding="4" cellspacing="6">
    <th>User Name</th>
    <td align="right">Jhon</td>
    <td align="right">India</td>
    <td align="right">123456789</td>
    <td align="right">Doe</td>
    <td align="right">USA</td>
    <td align="right">1234567890</td>
<div class="test">example To add a border to a table
<br />And for more information on sample text
<br /><span> There is no one who loves pain itself </span>
<br /><span>  Lorem ipsum dolor sit .</span>

$pdf->writeHTML($html, true, false, true, false, ''); 

//Close and output PDF document
$pdf->Output('example.pdf', 'D');

Parameters of Output() method

  1. @param string: name:- The name of the file when saved
  2. @param string: dest:- Destination where to send the document. It can take one of the following values:
    • I:- send the file inline to the browser (default).
    • D:- send to the browser and force a file download.
    • F:- It allows the generated pdf to be saved as a local file.
    • S:- return the document as a string.
    • FI:- equivalent to F + I option.
    • FD:- equivalent to F + D option.

Leave a Reply

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