There are many ways to create a PDF using PHP. And one of my favorite way to create PDF is by using FPDF library. So let’s see how we can create a PDF using the FPDF library. For the example of PDF, here I am creating a payment invoice slip.
Note. You should have some basic knowledge of PHP and how to execute a PHP file which is available on your htdocs folder. If you want to learn PHP as a beginner then watch this playlist on YouTube.
Step 1
Now first, go to the FPDF website and download the latest version of FPDF library.
Step 2
Extract FPDF library inside an htdocs folder.
Step 3
Then is the same folder, create an index.php file. After that inside an index.php file, you can paste a source code which is given below.
Source Code for PDF Invoice
<?php require('fpdf181/fpdf.php'); $pdf = new FPDF(); $pdf->AddPage(); $pdf->SetFont('Arial', '', 12); $pdf->Cell(55, 5, 'Reference Code', 0, 0); $pdf->Cell(58, 5, ': 026ETY', 0, 0); $pdf->Cell(25, 5, 'Date', 0, 0); $pdf->Cell(52, 5, ': 2018-12-24 11:47:10 AM', 0, 1); $pdf->Cell(55, 5, 'Amount', 0, 0); $pdf->Cell(58, 5, ': 2674', 0, 0); $pdf->Cell(25, 5, 'Channel', 0, 0); $pdf->Cell(52, 5, ': WEB', 0, 1); $pdf->Cell(55, 5, 'Status', 0, 0); $pdf->Cell(58, 5, ': Complete', 0, 1); $pdf->Line(10, 30, 200, 30); $pdf->Ln(10); $pdf->Cell(55, 5, 'Product Id', 0, 0); $pdf->Cell(58, 5, ': 64351-84503', 0, 1); $pdf->Cell(55, 5, 'Tax Amount', 0, 0); $pdf->Cell(58, 5, ': 0', 0, 1); $pdf->Cell(55, 5, 'Product Service Charge', 0, 0); $pdf->Cell(58, 5, ': 0', 0, 1); $pdf->Cell(55, 5, 'Product Delivery Charge', 0, 0); $pdf->Cell(58, 5, ': 0', 0, 1); $pdf->Line(10, 60, 200, 60); $pdf->Ln(10);//Line break $pdf->Cell(55, 5, 'Paid by', 0, 0); $pdf->Cell(58, 5, ': Nawaraj Shah', 0, 1); $pdf->Line(155, 75, 195, 75); $pdf->Ln(5);//Line break $pdf->Cell(140, 5, '', 0, 0); $pdf->Cell(50, 5, ': Signature', 0, 1, 'C'); $pdf->Output(); ?>
Output
Now let me explain what each code does so that you can create your own PDF as your need.
First, you have to include your FPDF library in your PHP file so that we write require('fpdf181/fpdf.php');
Then we write $pdf = new FPDF();
to create an instance of FPDF object and assign it on $pdfvariable
.
By default, there is no PDF page. So we create our new page by writing $pdf->AddPage();
Then we define font family, font decoration and font size by writing SetFont(string family , string style , float size);
eg. $pdf->SetFont('Arial', '', 12);
After you we add a page, now we are ready to write something in our PDF. to write something in our PDF we use Cell()method
. This cell method takes 8 parameters in this format Cell(float w , float h , string txt , mixed border , int ln , string align , boolean fill , mixed link)
Where
w: Cell width. If 0
, the cell extends up to the right margin.
h: Cell height. Default value: 0
.
txt: String to print. Default value: empty string.
border: Indicates if borders must be drawn around the cell. The value can be either a number:
0
: no border1
: frame
or a string containing some or all of the following characters (in any order):
L
: leftT
: topR
: rightB
: bottom
Default value:0
ln: Indicates where the current position should go after the call. Possible values are:
0
: to the right1
: to the beginning of the next line2
: below
align: Allows to center or align the text. Possible values are:
L
or empty string: left align (default value)C
: centerR
: right align
fill: Indicates if the cell background must be painted (true
) or transparent (false
). Default value: false
.
link: URL or identifier returned by AddLink().
And this is how you create PDF Invoice in Php using FPDF library.
Also learn how to create a calculator using Javascript, HTML, and CSS.
Or you can also watch this video if you found all this staff very complicated.