How to generate PDF file using PHP ? Last Updated : 26 Jul, 2024 Comments Improve Suggest changes Like Article Like Report In this article, we will learn how to generate PDF files with PHP by using FPDF. It is a free PHP class that contains many functions for creating and modifying PDFs. The FPDF class includes many features like page formats, page headers, footers, automatic page break, line break, image support, colors, links, and many more.Approach:You need to download the FPDF class from the FPDF website and include it in your PHP script.require('fpdf/fpdf.php');Instantiate and use the FPDF class according to your need as shown in the following examples.$pdf=new FPDF();Example 1: The following example generates a PDF file with the given text in the code. The file can be downloaded or previewed as needed. PHP <?php ob_end_clean(); require('fpdf/fpdf.php'); // Instantiate and use the FPDF class $pdf = new FPDF(); //Add a new page $pdf->AddPage(); // Set the font for the text $pdf->SetFont('Arial', 'B', 18); // Prints a cell with given text $pdf->Cell(60,20,'Hello GeeksforGeeks!'); // return the generated output $pdf->Output(); ?> Output:Example 2: The following example helps in understanding the setting of the page header and footer along with printing many lines on different pages of PDF files. PHP <?php require('fpdf/fpdf.php'); class PDF extends FPDF { // Page header function Header() { // Add logo to page $this->Image('gfg1.png',10,8,33); // Set font family to Arial bold $this->SetFont('Arial','B',20); // Move to the right $this->Cell(80); // Header $this->Cell(50,10,'Heading',1,0,'C'); // Line break $this->Ln(20); } // Page footer function Footer() { // Position at 1.5 cm from bottom $this->SetY(-15); // Arial italic 8 $this->SetFont('Arial','I',8); // Page number $this->Cell(0,10,'Page ' . $this->PageNo() . '/{nb}',0,0,'C'); } } // Instantiation of FPDF class $pdf = new PDF(); // Define alias for number of pages $pdf->AliasNbPages(); $pdf->AddPage(); $pdf->SetFont('Times','',14); for($i = 1; $i <= 30; $i++) $pdf->Cell(0, 10, 'line number ' . $i, 0, 1); $pdf->Output(); ?> Output: Comment More infoAdvertise with us Next Article How to generate PDF file using PHP ? G geetanjali16 Follow Improve Article Tags : Web Technologies PHP PHP-function PHP-Questions Similar Reads How to read PDF file using PHP ? In this article, we will learn how you can show/read PDF file contents on a browser using PHP.We have to include an external PHP file named "class.pdf2text.php". Include it in the required web page using PHP. Create an HTML form, in which we can choose a PDF file from your computer and also check wh 2 min read How to generate PDF file and add TrueType fonts using PHP ? In this article, we will learn how to generate PDF files and add new TrueType fonts with PHP by using FPDF. It is a free PHP class that contains many functions for creating and modifying PDFs. The FPDF class includes many features like page formats, page headers, footers, automatic page break, line 2 min read How to generate PDF file from external text files using PHP ? In this article, we will learn how to generate PDF files from external text files with PHP by using FPDF. It is a free PHP class that contains many functions for creating and modifying PDFs. The FPDF class includes many features like page formats, page headers, footers, automatic page break, line br 5 min read How to generate an XML file dynamically using PHP? A file can be generated using PHP from the database, and it can be done by the static or dynamic method in PHP. Static methods can be called directly - without creating an instance of a class. Here we are going to discuss how to create an XML file dynamically.The first thing we need to do is fetch t 2 min read Generate PDF File Using jsPDF Library The PDF Generation Project in JavaScript aims to create a seamless tool for generating PDFs dynamically. It allows users to easily convert data into well-formatted PDF documents, enhancing productivity and user experience with quick and efficient document creation.What Will You Learn?Weâll build an 3 min read Like