How to Create PDF Reports with FPDF in PHP

Good morning blogger friend this time I will share how to make PDF reports with FPDF in PHP. This is very necessary when we process a large website and always need printed data. But before that we want to explain what FPDF is.



According to the official FPDF website "FPDF is a PHP class that allows to produce PDF files with pure PHP, meaning without using the PDFlib library. F from FPDF stands for Free" and it means you can use it for all types of uses and modify according to your needs.

FPDF has other advantages: high level functions. Following is a list of its main features:

  1. Choice of unit size, page format and margin
  2. Management of headers and footers
  3. Automatic page breaks
  4. Automatic line separation and text justification
  5. Image support (JPEG, PNG and GIF)
  6. Color
  7. Link
  8. TrueType, Type1 and encoding support
  9. Page Compression

FPDF does not require an extension (except Zlib to enable compression and GD for GIF support).

But the first thing you do is download and install FPDF

You can download the FPDF file from the official site they have at http://www.fpdf.org/ Extract the file that you downloaded earlier and save it in your htdocs folder. Like an explanation on their official website, FDPF is very simple in its use, ie you don't have to install it, you just call it with the require command.

I took this tutorial from one of my web projects and I think sharing it is something that is very useful. Following are the stages.

Creating a Database For the name, it's up to you, later you can adjust it when you call the database later. I just give the example according to my project. For the table here I give the name of the database named cms or your name and the table name tblcomplaint.

