php

How to Create a PDF in PHP

PDF or Portable Document Format is a file document widely used in the modern world. Developed by Adobe, PDF files allow users to add text, images, and other supported file types into a compressed format readable by a PDF reader program.

This guide will teach you how to use the PHP programming language to create a PDF file.

What to use?

In this article, we will use a third-party library to generate PDF files using PHP. FPDF is a free library that allows you to quickly and efficiently create PDF files using PHP. The library contains loads of options and features to work with PDF, such as adding headers, footers, page breaks, image support, colors, and many more.

Installation

Before proceeding on how to use the FPDF library, let us set up our development environment.

Start by installing PHP as shown in the command below:

sudo apt-get update
sudo apt-get install php7.4

Next, create a directory to store the working files:

mkdir pdf-create
cd pdf-create

Open your browser and navigate to the FPDF downloads page.

http://www.fpdf.org/en/download.php

Select the latest version and download the compressed package to the pdf-create directory we created earlier.

Extract the file as:

tar xvf fpdf184.tgz

Next, move the fpdf.php file from the extracted directory to the current working directory:

sudo mv fpdf184/fpdf.php .

Once the file is created, we can create the PDF files using the FPDF package.

Create Sample PDF From Text

To create a PDF file with the specified text, start creating a file as create.php and open it with your text editor.

Add the code as shown below:

<?php
    require ("fpdf.php");
    $pdf = new FPDF();
    $pdf->AddPage();
    $pdf->SetFont("Arial", "I", 12);
    $pdf->Cell(40, 10, "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolo);
    $pdf->Output();
?>

The previous code is very straightforward. We start by including the library file (fpdf.php). Next, we create an FPDF object with the default constructor, such as A4 pages and millimeters as the unit of measure.

Keep in mind; you can define other parameters, such as the layout to landscape, size, and unit of measure.

In the following line, we use the AddPage() method to add a new page. The page’s origin is at the upper-left corner, and the current position is set to 1 cm from the borders.

Using the SetFont() method, we define the font parameters, such as the Font Family, and the format, such as Bold, Italics, and Underline. We also set the font size to 12.

The next to the last line defines the cell to print the provided text. We use a default cell value which creates a line of text.

Finally, we close the document and output to the browser using the Output() function.

Run the code above as:

php create.php > demo.pdf

Once you open the PDF file, you should see the output as shown:

Create PDF With a Header and Footer

The FPDF library also allows you to create a custom header and footer for your PDF files. The following code illustrates creating a PDF file with a header and footer.

<?php
    require ("fpdf.php");
    class PDF extends FPDF{
        function Header()
        {
            $this->SetFont("Arial", "B", 24);
            $this->Cell(80);
            $this->Cell(40, 10, "Heading");
            $this->Ln(25);
        }
        function Footer()
        {
            $this->SetY(-15);
            $this->SetFont("Arial", "U", 10);
            $this->Cell(0,10 "Page: ".$this->PageNo() . "/{nb}",0,0,"C");
        }
    }
    $pdf = new FPDF();
    $pdf->AliasNbPages();
    $pdf->AddPage();
    $pdf->SetFont("Arial", "I", 12);
    for($i=1;$iCell(0,10, "Line number ".$i,0,1)
    }
    $pdf->Output();
?>

The above example code generates a PDF file with a header and footer as specified. Check the primary documentation to learn more about the FPDF library and supported functions.

Conclusion

In this guide, we discussed how to work with the FPDF library to create and generate PDF documents using PHP. We hope you found this article helpful. Check out Linux Hint for more tips and information.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list