Categories
PHP

Membuat Laporan PDF Menggunakan PHP FPDF

belajar php pdf

Sesuai yang kami katakan pada artikel sebelumnya bahwa FPDF adalah kelas PHP yang memungkinkan untuk menghasilkan file PDF dengan PHP murni, artinya tanpa menggunakan perpustakaan PDFlib. F dari FPDF adalah Free. 

Ini adalah generator yang bisa nada gunakan secara bebas dan bisa anda kembangkan sesuai dengan kebutuhan anda, Langsung saja mari kita masuk kedalamam pembahasan utama yaitu membuat laporan PDF menggunakan PHP FPDF.

Tetapi sebelum anda memulai anda harus mendownload generator FPDF terlebih dahulu, Berikutnya buat database dengan nama check kemudian masukkan struktur database dibawah ini.

CREATE TABLE `tblcheck` (
  `id` int(11) NOT NULL,
  `techName` varchar(250) NOT NULL,
  `location` varchar(250) NOT NULL,
  `name` varchar(50) NOT NULL,
  `facilityName` varchar(50) NOT NULL,
  `PersoninCharge` varchar(50) NOT NULL,
  `checkDetails` mediumtext NOT NULL,
  `regDate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `status` varchar(25) DEFAULT NULL,
  `lastUpdationDate` varchar(25) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data untuk tabel `tblcheck`
--

INSERT INTO `tblcheck` (`id`, `techName`, `location`, `name`, `facilityName`, `PersoninCharge`, `checkDetails`, `regDate`, `status`, `lastUpdationDate`) VALUES
(8, 'Ardi', 'Jl. Sultan Hasanuddin, Keluarahan Malino, Pasangarahan', 'Rumah Direksi Utama', 'Amplifier', 'Ardi dan Abi dan Ah ah', 'Dimana mana hatiku senang oh contaku padamu gila gila gila dna ankajdkjasbdiasgdias', '2019-02-02 16:57:00', 'Kurang Bagus', '03-02-2019 09:32:34 PM');

Berikutnya buat file dengan nama config.php file ini berfungsi sebagai koneksi kedatabase.

<?php
define('DB_HOST', 'localhost');
define('DB_USER','root');
define('DB_PASS' ,'');
define('DB_NAME', 'check');
$con = mysqli_connect(DB_HOST,DB_USER,DB_PASS,DB_NAME);
// Check connection
if (mysqli_connect_errno())
{
 echo "Failed to connect to MySQL: " . mysqli_connect_error();\
}
?>

Buat file dengan nama index.php file ini berfungsi sebagai form untuk menampilkan database.

<?php session_start();
include('config.php');

?>
<!DOCTYPE html>
<html>
<head>
  <title>Print</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  
  <!--Script CSS-->
  <link type="text/css" href='https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css' rel='stylesheet'>
  <link type="text/css" href='https://cdn.datatables.net/responsive/2.2.1/css/responsive.dataTables.min.css' rel='stylesheet'>
  <link type="text/css" href='https://cdn.datatables.net/buttons/1.5.1/css/buttons.dataTables.min.css' rel='stylesheet'>
  
</head>
<body>
  <br /><br />
  <div class="container">
     <nav class="navbar navbar-inverse">
        <div class="container-fluid">
           <div class="navbar-header">
              <a class="navbar-brand" href="https://jasawebsite.ooo/">PRINT REPORT USING FPDF</a>
          </div>
      </div>
  </nav>
  <br />
  <h2 align="center">PRINT REPORT USING FPDF</h2>
  <br />
  <form method="post">
   <div class="form-group">
       
      <!--Script untuk memanggil Javascript-->
      <table id="example" class="display responsive nowrap" style="width:100%">
        <thead>
            <tr>
                <th>No</th>
                <th>Tech Name</th>
                <th>Location</th>
                <th>Home department name</th>
                <th>Print</th>
            </tr>
        </thead>
        <tbody>
            <?php $query=mysqli_query($con,"select * from tblcheck");
              $cnt=1;
              while($row=mysqli_fetch_array($query))
              {
                ?>                   
                <tr>
                  <td><?php echo htmlentities($row['id']);?></td>
                  <td><?php echo htmlentities($row['techName']);?></td>
                  <td><?php echo htmlentities($row['location']);?></td>
                  <td><?php echo htmlentities($row['name']);?></td>

                  <td>
                    <a href="print.php?id=<?php echo htmlentities($row['id']);?>"><button type="button" class="btn btn-primary">Print</button></a>
                  </td>

                </tr>

              <?php  } ?>

        </tbody>
    </table>
</div>
<div class="form-group">
</form>
</div>

<!--Script Javascript-->
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/responsive/2.2.1/js/dataTables.responsive.min.js"></script>
<script src="https://cdn.datatables.net/buttons/1.5.1/js/dataTables.buttons.min.js"></script>
<script src="https://cdn.datatables.net/buttons/1.5.1/js/buttons.colVis.min.js"></script>
<script>
   $(document).ready(function() {
      $('#example').DataTable( {
        dom: 'Bfrtip',
        buttons: [
        'colvis'
        ]
    } );
  } );
</script>

</body>
</html>

Buat file dengan nama dengan nama print.php file ini merupakan php murni yang berfungsi sebagai report dari database di atas.

<?php
include('config.php');
session_start();
require('fpdf/fpdf.php');



//A4 width : 219mm
//default margin : 10mm each side
//writable horizontal : 219-(10*2)=189mm

$pdf = new FPDF('P','mm','A4');

$pdf->AddPage();

//Image( file name , x position , y position , width [optional] , height [optional] )
$pdf->Image('watermark.png',60,30,89);


//set font to arial, bold, 14pt
$pdf->SetFont('Arial','B',14);



//Cell(width , height , text , border , end line , [align] )

$pdf->Cell(130 ,5,'TUTORIALSWB DAN TUTORPHPID',0,0);
$pdf->Cell(59 ,5,'CHECK FASILITAS',0,1);//end of line

//set font to arial, regular, 12pt
$pdf->SetFont('Arial','',12);

$pdf->Cell(130 ,5,'Jl. Tani, Mamampang',0,0);
$pdf->Cell(59 ,5,'',0,1);//end of line
$pdf->Cell(130 ,5,'Garassi, Tinggimoncong, 92174',0,0);


$query=mysqli_query($con,"select * from tblcheck where id = '".$_GET['id']."'");
        while($row=mysqli_fetch_array($query)){


$pdf->Cell(25 ,5,'Tanggal  :',0,0);
$pdf->Cell(34 ,5,$row['regDate'],0,1);//end of line

$pdf->Cell(130 ,5,'Nomor Telp (+62) 81524737292',0,0);
$pdf->Cell(25 ,5,'Nomor    :',0,0);
$pdf->Cell(34 ,5,$row['id'],0,1);//end of line

$pdf->Cell(130 ,5,'Fax (0411) 11223344',0,0);
$pdf->Cell(25 ,5,'Teknisi   :',0,0);
$pdf->Cell(34 ,5,$row['techName'],0,1);//end of line


//make a dummy empty cell as a vertical spacer
$pdf->Cell(189 ,10,'',0,1);//end of line

//billing address
$pdf->SetFont('Arial','B',12);
$pdf->Cell(100 ,5,'DETAILS LAPORAN CHECK',0,1);//end of line
$pdf->Cell(100 ,5,'',0,1);//end of line


$pdf->SetFont('Arial','B',10);
$pdf->Cell(45 ,5,'Rumah Department',0,0);
$pdf->SetFont('Arial','I',10);
$pdf->Cell(90 ,5, $row['name'],0,1);
$pdf->Cell(100 ,2,'',0,1);//end of line


$pdf->SetFont('Arial','B',10);
$pdf->Cell(45 ,5,'Lokasi',0,0);
$pdf->SetFont('Arial','I',10);
$pdf->Cell(90 ,5,$row['location'],0,1);
$pdf->Cell(100 ,2,'',0,1);//end of line


$pdf->SetFont('Arial','B',10);
$pdf->Cell(45 ,5,'Nama Teknisi',0,0);
$pdf->SetFont('Arial','I',10);
$pdf->Cell(90 ,5,$row['techName'],0,1);
$pdf->Cell(100 ,2,'',0,1);//end of line


$pdf->SetFont('Arial','B',10);
$pdf->Cell(45 ,5,'Penanggung Jawab',0,0);
$pdf->SetFont('Arial','I',10);
$pdf->Cell(90 ,5,$row['PersoninCharge'],0,1);
$pdf->Cell(100 ,2,'',0,1);//end of line


$pdf->SetFont('Arial','B',10);
$pdf->Cell(45 ,5,'Nama Fasilitas',0,0);
$pdf->SetFont('Arial','I',10);
$pdf->Cell(90 ,5,$row['facilityName'],0,1);
$pdf->Cell(100 ,2,'',0,1);//end of line


$pdf->SetFont('Arial','B',10);
$pdf->Cell(45 ,5,'Update Check',0,0);
$pdf->SetFont('Arial','I',10);
$pdf->Cell(90 ,5,$row['lastUpdationDate'],0,1);
$pdf->Cell(100 ,2,'',0,1);//end of line


$pdf->SetFont('Arial','B',10);
$pdf->Cell(45 ,5,'Status',0,0);
$pdf->SetFont('Arial','I',10);
$pdf->Cell(90 ,5,$row['status'],0,1);
$pdf->Cell(100 ,2,'',0,1);//end of line


$pdf->SetFont('Arial','B',10);
$pdf->Cell(45 ,5,'Check Details',0,0);
$pdf->SetFont('Arial','I',10);
$pdf->MultiCell(120 ,5,$row['checkDetails'],0,1);


//make a dummy empty cell as a vertical spacer
$pdf->Cell(189 ,10,'',0,1);//end of line
$pdf->SetFont('Arial','B',10);
$pdf->Cell(182,4,"Di Cetak Pada : ".date("D-d/m/Y"),0,0,'R');
//set font to arial, bold, 14pt

$pdf->Cell(189 ,10,'',0,1);//end of line
$pdf->SetFont('Arial','B',10);

$pdf->Cell(130 ,4,'PEMILIK RUMAH',0,0);
$pdf->Cell(59 ,5,'APARAT KEAMANAN',0,1);//end of line


}

$pdf->Output("Laporan Check.pdf","I");
?>

Kalian bisa mendownload filenya langsung pada link button dibawah ini.

Jika kalian belum paham dan ingin menanyakan sesuatu terkait dengan apa yang kami share di atas dilahkan tinggalkan komentar dibawah ini.

Leave a Reply

Your email address will not be published. Required fields are marked *