All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Generate PDF In PHP Dynamically

Last Updated : Mar 11, 2024

How To Generate PDF In PHP Dynamically

In this tutorial we will show you the solution of how to generate PDF in PHP dynamically, when compared to HTML structured emails, the text format is best suited for sending emails because the chances of the email contents becoming corrupted during the transition are significantly lower.

For printing a tangible copy, a PDF or Word document is the best option, whereas HTML is the best option for displaying material in web browsers.

When it comes to downloading documents such as bills or invoices, PDF is the way to go.

When creating a backend software, like a CRM or an invoicing system, you'll need to produce documents dynamically on the run and send them as PDFs. Email attachments or downloads are also acceptable options.

In this tutorial, we'll look at how to convert HTML to PDF using PHP.

The core concept of how to convert an HTML file to a PDF file will not be explained in this post.

Step By Step Guide On How To Generate PDF In PHP Dynamically :-

Method 1 - FPDF: The PDF Generator

FPDF library is the initial and most important foundation for this file conversion. FPDF is a PHP class that allows you to create PDF files on the fly. Let's begin the PDF creation process with a basic Hello world message.

<?php
require('fpdf.php');
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output();
?>
  1. To create a pdf file, we must first include the fpdf.php library file. Then, using the default function Object() { [native code] } FPDF, we must create an FPDF object ().
  2. There are three parameters that can be supplied to this function Object() { [native code] }: page orientation (portrait or landscape), measure unit, and page size (A4, A5, etc.,).
  3. Pages are set to A4 portrait by default, with millimetres as the measuring unit. It may have been stated explicitly as follows: $pdf=new FPDF('P','mm','A4');
  4. Landscape (L), alternative page layouts (such as Letter and Legal), and measurement units are all options (pt, cm, in).
  5. Then we used AddPage() to add a page to our pdf document. The origin is in the upper-left corner, and the current position is set at 1 cm from the borders by default; the margins can be modified using the SetMargins function ().
  6. To print a text, we must first use SetFont to choose a font (). Let's use Arial bold 16 as an example:
  7. $pdf->SetFont('Arial','B',16);
  8. To print text, we use the Cell() function. A cell is a rectangular section with some text that is possibly framed. It's displayed in the current position.
  9. We define its dimensions, the text (centred or aligned), whether or not boundaries should be drawn, and where the current position will move after it (to the right, below, or to the beginning of the next line).
  10. To add a frame, we'd perform the following: $pdf->Cell(40,10,'Hello World !',1);
  11. Finally, using Output, the document is closed and sent to the browser. We may have stored it in a file with a different name than the one we wanted.

Method 2 - HTML2FPDF: The Converter

HTML2FPDF is a PHP class library that converts HTML files to PDF files using the FPDF class library.

There are three classes in this library: PDF, HTML2FPDF, and FPDF (modified FPDF class). The class PDF is an extension of the class HTML2FPDF, which is an extension of the class FPDF.

<?
require('html2fpdf.php');
$pdf=new HTML2FPDF();
$pdf->AddPage();
$fp = fopen("sample.html","r");
$strContent = fread($fp, filesize("sample.html"));
fclose($fp);
$pdf->WriteHTML($strContent);
$pdf->Output("sample.pdf");
echo "PDF file is generated successfully!";
?>
  1. Let's look at how to use the HTML2FPDF Library to convert a sample HTML page into a PDF file.
  2. A table on the HTML page lists a few nations and their corresponding national flags. The conversion code can be found below.
  3. First, we must include the html2fpdf.php file, which contains the HTML2FPDF class, and then we must use the function Object() { [native code] } HTML2FPDF to create an object ().
  4. The function AddPage is then used to add a new page to the pdf document (). The contents of the sample.html file are read using file functions.
  5. The HTML material is then converted to pdf using the WriteHTML() function.

Conclusion :-

Converting data from one file format to another is a common occurrence. Some people will need to convert text to HTML, while others will need to convert HTML material to an image format.

With XHTML 1.0, the HTML2FPDF and FPDF class library will operate best. I hope this tutorial on how to generate PDF in PHP dynamically helps you.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