Programming PHP How to Create PDF in PHP with TCPDF

How to Create PDF in PHP with TCPDF

How to Create PDF in PHP with TCPDF - Techbriefers

In this tutorial, I will tell you, how to easily create PDF documents in your PHP applications. For creating PDF document in PHP I am using a library available – TCPDF. This library offers a bundle of benefits. With TCPDF, you can convert various HTML documents to PDF document. It also facilitates the use of PJEGs, PNGs as well as SVGs in PDF. Below I am mentioning a few benefits of using TCPDF library:

Benefits of using TCPDF library

  1. This library is easy to use and fully supports HTML, CSS and also JavaScript.
  2. Creating PDFs using this library takes only a few minutes.
  3. If you are using TCPDF, nothing other than a few PHP codes is required.
  4. It has pre-built functions for creating images, SVGs, and Graphic elements. Like squares, rectangles and many other shapes.
  5. You can also set margins and columns of your choices. This is very useful when you will need to create a PDF with a multi-column layout. For example an E-book or a PDF version of a document.
  6. Headers and footers can also be set in one go. You need to bother about such things on each page.
  7. It also facilitates setting margins and page sizes for your document.
  8. You can also put the entire layout in the form of HTML code and create PDF for that HTML.
  9. This library also supports multiple-languages. Also includes RTL support, like for Persian and Arabic languages.
  10. Other multiple benefits include – document encryption, index, and bookmarks, forms and JS support, etc.

How to create a PDF in Five minutes in PHP

Let’s create a PDF within two minutes Using PHP and TCPDF library.

Steps to create a PDF in 5 minutes are as following:

TCPDF library to create PDF in PHP techbriefers
  1. Download TCPDF Library zipped file from the Github repository.
  2. Extract all the files in the tcpdf folder ofyour project folder (htdocs/your-project-folder/ tcpdf). I have extracted in htdocs/mypdf/tcpdf.
  3. Along with library files, you get a bunch of examples in the examples folder.
  4. Now create a PHP file with name index.php file in your project folder i.e. htdocs/mypdf/index.php.
  5. Put all the code given below in that index.php file and browse for your project in the browser.
Simple PDF file created by PHP using TCPDF techbriefers

Here, you can see the HTML is printed in your PDF file. This file is not saved on your machine but is simply an inline print of the file to be shown on browser. This is stored in temporary storage. To make the PDF downloadable, the last line of code needs to be changed to:

How to Insert Image in PDF using PHP

To add Image in your PDF file, you need to use the Image() method of TCPDF.

We need to give Image() method some parameters.

  1. An image file name with the complete path.
  2. x co-ordinate as a horizontal starting point
  3. y co-ordinate as vertical starting
  4. Width of image
  5. Height of the image to be shown
  6. Type of file to be rendered (JPG, PNG, etc)
  7. Link: to link make image clickable or false otherwise.
  8. Alignment to align the image to either side. And many more which can be found with method documentation.

My personal experience with this parameter is not so good. AS x and y co-ordinate always define the starting points for both the verticals.

Using the Image() method wisely with x y co-ordinates we can create a nice image and text layout as shown below.

In the code above, I have divided the HTML string into two parts. One is for the heading in the top and the other to display in inline layout with the image.

Fine layout PDF created by PHP with TCPDF techbriefers

As you can see, the x coordinates of all the images are the same and x coordinates of all the text parts are also same. And to present this layout, I am using square-shaped images. And the Y coordinates are increased sequentially to move the sections downwards.

Create PDF with custom margins in PHP

We can also set custom margins to our document. To do this we need to use SetMargins() method. We need to pass four arguments for the left, top and right margin. The fourth parameter is $keepmargins which is false by default. If it will be set to true, then will overwrite the default page margins. In the below example I am setting the left margin to 30, Top margin to 10 and right margin to 10.

Below is the complete code for changing margins.

You can compare the previous image with the image below to compare margin differences.

Create custom margin PDF in PHP - Techbriefers

Leave a Reply