Dear Reader, let us we know that “How to generate PDF in PHP using Mysql and MPDF Library“. So in this Tutorial, We Learn Step By Step How to generate PDF from HTML Process.
PDF stands for Portable Document Format. PDF is a file format that used to presents documents in a manner independent of application software, hardware and operating system.PDF files are created using Adobe Acrobat, Acrobat Capture, or similar products. To view and use the files, you need the free Acrobat Reader, which you can easily download.
First, we see Snapshots for our Export data to PDF format
Step 1:- First of all you need to create the database like ci_demo
Step 2:- Now create a table “mobiles” in a database using the below SQL code
1 2 3 4 5 6 7 8 |
CREATE TABLE `mobiles` ( `id` int(11) NOT NULL, `model_no` varchar(30) NOT NULL, `mobile_name` varchar(30) NOT NULL, `company` varchar(40) NOT NULL, `mobile_category` text NOT NULL, `price` double(16,2) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; |
Step 3:- Now dump some data for table “mobiles” using bellow SQL code
1 2 3 4 5 6 7 8 9 |
INSERT INTO `mobiles` (`id`, `model_no`, `mobile_name`, `company`, `mobile_category`, `price`) VALUES (13, 'SM-G615FZKUINS', 'Samsung Galaxy On Max (Black, ', 'Samsung', 'Smartphones', 20800.00), (14, ' SM-G955FZKGINS', 'Samsung Galaxy S8 Plus (Midnig', 'Samsung', 'Smartphones', 18300.00), (15, 'MN0X2HN/A', 'Apple iPhone 6s (Silver, 32 GB', 'Apple', 'Smartphones', 50000.00), (16, 'MQ8E2HN/A', 'Apple iPhone 8 Plus (Silver, 6', 'Apple', 'Smartphones', 60200.00), (17, ' R1 R829', 'OPPO R1 R829 (Black, 16 GB) (', 'OPPO', 'Smartphones', 19000.00), (18, 'F1', 'OPPO F1 (Gold, 16 GB) (3 GB R', 'OPPO', 'Smartphones', 15500.00), (19, 'MZB5602IN', 'Redmi 4A (Gold, 32 GB) (3 GB ', 'Xiomi', 'Smartphones', 5999.00), (20, 'MZB5653IN', 'Mi A1', 'Xiomi', 'Smartphones', 17500.00); |
Step 4:- Now we need to download MPDF library here and extract this in your project root directory as we can see in below snapshot
Step 5:- Now Create mobiles.php file and paste the below code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
<?php include('header.php'); include('db.php'); $sql = "SELECT * FROM mobiles"; $result = mysqli_query($con,$sql); if(isset($_REQUEST['submit'])) { /*-------------------- for genearte pdf start -----------------*/ include("mpdf/mpdf.php"); $mpdf = new mPDF(); //$html = '<h1>Welcome</h1>'; $html = '<div class="table-responsive"> <table class="table table-hover tablesorter pdf_border"> <thead> <tr class="pdf_border"> <th class="header pdf_border">Model No.</th> <th class="header pdf_border">Mobile Name</th> <th class="header pdf_border">Price</th> <th class="header pdf_border">Company</th> <th class="header pdf_border">Category</th> </tr> </thead> <tbody>'; ?> <?php $sql = "SELECT * FROM mobiles"; $result = mysqli_query($con,$sql); while($row = mysqli_fetch_array($result)) { $html .= '<tr class="pdf_border"> <td class="pdf_border">'.$row['model_no'].'</td> <td class="pdf_border">'.$row['mobile_name'].'</td> <td class="pdf_border">'.$row['price'].'</td> <td class="pdf_border">'.$row['company'].'</td> <td class="pdf_border">'.$row['mobile_category'].'</td> </tr>'; } $html .= '</tbody></table>'; //echo $html; die; $path = 'upload/'; $file_name ="webpreparations-".time().".pdf"; $stylesheet = '<style>'.file_get_contents('assets/css/bootstrap.min.css').'</style>'; // Read the css file $mpdf->WriteHTML($stylesheet,1); // $mpdf->WriteHTML($html,2); $mpdf->Output($path.$file_name, "F"); /*-------------------- for genearte pdf close -----------------*/ $sql = "SELECT * FROM mobiles"; $result = mysqli_query($con,$sql); } ?> <!DOCTYPE html> <html lang="en"> <head> <link rel="stylesheet" href="assets/css/style.css"> <link rel="stylesheet" href="assets/css/bootstrap.min.css"> </head> <title>How to generate pdf and send mail with attachment</title> <body> <form action="" method="POST"> <div class="table-responsive"> <table class="table table-hover tablesorter pdf_border"> <thead> <tr class="pdf_border"> <th class="header pdf_border">Model No.</th> <th class="header pdf_border">Mobile Name</th> <th class="header pdf_border">Price</th> <th class="header pdf_border">Company</th> <th class="header pdf_border">Category</th> </tr> </thead> <br> <input type="submit" name="submit" class="pull-right btn btn-warning btn-large" style="margin-right:40px" value="Genarte PDF"> <br> <tbody> <?php if (mysqli_num_rows($result) > 0 ) { while($row = mysqli_fetch_array($result)){ ?> <tr class="pdf_border"> <td class="pdf_border"><?php echo $row['model_no']; ?></td> <td class="pdf_border"><?php echo $row['mobile_name']; ?></td> <td class="pdf_border"><?php echo $row['price']; ?></td> <td class="pdf_border"><?php echo $row['company']; ?></td> <td class="pdf_border"><?php echo $row['mobile_category']; ?></td> </tr><?php } } else { ?> <tr> <td colspan="5" class="alert alert-danger">No Records founds</td> </tr> <?php } ?> </tbody> </table> </div> </form> </body> </html> |
extract zip file and copy assets folder and paste in your project’s root directory as we can see in below snapshot
Step 7:- Now run your project, so you need to type in browser localhost/your_project_name/mobiles.php
like
http://localhost/php_demo/mobiles.php then you can see the result as in below snapshot, you can Export data in PDF format by click on Generate PDF button
Congratulations you have successfully generate PDF with MySQL data into PDF format in PHP using MPDF library, if you like this post and was helpful for you then share this post on social media and if you have any query then please contact us or comment below, Thanks.
2 Comments:
pinij July 06, 2018
Error
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in /home/yourcompa1/domains/yourcompanyprofile.com/public_html/PDF/mobiles.php on line 82
Webpreparations Team July 08, 2018
Please share your code.