Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to generate PDF file using PHP ?

  • Last Updated : 02 Jul, 2021

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.


  • 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.


// Instantiate and use the FPDF class 
$pdf = new FPDF();
//Add a new page
// 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



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.


class PDF extends FPDF {
    // Page header
    function Header() {
        // Add logo to page
        // Set font family to Arial bold 
        // Move to the right
        // Header
        // Line break
    // Page footer
    function Footer() {
        // Position at 1.5 cm from bottom
        // Arial italic 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
for($i = 1; $i <= 30; $i++)
    $pdf->Cell(0, 10, 'line number ' 
            . $i, 0, 1);


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!