Paket Web Aplikasi



Cara Mudah Membuat Laporan PDF di PHP dan MySQL dengan MPDF


Oke pada kesempatan kali ini saya akan berbagi sedikit tutorial untuk membuat report PDF di PHP dan MySQL dengan MPDF, MPDF merupakan plugin untuk generate report ke dalam file PDF, kelebihan MPDF adalah mudah di customize dan mudah untuk digunakan.

Untuk anda yang ingin membuat report PDF di PHP dengan mudah, MPDF adalah jawabannya solusinya, langsung saja untuk membuat report PDF di php kita memerlukan MPDF yang bisa di download di situs resminya di sini http://www.mpdf1.com.

Bila sudah di download kita bisa memulai untuk membuat sebuah laporan PDF, pertama buatlah sebuah database dengan nama "nilai" (tanpa tanda petik). kemudian silahkan copas SQl dumping berikut ini di MySQL Query.


-- phpMyAdmin SQL Dump
-- version 4.2.11
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Generation Time: 16 Sep 2016 pada 23.36
-- Versi Server: 5.6.21
-- PHP Version: 5.5.19

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- Database: `smk`
--

-- --------------------------------------------------------

--
-- Struktur dari tabel `nilai`
--

CREATE TABLE IF NOT EXISTS `nilai` (
`id` int(5) NOT NULL,
`semester` int(2) NOT NULL,
`kode_pelajaran` char(4) NOT NULL,
`kode_guru` char(5) NOT NULL,
`kode_kelas` char(4) NOT NULL,
`kode_siswa` char(5) NOT NULL,
`nilai_tugas1` int(4) NOT NULL,
`nilai_tugas2` int(4) NOT NULL,
`nilai_tugas3` int(4) NOT NULL,
`nilai_tugas4` int(4) NOT NULL,
`nilai_tugas5` int(4) NOT NULL,
`nilai_tugas6` int(4) NOT NULL,
`nilai_tugas7` int(4) NOT NULL,
`nilai_tugas8` int(4) NOT NULL,
`nilai_tugas9` int(4) NOT NULL,
`nilai_tugas10` int(4) NOT NULL,
`nilai_tugas11` int(4) NOT NULL,
`nilai_tugas12` int(4) NOT NULL,
`nilai_tugas13` int(4) NOT NULL,
`nilai_uts` int(4) NOT NULL,
`nilai_uas` int(4) NOT NULL,
`keterangan` varchar(100) NOT NULL
) ENGINE=MyISAM AUTO_INCREMENT=20 DEFAULT CHARSET=latin1;

--
-- Dumping data untuk tabel `nilai`
--