For complaint table enter the following query.

 CREATE TABLE IF NOT EXISTS `tblcomplaints` (
  `complaintNumber` int(11) NOT NULL,
  `userId` int(11) NOT NULL,
  `category` int(11) NOT NULL,
  `subcategory` varchar(255) NOT NULL,
  `complaintType` varchar(255) NOT NULL,
  `noc` varchar(255) NOT NULL,
  `complaintDetails` mediumtext NOT NULL,
  `regDate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `status` varchar(50) DEFAULT NULL,
  `lastUpdationDate` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
 

The table above has many queries but it is up to you to use all or only half of your needs, Namin I suggest that nothing be removed if only for learning and experimental materials.

Then you enter the contents of the thickness above, the following you must enter.

  

INSERT INTO `tblcomplaints` (`complaintNumber`, `userId`, `category`, `subcategory`, `complaintType`, `noc`, `complaintDetails`, `regDate`, `status`, `lastUpdationDate`) VALUES
(2, 1, 2, 'Compuetr Lab', 'General Query', 'Damage', 'Proyektornya tidak nyala Pak', '2018-03-06 12:34:18', 'closed', '2018-03-07 12:01:39'),
(3, 1, 4, '302', ' Complaint', 'Repairs', 'Acnya nyala tapi tidak dingin', '2018-03-06 12:35:05', NULL, '0000-00-00 00:00:00'),
(4, 1, 3, 'Select Room Available', ' Complaint', 'Damage', 'Platformnya bocor', '2018-03-06 12:35:28', NULL, '0000-00-00 00:00:00'),
(5, 1, 2, 'Aquarium Room', ' Complaint', 'Damage', 'Acnya rusak', '2018-03-06 12:35:55', NULL, '0000-00-00 00:00:00'),
(6, 2, 1, 'Library ', 'General Query', 'Repairs', 'Proyektornya tidak nyala pak', '2018-03-06 12:37:53', NULL, '0000-00-00 00:00:00'),
(7, 2, 3, '202', ' Complaint', 'Damage', 'Tidak bisa nyala', '2018-03-06 12:39:45', NULL, '0000-00-00 00:00:00'),
(8, 2, 3, '202', ' Complaint', 'Damage', 'Rusak Parah Pak', '2018-03-06 12:40:07', NULL, '0000-00-00 00:00:00'),
(9, 3, 3, '202', ' Complaint', 'Repairs', 'Bocor Bocor', '2018-03-06 12:41:21', NULL, '0000-00-00 00:00:00'),
(10, 3, 3, '202', 'General Query', 'Damage', 'Tidak Nyala Pak', '2018-03-06 12:42:12', NULL, '0000-00-00 00:00:00'),
(11, 4, 2, 'Compuetr Lab', ' Complaint', 'Damage', 'Mantap', '2018-03-06 12:47:01', NULL, '0000-00-00 00:00:00'),
(12, 4, 3, '203', ' Complaint', 'Damage', 'ngga panas', '2018-03-06 12:50:10', NULL, '0000-00-00 00:00:00'),
(13, 9, 3, '202', ' Complaint', 'Repairs', 'Panas Pak', '2018-03-06 12:55:54', NULL, '0000-00-00 00:00:00'),
(14, 9, 4, '302', 'General Query', 'Damage', 'Panas Pak Ya', '2018-03-06 12:56:18', NULL, '0000-00-00 00:00:00'),
(15, 6, 2, 'Compuetr Lab', ' Complaint', 'Damage', 'Roboh Pak', '2018-03-06 17:07:46', NULL, '0000-00-00 00:00:00'),
(16, 6, 1, 'Architecture Room', 'General Query', 'Repairs', 'Ngga bisa nyala Pak', '2018-03-06 17:08:19', NULL, '0000-00-00 00:00:00'),
(17, 6, 5, '402', ' Complaint', 'Damage', 'Panas Banget Pak', '2018-03-06 17:08:53', NULL, '0000-00-00 00:00:00'),
(18, 7, 2, 'Aquarium Room', ' Complaint', 'Repairs', 'Roboh Pak', '2018-03-06 17:10:37', NULL, '0000-00-00 00:00:00'),
(19, 7, 3, '202', ' Complaint', 'Repairs', 'Roboh Pak', '2018-03-06 17:10:53', NULL, '0000-00-00 00:00:00'),
(20, 7, 2, 'Compuetr Lab', 'General Query', 'Repairs', 'Nda bisa menyala Pak', '2018-03-06 17:11:47', NULL, '0000-00-00 00:00:00'),
(21, 7, 3, '202', 'General Query', 'Damage', 'Rusak Parah Pak', '2018-03-06 17:13:18', NULL, '0000-00-00 00:00:00'),
(22, 7, 2, 'Compuetr Lab', 'General Query', 'Damage', 'Tidak bisa nyala Pak', '2018-03-07 10:24:19', 'closed', '2018-03-07 12:03:46'),
(23, 7, 2, 'Aquarium Room', 'General Query', 'Repairs', 'Acnya tidak bisa nyala pak', '2018-03-07 10:24:48', NULL, '0000-00-00 00:00:00'),
(24, 10, 3, '202', ' Complaint', 'Damage', 'Nda Panas Pak', '2018-03-07 10:38:10', NULL, '0000-00-00 00:00:00'),
(25, 10, 2, 'Aquarium Room', ' Complaint', 'Damage', 'Roboh', '2018-03-07 10:38:26', NULL, '0000-00-00 00:00:00'),
(26, 10, 5, '402', ' Complaint', 'Damage', 'Nda bisa menyala', '2018-03-07 10:38:40', NULL, '0000-00-00 00:00:00'),
(27, 10, 5, '402', 'General Query', 'Repairs', 'Tidak Menyala', '2018-03-07 10:39:15', NULL, '0000-00-00 00:00:00'),
(28, 9, 3, '203', ' Complaint', 'Repairs', 'Acnya nda bisa menyala', '2018-03-07 10:40:09', NULL, '0000-00-00 00:00:00'),
(29, 9, 4, '302', ' Complaint', 'Repairs', 'Rusak Parah Pak', '2018-03-07 10:40:24', NULL, '0000-00-00 00:00:00'),
(30, 9, 4, '304', 'General Query', 'Damage', 'Tidak bisa menyala', '2018-03-07 10:40:41', NULL, '0000-00-00 00:00:00'),
(31, 9, 4, '302', 'General Query', 'Damage', 'tidak bisa nyala', '2018-03-07 10:41:01', NULL, '0000-00-00 00:00:00'),
(32, 5, 2, 'Compuetr Lab', ' Complaint', 'Damage', 'Panas Bangat Pak', '2018-03-07 10:41:44', NULL, '0000-00-00 00:00:00'),
(33, 5, 3, '204', ' Complaint', 'Repairs', 'Rusak Parah', '2018-03-07 10:41:58', NULL, '0000-00-00 00:00:00'),
(34, 5, 1, 'Library ', ' Complaint', 'Damage', 'Panas Banget Pak', '2018-03-07 10:42:14', NULL, '0000-00-00 00:00:00'),
(35, 5, 4, '302', 'General Query', 'Repairs', 'Tidak bisa nyala', '2018-03-07 10:42:37', NULL, '0000-00-00 00:00:00');
 

Next you create a file named report_filter.php and enter the code below.

  

<?php
//koneksi ke database
define('DB_SERVER','localhost');
define('DB_USER','root');
define('DB_PASS' ,'');
define('DB_NAME', 'cms');
$con = mysqli_connect(DB_SERVER,DB_USER,DB_PASS,DB_NAME);
// Check connection
if (mysqli_connect_errno())
{
 echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//Memanggil file FPDF dari file yang anda download tadi
require('../fpdf/fpdf.php');

$pdf = new FPDF("L","cm","A4");

$pdf->SetMargins(2,1,1);
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Times','B',11);
$pdf->ln(1);
$pdf->SetFont('Arial','B',14);
$pdf->Cell(25.5,0.7,"Building Maintenance Management",0,10,'C');
$pdf->ln(1);
$pdf->SetFont('Arial','B',10);
$pdf->Cell(5,0.7,"Printed On : ".date("D-d/m/Y"),0,0,'C');
$pdf->ln(1);
$pdf->SetFont('Arial','B',10);
//Tidak berpengaruh dengan database hanya sebagai keterangan pada tabel nantinya
$pdf->Cell(1, 0.8, 'NO', 1, 0, 'C');
$pdf->Cell(4.5, 0.8, 'Room', 1, 0, 'C');
$pdf->Cell(9, 0.8, 'Complaints Details', 1, 0, 'C');
$pdf->Cell(4.5, 0.8, 'Regist Date', 1, 0, 'C');
$pdf->Cell(4.5, 0.8, 'Update Date', 1, 0, 'C');
$pdf->Cell(2, 0.8, 'status', 1, 1, 'C');
$pdf->SetFont('Arial','',10);
$no=1;
//Panggil tblcomplaints dari database cms
$query=mysqli_query($con,"select * from tblcomplaints");
while($lihat=mysqli_fetch_array($query)){
//Queri tabel yang ingin ditampilkan
 $pdf->Cell(1, 0.8, $no, 1, 0, 'C');
 $pdf->Cell(4.5, 0.8, $lihat['subcategory'], 1, 0,'C');
 $pdf->Cell(9, 0.8, $lihat['complaintDetails'], 1, 0,'C');
 $pdf->Cell(4.5, 0.8, $lihat['regDate'],1, 0, 'C');
 $pdf->Cell(4.5, 0.8, $lihat['lastUpdationDate'],1, 0, 'C');
 $pdf->Cell(2, 0.8, $lihat['status'], 1, 1,'C');

 $no++;
}
$pdf->ln(1);
$pdf->SetFont('Arial','B',11);
$pdf->Cell(40.5,0.7,"Approve",0,10,'C');

$pdf->ln(1);
$pdf->SetFont('Arial','B',9);
$pdf->Cell(40.5,0.7,"Technician Name",0,10,'C');
//Nama file ketika di print
$pdf->Output("laporan_buku.pdf","I");

<?php
//koneksi ke database
define('DB_SERVER','localhost');
define('DB_USER','root');
define('DB_PASS' ,'');
define('DB_NAME', 'cms');
$con = mysqli_connect(DB_SERVER,DB_USER,DB_PASS,DB_NAME);
// Check connection
if (mysqli_connect_errno())
{
 echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//Memanggil file FPDF dari file yang anda download tadi
require('../fpdf/fpdf.php');

$pdf = new FPDF("L","cm","A4");

$pdf->SetMargins(2,1,1);
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Times','B',11);
$pdf->ln(1);
$pdf->SetFont('Arial','B',14);
$pdf->Cell(25.5,0.7,"Building Maintenance Management",0,10,'C');
$pdf->ln(1);
$pdf->SetFont('Arial','B',10);
$pdf->Cell(5,0.7,"Printed On : ".date("D-d/m/Y"),0,0,'C');
$pdf->ln(1);
$pdf->SetFont('Arial','B',10);
//Tidak berpengaruh dengan database hanya sebagai keterangan pada tabel nantinya
$pdf->Cell(1, 0.8, 'NO', 1, 0, 'C');
$pdf->Cell(4.5, 0.8, 'Room', 1, 0, 'C');
$pdf->Cell(9, 0.8, 'Complaints Details', 1, 0, 'C');
$pdf->Cell(4.5, 0.8, 'Regist Date', 1, 0, 'C');
$pdf->Cell(4.5, 0.8, 'Update Date', 1, 0, 'C');
$pdf->Cell(2, 0.8, 'status', 1, 1, 'C');
$pdf->SetFont('Arial','',10);
$no=1;
//Panggil tblcomplaints dari database cms
$query=mysqli_query($con,"select * from tblcomplaints");
while($lihat=mysqli_fetch_array($query)){
//Queri tabel yang ingin ditampilkan
 $pdf->Cell(1, 0.8, $no, 1, 0, 'C');
 $pdf->Cell(4.5, 0.8, $lihat['subcategory'], 1, 0,'C');
 $pdf->Cell(9, 0.8, $lihat['complaintDetails'], 1, 0,'C');
 $pdf->Cell(4.5, 0.8, $lihat['regDate'],1, 0, 'C');
 $pdf->Cell(4.5, 0.8, $lihat['lastUpdationDate'],1, 0, 'C');
 $pdf->Cell(2, 0.8, $lihat['status'], 1, 1,'C');

 $no++;
}
$pdf->ln(1);
$pdf->SetFont('Arial','B',11);
$pdf->Cell(40.5,0.7,"Approve",0,10,'C');

$pdf->ln(1);
$pdf->SetFont('Arial','B',9);
$pdf->Cell(40.5,0.7,"Technician Name",0,10,'C');
//Nama file ketika di print
$pdf->Output("laporan_buku.pdf","I");

?>
 

Now you can try it for yourself, I don't need to explain the details above, because I have inserted an explanation in each code that I think is important.

So I highly recommend using this FPDF to check the report you want, you just need to enter the date and click search, the report you are looking for appears.

PHP has very good capabilities in creating PDF files on-the-fly. FPDF is one of the PHP libraries that can be used to create reports and other PDF documents easily, without the need to install or activate certain modules. The PDF report produced also has a good and quality appearance.

Maybe some of the posts above, if you still want to use the above, or you find the coded error please leave a comment below I will be happy to help you. Thanks

No comments:

Post a Comment