Generate PDF from MySQL Data using PHP.

In this tutorial I explains how to generate PDF from MySQL Data using PHP. You are going to see how to convert MySQL Data into PDF using FPDF library.
Before start learning first of all we should know about Fpdf.

What is Fpdf.?

The FPDF is awesome PHP class to generate PDF using PHP from MySQL/MySqli database.This is open source php library to generate pdf file using PHP.
Awesome Features of FPDF.

  • Choice of measure unit, page format and margins.
  • Page header and footer management.
  • Automatic page and line break with text justification
  • Image support (JPEG, PNG and GIF).
  • Colors
  • Links
  • TrueType, Type1 and encoding support
  • Page compression.

 Make PDF Invoice using PHP FPDF library

In this tutorial we will see how to download Mysql data into pdf format and make a dynamic invoice with the help of Fpdf Library.
In the first step  you need to download Fpdf library.
after downloading unzip the package into your htdocs directory.

lets make a pdf file as a pdf generator.
in this example , i am going to portrait, A4 size paper with mm measurement unit.
here i am making three pages.

Also Read :
How to Install PHP on CentOS.
How to integrate Razorpay Payment Gateway using PHP.

PHP Login Script With Remember me.
Change password using javascript, php and mysqli.
Password and Confirm Password Validation Using JavaScript
Check Email is Already Registered in Database using Ajax and JavaScript.
How to hide extension of html and php file.?

1. connection.php
2. index.php
3. invoice.php
In the second step ,we make our database connecton in connection.php file

<?php
$con = mysqli_connect("localhost","username","password","test");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
?>

In the third step we make a index.php file, where we choose invoice number and generate invoice of that number.

<?php 
// db connectionn
require("connection.php");
?>
<html>
<head>
<title>Shortlearner</title>
</head>
<body>
select invoice :
<form method="get" action= "invoice.php">
<select name="id">
<?php
$qry= mysqli_query($con,"SELECT * FROM invoice");
while($invoice=mysqli_fetch_array($qry))
{
echo "<option value='".$invoice['id']."'>".$invoice['id']."</option>";
}
?>
</select>
<input type="submit" value="Generate">
</form>
</body>
</html>

In Last step we make a pdf generator php file as invoice.php where we generate invoice of selected invoice number.

<?php
//call the FPDF library
require('fpdf17/fpdf.php');
require("connection.php");
$id= $_GET['id'];
$qry="SELECT * FROM invoice WHERE id=$id";
$result= mysqli_query($con,$qry);
$invoice=mysqli_fetch_array($result);
//A4 width : 219mm
//default margin : 10mm each side
//writable horizontal : 219-(10*2)=189mm
//create pdf object
$pdf = new FPDF('P','mm','A4');
//add new page
$pdf->AddPage();
//set font to arial, bold, 14pt
$pdf->SetFont('Arial','B',14);
//Cell(width , height , text , border , end line , [align] )
$pdf->Cell(130 ,5,'',0,0);
$pdf->Cell(59 ,5,'INVOICE',0,1);//end of line
//set font to arial, regular, 12pt
$pdf->SetFont('Arial','',12);
$pdf->Image('img/MM.png',0,0,0);// logo of your company
$pdf->Cell(59 ,5,'',0,1);//end of line
$pdf->Cell(130 ,5,'',0,0);
$pdf->Cell(25 ,5,'Status',0,0);
$pdf->Cell(34 ,5,$invoice['status'],0,1);//end of line
$pdf->Cell(130 ,5,'',0,0);
$pdf->Cell(25 ,5,'Date',0,0);
$pdf->Cell(34 ,5,$invoice['date'],0,1);//end of line
$pdf->Cell(130 ,5,'',0,0);
$pdf->Cell(25 ,5,'Invoice #',0,0);
$pdf->Cell(34 ,5,$invoice['id'],0,1);//end of line
$pdf->Cell(130 ,5,'',0,0);
$pdf->Cell(25 ,5,'Customer ID ',0,0);
$pdf->Cell(34 ,5,$invoice['uid'],0,1);//end of line
//make a dummy empty cell as a vertical spacer
$pdf->Cell(89 ,15,'',0,1);//end of line
//billing address
$pdf->Cell(100 ,5,'Bill to',0,1);//end of line
//add dummy cell at beginning of each line for indentation
$pdf->Cell(10 ,5,'',0,0);
$pdf->Cell(90 ,5,$invoice['fname'].$invoice['lname'],0,1);
$pdf->Cell(10 ,5,'',0,0);
$pdf->Cell(90 ,5,$invoice['cname'],0,1);
$pdf->Cell(10 ,5,'',0,0);
$pdf->Cell(90 ,5,$invoice['address'],0,1);
$pdf->Cell(10 ,5,'',0,0);
$pdf->Cell(90 ,5,$invoice['city'],0,1);
$pdf->Cell(10 ,5,'',0,0);
$pdf->Cell(90 ,5,$invoice['pincode'],0,1);
$pdf->Cell(10 ,5,'',0,0);
$pdf->Cell(90 ,5,$invoice['state'],0,1);
$pdf->Cell(10 ,5,'',0,0);
$pdf->Cell(90 ,5,$invoice['country'],0,1);
//make a dummy empty cell as a vertical spacer
$pdf->Cell(189 ,10,'',0,1);//end of line
//invoice contents
$pdf->SetFont('Arial','B',12);
$pdf->Cell(130 ,5,'Description',1,0);
$pdf->Cell(25 ,5,'Taxable',1,0);
$pdf->Cell(34 ,5,'Amount',1,1);//end of line
$pdf->SetFont('Arial','',12);
//Numbers are right-aligned so we give 'R' after new line parameter
$pdf->Cell(130 ,5,$invoice['name'].$inovice['os'],1,0);
$pdf->Cell(25 ,5,'-',1,0);
$pdf->Cell(34 ,5,$invoice['grand_total'],1,1,'R');//end of line
//summary
$pdf->Cell(130 ,5,'',0,0);
$pdf->Cell(25 ,5,'Subtotal',0,0);
$pdf->Cell(4 ,5,'
,1,0);
$pdf->Cell(30 ,5,$invoice['grand_total'],1,1,'R');//end of line
//output the result
$pdf->Output();
?>