Home » How to convert html to pdf in php? (By Using TCPDF)
How to Php

How to convert html to pdf in php? (By Using TCPDF)

In this tutorial, I will show you how to convert HTML to pdf in PHP or how to transfer HTML to pdf using PHP by following simple steps. No need to be an expert in PHP or HTML but a little bit PHP and HTML knowledge is required. I am explaining to you in very basic language. Please follow the instructions.

There are too many scripts are available on the internet to convert pdf to HTML in PHP. I have tried most of them and find the best one which is “TCPDF”.

convert HTML to pdf in PHP

Why TCPDF is the best?

It is free and many functions are available in this script. Also, functions are very easy to understand and implement. TCPDF is user-friendly and coding is very easy to understand. The main advantage of this script is a page break function. When your pdf pages are more than one then it works very charmingly. It will arrange all the designs of your HTML in a very good manner. Rather other scripts can’t do it very well. Another advantage is the HTML table structure(tr-td). This script makes it very simple and attractive.

When I tried other scripts there are too many issues occurs like:

  • When PDF goes to the next page then the design is messing up.
  • Can’t fix proper table structure.
  • Margin issue.
  • etc…

Now I will show you how to set up all the things for transfer HTML to pdf using PHP and HTML.

Follow the steps for convert HTML to PDF in PHP

Step 1: Download “TCPDF” script

First of all, you have to download the TCPDF library because we need this library to execute the code. You can download the library from the click below button.

Download Script

Step 2: Unzip and Upload “TCPDF” folder to your server

Then you have to unzip the downloaded file, You will see one folder name “TCPDF”. Now upload this folder in your root directory of the server using FTP (Filezilla). If you are work in localhost then you can copy the folder and paste it in this path (xampp -> htdocs -> project name -> paste it)

Step 3: Now create “index.php” file and put the below code in this file.

$pdf = '
// You can put your HTML code here
< h1 > Lorem Ipsum... </ h1 >
< h2 > Lorem Ipsum... </ h2 >
< h3 > Lorem Ipsum... </ h3 >
< h4 > Lorem Ipsum... </ h4 >
';

require('TCPDF/tcpdf.php');
$tcpdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

// set default monospaced font
$tcpdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

// set title of pdf
$tcpdf->SetTitle('Bill Collection Letter');

// set margins
$tcpdf->SetMargins(10, 10, 10, 10);
$tcpdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$tcpdf->SetFooterMargin(PDF_MARGIN_FOOTER);

// set header and footer in pdf
$tcpdf->setPrintHeader(false);
$tcpdf->setPrintFooter(false);
$tcpdf->setListIndentWidth(3);

// set auto page breaks
$tcpdf->SetAutoPageBreak(TRUE, 11);

// set image scale factor
$tcpdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

$tcpdf->AddPage();

$tcpdf->SetFont('times', '', 10.5);

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

//Close and output PDF document
$tcpdf->Output('demo.pdf', 'I');

For set page title: $tcpdf->SetTitle(‘Bill Collection Letter’);
You can set the margins of your pdf page by changing this: $tcpdf->SetMargins(10, 10, 10, 10);

By following these steps you can easily convert HTML to pdf in PHP. That’s it, now you can execute the code and you will see your HTML code is converted into pdf. You can get more examples, functions, and demos on TCPDF official site.

I hope you enjoyed this tutorial, please comment below and share it with your friends.

Releated Post: