Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

MODULE

MEETING 10 – PDF REPORTING

1. INTRODUCTION

Mpdf is a library provided to assist in making data reports in PDF format.

2. Composer preparation.
- mpdf link: https://mpdf.github.io/
- Pay attention to the installation instructions, mpdf requires composer that is already packaged on the
computer.

- To enable composer, download composer on page


https://getcomposer.org/download/
- On windows, just click Next, and the installation is complete.
- Check composer whether it is active or not in your command prompt
Open CMD, navigate to the directory where you saved your php file.

- Type "composer" and enter, if it is installed it will appear as follows:

- Composer is active and ready to use for mpdf installation.


3. mpdf installation
- Make sure your computer is connected to the internet.
- The mpdf file is quite large, so be patient during the download process.
- Type "composer require mpdf/mpdf"

- Make sure the latest version is installed.


- The installation is complete then the mpdf is ready to use. Check in the meeting folder 10 there will be a new
volder with the name "vendor".

4. Open pageadmin.php.
- Add link to print data <a href="print.php"
target="_blank">print</a>

5. We will print student data to be used as a report. Check


- the mpdf page, how to use it.
-
- The above process begins with loading from the vendor folder and creating a new pdf
file. Note that the WriteHTML operation is the content of the document to be printed.
So to display our data, what is needed is to change the contents of the data according
to student data.
- Create a new file with the name print.php
<?php
//fetch the mpdf library from vendor require_once
__DIR__ folder. '/vendor/autoload.php';

//get data from database


$conn = mysqli_connect('localhost','root','',pwd2020');
$search = "SELECT * FROMstudent";
$student = mysqli_query($conn, $search);

//create a new pdf file


$mpdf = new \Mpdf\Mpdf();

//contents of the printed


document $html = '<!DOCTYPE
html> <html>
<head>
<title>Student List</title>
</head>
<body>
<h1>Student List</h1>
<table border="1" cellpadding="10" cellspacing="0">
<tr>
<th>No.</th>
<th>NIM</th>
<th>Name</th>
<th>Email</th>
<th>Department</th>
</tr>';
$i = 1;
foreach ($student as $row)
{ $html .='<tr>
<td>'.$i++.'</td>
<td>'.$row["nim"].'</td> <td>'.
$row["name"].'</td> <td>'.
$row["email"].' </td> <td>'.
$row["department"].'</td>
</tr>';
}
$html .='</table>

</body>
</html>';

//print a pdf with $html is the document content


$mpdf->WriteHTML($html);

//save in pdf format $mpdf-


>Output();

?>

You might also like