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.
<?php
require_once 'tcpdf/tcpdf.php';
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->setPrintHeader(false);
$pdf->AddPage();

$html = '<h1>Welcome to <a href="https://techbriefers.com/" style="text-decoration:none;padding: 10px;"> <span style="background-color:#ef3e47;color:#fff;"> Tech</span><span style="background-color:#fff1f0;color:#000;">Briefers</span> </a>!</h1>
<i>This is the two minute example of TCPDF library by <a href="https://techbriefers.com/">techbriefers</a>.</i>
<h2>What is Lorem Ipsum?</h2>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>';

$pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);
$pdf->Output('five-min-pdf.pdf', 'I');
?>
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:

$pdf->Output('five-min-pdf.pdf', 'D'); 

How to Insert Image in PDF using PHP

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

$pdf->Image(__DIR__ . '/image-1.jpg', '', 90, 95, 50, 'JPG', false, 'left');

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.

require_once 'tcpdf/tcpdf.php';
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->setPrintHeader(false);
$pdf->AddPage();
$html = '<h1>Welcome to <a href="https://techbriefers.com/" style="text-decoration:none;padding: 10px;"> <span style="background-color:#ef3e47;color:#fff;"> Tech</span><span style="background-color:#fff1f0;color:#000;">Briefers</span> </a>!</h1>
<i>This is the five minute example of TCPDF library by <a href="https://techbriefers.com/">techbriefers</a>.</i>';
$html2 = '<h2>What is Lorem Ipsum?</h2>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages.</p>';
$pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, 'center', true);

$pdf->Image(__DIR__ . '/sq-1.jpg', '', 35, 50, 50, 'JPG', false);
$pdf->writeHTMLCell(0, 0, 65, 35, $html2, 0, 1, 0, true);

$pdf->Image(__DIR__ . '/sq-2.jpg', '', 90, 50, 50, 'JPG', false);
$pdf->writeHTMLCell(0, 0, 65, 90, $html2, 0, 1, 0, true);

$pdf->Image(__DIR__ . '/sq-3.jpg', '', 145, 50, 50, 'JPG', false);
$pdf->writeHTMLCell(0, 0, 65, 145, $html2, 0, 1, 0, true);

$pdf->Output('2-min-pdf.pdf', 'I');

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.

$pdf->SetMargins(30, 10, 10);

Below is the complete code for changing margins.

<?php
require_once 'tcpdf/tcpdf.php';
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->setPrintHeader(false);
/**  Set margins left = 30, top = 10m right = 10 **/
$pdf->SetMargins(30, 10, 10);
$pdf->AddPage();
$html = '<h1>Welcome to <a href="https://techbriefers.com/" style="text-decoration:none;padding: 10px;"> <span style="background-color:#ef3e47;color:#fff;"> Tech</span><span style="background-color:#fff1f0;color:#000;">Briefers </span> </a>!</h1>
<i>This is the five minute example of TCPDF library by <a href="https://techbriefers.com/">techbriefers</a>.</i>';
$html2 = '<h2>What is Lorem Ipsum?</h2>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. </p>';
$pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1);

$pdf->Image(__DIR__ . '/sq-1.jpg', '', 35, 50, 50, 'JPG', false);
$pdf->writeHTMLCell(0, 0, 85, 35, $html2, 0, 1, 0, true);

$pdf->Image(__DIR__ . '/sq-2.jpg', '', 90, 50, 50, 'JPG', false);
$pdf->writeHTMLCell(0, 0, 85, 90, $html2, 0, 1, 0, true);

$pdf->Image(__DIR__ . '/sq-3.jpg', '', 145, 50, 50, 'JPG', false);
$pdf->writeHTMLCell(0, 0, 85, 145, $html2, 0, 1, 0, true);

$pdf->Output('five-min-pdf.pdf', 'I');
?>

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

Create custom margin PDF in PHP - Techbriefers

Leave a Reply