INSERT INTO `nilai` (`id`, `semester`, `kode_pelajaran`, `kode_guru`, `kode_kelas`, `kode_siswa`, `nilai_tugas1`, `nilai_tugas2`, `nilai_tugas3`, `nilai_tugas4`, `nilai_tugas5`, `nilai_tugas6`, `nilai_tugas7`, `nilai_tugas8`, `nilai_tugas9`, `nilai_tugas10`, `nilai_tugas11`, `nilai_tugas12`, `nilai_tugas13`, `nilai_uts`, `nilai_uas`, `keterangan`) VALUES
(1, 1, 'P001', 'G0002', 'K001', 'S0002', 75, 60, 75, 60, 75, 60, 75, 60, 75, 60, 60, 60, 85, 80, 80, 'Delapan Puluh'),
(2, 1, 'P001', 'G0003', 'K001', 'S0003', 70, 60, 75, 60, 75, 60, 75, 60, 75, 60, 60, 60, 60, 75, 80, 'Delapan Puluh'),
(3, 1, 'P001', 'G0004', 'K001', 'S0004', 75, 80, 75, 60, 75, 60, 75, 60, 75, 60, 60, 60, 60, 75, 80, 'Delapan Puluh'),
(4, 1, 'P001', 'G0005', 'K001', 'S0006', 68, 70, 75, 60, 75, 60, 75, 60, 75, 60, 60, 60, 60, 85, 80, 'Delapan Puluh'),
(5, 1, 'P001', 'G0001', 'K002', 'S0007', 70, 70, 75, 60, 75, 60, 75, 60, 75, 60, 60, 60, 60, 75, 79, 'Tujuh Puluh Sembilan'),
(6, 1, 'P001', 'G0002', 'K002', 'S0010', 78, 80, 75, 60, 75, 60, 75, 60, 75, 60, 60, 60, 60, 75, 85, 'Delapan Puluh Lima'),
(7, 1, 'P001', 'G0002', 'K002', 'S0009', 78, 80, 75, 60, 75, 80, 75, 60, 75, 60, 60, 60, 60, 75, 80, 'Delapan Puluh'),
(8, 1, 'P001', 'G0002', 'K002', 'S0008', 85, 80, 75, 60, 75, 60, 75, 60, 75, 60, 60, 60, 60, 85, 90, 'Sembilan Puluh'),
(9, 1, 'P001', 'G0002', 'K002', 'S0005', 75, 70, 75, 60, 75, 60, 75, 60, 75, 60, 60, 60, 60, 75, 80, 'Delapan Puluh'),
(10, 1, 'P001', 'G0001', 'K002', 'S0007', 80, 100, 75, 60, 75, 60, 75, 60, 75, 60, 60, 60, 60, 100, 90, 'Sembilan Puluh'),
(19, 2, 'P001', 'G0006', 'K004', 'S0016', 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 'ok'),
(12, 2, 'P008', 'G0006', 'K002', 'S0009', 80, 70, 75, 60, 75, 60, 75, 60, 75, 60, 60, 60, 60, 90, 80, 'Delapan Puluh'),
(13, 1, 'P003', 'G0005', 'K003', 'S0002', 80, 80, 75, 60, 75, 60, 75, 60, 75, 60, 60, 60, 60, 75, 80, 'Delapan Puluh'),
(14, 1, 'P003', 'G0002', 'K003', 'S0002', 80, 80, 75, 60, 75, 60, 75, 60, 75, 60, 60, 60, 60, 80, 80, 'Selapan Puluh'),
(15, 1, 'P004', 'G0006', 'K001', 'S0005', 50, 0, 75, 60, 75, 60, 75, 60, 75, 60, 60, 60, 60, 80, 80, 'Delapan Puluh'),
(18, 1, 'P010', 'G0006', 'K001', 'S0006', 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 'LULUS');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `nilai`
--
ALTER TABLE `nilai`
ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `nilai`
--
ALTER TABLE `nilai`
MODIFY `id` int(5) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=20;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;



  • Bila database sudah di import silahkan buat sebuagh folder dengan nama "laporan" kemudian simpan ke dalam direktory C:/xampp/htdocs/
  • Simpan plugin MPDF ke dalam folder laoran yang baru saja di buat.
  • Kemudian buatlah file koneksi dengan copas code di bawah ini dan simpan dengan nama conn.php (
<?php
$db_host = "localhost";
$db_user = "root";
$db_pass = "";
$db_name = "nilai";

$koneksi = mysqli_connect($db_host, $db_user, $db_pass, $db_name);

if(mysqli_connect_errno()){
echo 'Gagal melakukan koneksi ke Database : '.mysqli_connect_error();
}

?>



  • kemudian silahkan copas code di bawah ini dan simpan dengan nama index.php ke dalam folder laporan (C:/xampp/htdocs/laporan)

<?php
//Define relative path from this script to mPDF
$nama_file='Surat Jalan'; //Beri nama file PDF hasil.
define('_MPDF_PATH','mpdf60/');
//define("_JPGRAPH_PATH", '../mpdf60/graph_cache/src/');

//define("_JPGRAPH_PATH", '../jpgraph/src/');

include(_MPDF_PATH . "mpdf.php");
//include(_MPDF_PATH . "graph.php");

//include(_MPDF_PATH . "graph_cache/src/");

$mpdf=new mPDF('utf-8', 'A4'); // Create new mPDF Document

//Beginning Buffer to save PHP variables and HTML tags
ob_start();

$mpdf->useGraphs = true;

?>
<!DOCTYPE html>
<html>
<head>
<title>MPDF Printout</title>
<style>
*
{
margin:0;
padding:0;
font-family: calibri;
font-size:10pt;
color:#000;
}
body
{
width:100%;
font-family: calibri;
font-size:8pt;
margin:0;
padding:0;
}

p
{
margin:0;
padding:0;
margin-left: 200px;
}

#wrapper
{
width:200mm;
margin:0 5mm;
}

.page
{
height:297mm;
width:210mm;
page-break-after:always;
}

table
{
border-left: 1px solid #fff;
border-top: 1px solid #fff;
font-family: calibri;
border-spacing:0;
border-collapse: collapse;

}

table td
{
border-right: 1px solid #fff;
border-bottom: 1px solid #fff;
padding: 2mm;

}

table.heading
{
height:50mm;
}

h1.heading
{
font-size:10pt;
color:#000;
font-weight:normal;
font-style: italic;
}

h2.heading
{
font-size:10pt;
color:#000;
font-weight:normal;
}

hr
{
color:#ccc;
background:#ccc;
}

#invoice_body
{
height: auto;
}

#invoice_body , #invoice_total
{
width:100%;
}
#invoice_body table , #invoice_total table
{
width:100%;
border-left: 1px solid #ccc;
border-top: 1px solid #ccc;

border-spacing:0;
border-collapse: collapse;

margin-top:5mm;
}

#invoice_body table td , #invoice_total table td
{
text-align:center;
font-size:8pt;
border-right: 1px solid black;
border-bottom: 1px solid black;
padding:2mm 0;
}

#invoice_body table td.mono , #invoice_total table td.mono
{
text-align:right;
padding-right:3mm;
font-size:8pt;
}

#footer
{
width:200mm;
margin:0 5mm;
padding-bottom:3mm;
}
#footer table
{
width:100%;
border-left: 1px solid #ccc;
border-top: 1px solid #ccc;

background:#eee;

border-spacing:0;
border-collapse: collapse;
}
#footer table td
{
width:25%;
text-align:center;
font-size:9pt;
border-right: 1px solid #ccc;
border-bottom: 1px solid #ccc;
}
</style>
</head>
<body>
<div id="wrapper">
<?php
include "conn.php";
?>

<table class="heading" style="width:100%;">
<tr>
<td> <center><p style="text-align:center; font-size: 16px; font-weight:bold;">LAPORAN NILAI SISWA</p></center></td>
</tr>
</table>


<div id="content">

<div id="invoice_body">
<?php
$query1="SELECT * FROM nilai ORDER BY id";

$tampil=mysqli_query($koneksi, $query1) or die(mysqli_error());
?>
<table border="1">
<tr>
<td style="width:10%;"><b>Id</b></td>
<td style="width:10%;"><b>SMT</b></td>
<td style="width:10%;"><b>Mapel</b></td>
<td style="width:10%;"><b>Guru</b></td>
<td style="width:10%;"><b>Kelas</b></td>
<td style="width:10%;"><b>Siswa</b></td>
<td style="width:10%;"><b>NH 1</b></td>
<td style="width:10%;"><b>NH 2</b></td>
<td style="width:10%;"><b>UTS</b></td>
<td style="width:10%;"><b>UAS</b></td>
</tr>
<?php
$no=0;
while($data1=mysqli_fetch_array($tampil))
{ $no++; ?>
<tr>
<td style="width:10%;" class="mono"><b><?php echo $data1['id']; ?></b></td>
<td style="width:10%;" class="mono"><b><?php echo $data1['semester']; ?></b></td>
<td style="width:10%;" class="mono"><b><?php echo $data1['kode_pelajaran']; ?></b></td>
<td style="width:10%;" class="mono"><b><?php echo $data1['kode_guru']; ?></b></td>
<td style="width:10%;" class="mono"><b><?php echo $data1['kode_kelas']; ?></b></td>
<td style="width:10%;" class="mono"><b><?php echo $data1['kode_siswa']; ?></b></td>
<td style="width:10%;" class="mono"><b><?php echo $data1['nilai_tugas1']; ?></b></td>
<td style="width:10%;" class="mono"><b><?php echo $data1['nilai_tugas2']; ?></b></td>
<td style="width:10%;" class="mono"><b><?php echo $data1['nilai_uts']; ?></b></td>
<td style="width:10%;" class="mono"><b><?php echo $data1['nilai_uas']; ?></b></td>
</tr>
<?php
}
?>
</table>
</div>
<div id="invoice_total">
<b>Keterangan :</b>
<table>
<tr>

</tr>
</table>
</div>
<br />
<hr />
<br />
<td style="border: 1px solid black;" colspan="2"><center><b>Mengetahui</b></center></td>
<table border="1" style="width:100%; height:35mm;">
<tr >
<td style="border: 1px solid black; border-top: 1px solid black;"><center><b>Kepala</b></center></td>
<td style="border: 1px solid black; border-top: 1px solid black;"><center><b>Wali Kelas</b></center></td>
<td style="border-right: 1px solid black; border-left: 1px solid black; border-bottom: 1px solid white; border-top: 1px solid white;" rowspan="8"></td>
<td style="border: 1px solid black; "><center><b>Orang Tua Siswa</b></center></td>
<td style="border-right: 1px solid black; border-left: 1px solid black; border-bottom: 1px solid white; border-top: 1px solid white;" rowspan="8"></td>
<td style="border: 1px solid black;"><center><b>Siswa</b></center></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td style="border-right: 1px solid black;"></td>
</tr>
<tr>
<td style="border-top: 1px solid white;"></td>
<td style="border-top: 1px solid white;"></td>
<td style="border-top: 1px solid white;"></td>
<td style="border-top: 1px solid white; border-right: 1px solid black;"></td>
</tr>
<tr>
<td style="border-top: 1px solid white;"></td>
<td style="border-top: 1px solid white;"></td>
<td style="border-top: 1px solid white;"></td>
<td style="border-top: 1px solid white; border-right: 1px solid black;"></td>
</tr>
<tr>
<td style="border-top: 1px solid white;"></td>
<td style="border-top: 1px solid white;"></td>
<td style="border-top: 1px solid white;"></td>
<td style="border-top: 1p solid white; border-right: 1px solid black;"></td>
</tr>
<tr>
<td style="border-top: 1px solid white;"></td>
<td style="border-top: 1px solid white;"></td>
<td style="border-top: 1px solid white;"></td>
<td style="border-top: 1p solid white; border-right: 1px solid black;"></td>
</tr>
<tr>
<td style="border-bottom: 1px solid black;"><center></center> </td>
<td style="border-bottom: 1px solid black;"><center></center></td>
<td style="border-bottom: 1px solid ;"><center> </center></td>
<td style="border-bottom: 1px solid black; border-right: 1px solid black;"><center></center></td>

</tr>
</table>
</div>
<br />
</div>

<?php

$html = ob_get_contents(); //Proses untuk mengambil data
ob_end_clean();
//Here convert the encode for UTF-8, if you prefer the ISO-8859-1 just change for $mpdf->WriteHTML($html);
$mpdf->WriteHTML(utf8_encode($html));
// LOAD a stylesheet
$stylesheet = file_get_contents('mpdfstyletables.css');
$mpdf->WriteHTML($stylesheet,1); // The parameter 1 tells that this is css/style only and no body/html/text

$mpdf->WriteHTML($html,1);

$mpdf->Output($nama_file."-".$data['no_sj'].".pdf" ,'I');




exit;
?>
</body>
</html>



  • Setelah di simpan filenya kemudian jalankan reportnya via browser, dengan mengetikan url localhost/laporan di address bar browser, bila berhasil anda akan mendappatkan report PDF seperti di bawah ini


Masih bingung.? tenang saya sudah siapkan source code nya lengkap silahkan anda download di link berikut


Tutorial sudah saya berikan dan link download source code juga sudah saya berikan, tinggal kemauan anda untuk belajar saja, karena tanpa belajar semuanya akan sia sia, semoga tutorial singkat ini bisa membantu memecahkan masalah pembuatan report PDF di PHP dan MYSQL, Tetap semangat maju terus putra - putri indonesia.

Happy Coding & Programming
Previous
Next Post »

Komentar