How to convert HTML to PDF in PHP

How to convert HTML to PDF in PHP

This post has been viewed 250 times.

In this tutorial, we will be learning on how to convert HTML to PDF in PHP.

There are many PHP libraries around the internet nowadays but most of them just don’t work like you expected it to be. I have been required by my client to automatically generate documents in pdf format. I have tried many PHP pdf libraries and this library really makes it possible.

What you’ll need:

  • TCPDF PHP Library.
  • A text editor.
  • A local/live server.
  • Basic HTML, CSS and PHP knowledge.

Step 1: Create the HTML file.

This static HTML file will be converted to pdf. You may use all HTML tags that you know combined with CSS.

This is somehow WYSIWYG like process, where the look of the output in your HTML file will also be the look when it was converted to pdf.

In this example, I will just create a simple invoice document. We will name the file as file.html and the content as follows.

This will be the output of file.html when it is opened in a browser.

The output of file.html in the browser.

Picture 1.1. The output of file.html in the browser.

Step 2: Download the TCPDF library.

You may download the TCPDF library on the following links: link1 / link2

This library will allow us to convert the HTML file that we have created into a PDF file format.

Step 3: Create a PHP script file.

This PHP file will execute the library together with the HTML file to have a PDF output.

In this example, we will name it as index.php

The code above will require the library in our file so that we can initialize the TCPDF class in the library.

The variable $html will hold the HTML file that we have created earlier and the library will do the rest to convert it to PDF.

The last line has two (2) parameters, the first parameter will be the filename of our PDF file while the second parameter is the action that we are going to do with the file.

There are many types of action that we can choose from for the second paramter. There are I, D, F, S, FI, FD, and E.

The I action is it will send the file inline with the browser, allowing it to be viewed in the browser without actually saving the file.

The D action means the file will be forced to be downloaded on the local machine given the filename in the first parameter.

The F action means the file will be saved on the local server that can be retrieved and downloaded later.

The S action will return the document as a string where the filename is ignored.

The FI action is the combination of F and I action. It will be both viewed in the browser and saved on the local server.

The FD action is the combination of F and D action. It will be both downloaded and saved on the local server.

The E action will return the document as a base64 mime multi-part email attachment (RFC 2045). This is used when the file is going to be attached to an email.

In this example, we will choose to download the pdf file that we are going to create.

The directory will have 1 folder and 2 files.

This is how the directory must look like.

Picture 3.1. This is how the directory must look like.

If you don’t have encountered an error when executing index.php then you must have the output as follows.

The output of file.pdf opened in Google Chrome browser.

Picture 3.2. The output of file.pdf opened in Google Chrome browser.

That’s it, we have now converted an HTML file into a PDF file format document in PHP.

Hello, I am a freelance website and mobile application developer. I am also the owner of this website, so if you have any projects for me you can contact me at [email protected] or use the contact page. When I’m not doing some work, I can be found watching Anime/Asian Dramas/American TV Series, playing computer games, and writing tutorials on this website. I run the program repeatedly hoping the error won’t show up next time.