How to Generate mPDF from MySQL Data using PHP
mPDF is a PHP library generating PDF files from UTF-8 encoded HTML. mPDF is based on FPDF and HTML2FPDF with a number of enhancements. mPdf is Supports almost all languages including RTL. and CSS stylesheets inline and external and internal way to use. mPDF to Password protection pdf Generate.
Installation :
Installation method via composer and its packagist package mpdf/mpdf. inside your project directory the below command run :
composer require mpdf/mpdf
Folder structure :
pdf (project directory)
vendor
composer.json
composer.lock
index.php(create new file)
Create the database table :
The following SQL code. This is to create our “event” database table.
...
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";
--
-- Database: `devnote`
--
-- --------------------------------------------------------
--
-- Table structure for table `event`
--
CREATE TABLE `event` (
`id` int(11) NOT NULL,
`event_name` varchar(255) DEFAULT NULL,
`event_venue` varchar(255) DEFAULT NULL,
`event_details` longtext,
`event_date` varchar(255) DEFAULT NULL,
`sport` int(11) DEFAULT NULL,
`event_fee` float(10,2) DEFAULT NULL,
`player_fee` float(10,2) DEFAULT NULL,
`visitor_fee` float(10,2) DEFAULT NULL,
`event_image` longtext,
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
--
-- Dumping data for table `event`
--
INSERT INTO `event` (`id`, `event_name`, `event_venue`, `event_details`, `event_date`, `sport`, `event_fee`, `player_fee`, `visitor_fee`, `event_image`, `created_at`) VALUES
(1, 'lorem lipsum', 'rajkot', 'lorem lipsum content', '2020-03-18', 0, 100.00, 0.00, 0.00, '1584160487_event-img.jpg', '2020-03-14 10:04:47'),
(2, 'lorem lipsum 1', 'rajkot', 'lorem lipsum content 1', '2020-03-18', 0, 100.00, 0.00, 0.00, '1584160487_event-img.jpg', '2020-03-14 10:04:47'),
(3, 'lorem lipsum 2', 'rajkot', 'lorem lipsum content 2', '2020-03-18', 0, 100.00, 0.00, 0.00, '1584160487_event-img.jpg', '2020-03-14 10:04:47'),
(4, 'lorem lipsum 3', 'rajkot', 'lorem lipsum content 3', '2020-03-18', 0, 100.00, 0.00, 0.00, '1584160487_event-img.jpg', '2020-03-14 10:04:47'),
(5, 'lorem lipsum 4', 'rajkot', 'lorem lipsum content 4', '2020-03-18', 0, 100.00, 0.00, 0.00, '1584160487_event-img.jpg', '2020-03-14 10:04:47'),
(6, 'lorem lipsum 5', 'rajkot', 'lorem lipsum content 5', '2020-03-18', 0, 100.00, 0.00, 0.00, '1584160487_event-img.jpg', '2020-03-14 10:04:47'),
(7, 'lorem lipsum 6', 'rajkot', 'lorem lipsum content 6', '2020-03-18', 0, 100.00, 0.00, 0.00, '1584160487_event-img.jpg', '2020-03-14 10:04:47'),
(8, 'lorem lipsum 7', 'rajkot', 'lorem lipsum content 7', '2020-03-18', 0, 100.00, 0.00, 0.00, '1584160487_event-img.jpg', '2020-03-14 10:04:47'),
(9, 'lorem lipsum 8', 'rajkot', 'lorem lipsum content 8', '2020-03-18', 0, 100.00, 0.00, 0.00, '1584160487_event-img.jpg', '2020-03-14 10:04:47'),
(10, 'lorem lipsum 9', 'rajkot', 'lorem lipsum content 9', '2020-03-18', 0, 100.00, 0.00, 0.00, '1584160487_event-img.jpg', '2020-03-14 10:04:47'),
(11, 'lorem lipsum 0', 'rajkot', 'lorem lipsum content 0', '2020-03-16', 1, 0.00, 1.00, 2.00, '1584160487_event-img.jpg', '2020-03-16 11:57:46');
--
-- Indexes for dumped tables
--
--
-- Indexes for table `event`
--
ALTER TABLE `event`
ADD PRIMARY KEY (`id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `event`
--
ALTER TABLE `event`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=12;
COMMIT;
Example :
#index.php
<?php
$html = '';
$html .= '<table border="1" cellspacing="0" cellspadding="0" width="100%">
<thead>
<tr>
<th>No</th>
<th>Event</th>
<th>Venue</th>
<th>Details</th>
<th>Date</th>
<th>Fee</th>
<th>Player</th>
<th>Visitor</th>
</tr>
</thead>
<tbody>';
$link = mysqli_connect("localhost","root","","devnote");
$no = 1;
$q = "SELECT * FROM `event` ORDER BY id DESC";
$res = mysqli_query($link,$q);
$row = mysqli_num_rows($res);
if($row > 0) {
while($row = mysqli_fetch_assoc($res)) {
$html .= '<tr align="center"><td>'.$no.'</td>
<td>'.$row['event_name'].'</td>
<td>'.$row['event_venue'].'</td>
<td>'.substr($row['event_details'],0,200).'</td>
<td>'.$row['event_date'].'</td>
<td>'.$row['event_fee'].'</td>
<td>'.$row['player_fee'].'</td>
<td>'.$row['visitor_fee'].'</td>';
$no++;
}
} else {
$html .= '<tr aling="center"><td colspan="8">No Event</td></tr>';
}
$html .= '</tbody></table>';
require_once __DIR__ . '/vendor/autoload.php';
$mpdf = new \Mpdf\Mpdf(['mode' => 'utf-8','format' => 'A4-L' ]);
$mpdf->SetDisplayMode('fullpage');
$mpdf->WriteHTML( $html );
/* $mpdf->Output( 'devnote.pdf', 'F' ); */ Direct download your project directory
$mpdf->Output();
exit;
gracias por el aporte, puede por favor, mostrar en tiempo real este ejemplo, para poder guiarnos, ya que en mi localhost, no me esta permitiendo visualizar toda la informacion