Convert Html to Pdf using PHP
Convert Html to Pdf using PHP and DOMPDF
In this tutorial, you are going to learn, how to generate a PDF of HTML table contents using dompdf library in PHP on a Button Click.
There are 3 files used in this tutorial.
Versions:
- PHP Version 7.2.8
- dompdf_0-8-3
Create a project folder and copy all the given files into the folder. Download the given dompdf library with above link. Extract and copy the dompdf folder, into a project folder.
Now your project folder looks as given in the picture.
Next, Create a database, table with given Queries.
Db Queries:
Create a Database
Database: `agencydb`
CREATE TABLE `tbl_visitors` (
`id` int(11) NOT NULL,
`visitorname` varchar(30) NOT NULL,
`visitoremail` varchar(30) NOT NULL,
`visitormobile` varchar(30) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `tbl_visitors`
--
INSERT INTO `tbl_visitors` (`id`, `visitorname`, `visitoremail`, `visitormobile`) VALUES
(1, 'John', 'john@gmail.com', '123412345'),
(2, 'dany', 'dany@gmail.com', '543212345');
1.config.php
<?php $databaseHost = 'localhost'; $databaseName = 'agencydb'; $databaseUsername = 'root'; $databasePassword = '';
$con = mysqli_connect($databaseHost, $databaseUsername, $databasePassword, $databaseName);
?>
2.index.php
<?php include_once("config.php"); $sql = "SELECT * FROM tbl_visitors"; $table = ""; $result = mysqli_query($con,$sql );
$table ="'<h1> Sample Data </h1><table border='1'><tr><td>VisitorID</td><td>VisitorName</td><td>VisitorEmail</td><td>VisitorMobile</td></tr>";
while($res = mysqli_fetch_array($result)) {
$visitorid = $res['id'];
$visitorname =$res['visitorname'];
$visitoremail =$res['visitoremail'];
$visitormobile =$res['visitormobile'];
$table .= "<tr><td>" . $visitorid . "</td><td>" . $visitorname . "</td><td>" . $visitoremail . "</td><td>" . $visitormobile . "</td></tr>";
}
$table .= "</table>";
echo $table;
?>
<html>
<head>
<title>Coding-zon Demo - Convert HTML to PDF using DomPDF </title>
<style>
body
{
width: 80%;
margin-left: auto;
margin-right: auto;
text-align:center;
font-family: "Helvetica,Arial,Sans-Serif,Myriad Pro","Helvetica Neue";
}
table {
font-family: sans-serif,arial;
border-collapse: collapse;
width: 80%;
align:center;
}
td, th {
border: 1px solid #cccccc;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #fffccc;
}
</style>
</head>
<body>
<div>
<form action="generatepdf.php" method="post">
<input type="hidden" name="table" value="<?php
echo $table; ?>"><br />
<input type="submit" name="submit_val" value="GeneratePDF">
</form>
</div>
</body>
</html>
3. generatepdf.php
<?php namespace Dompdf; require_once 'dompdf/autoload.inc.php'; //how-to-convert-dynamic-php-file-to-pdf if(isset($_POST['submit_val'])) { $table1 = $_POST['table']; $dompdf = new Dompdf();
$dompdf->loadHtml( $table1);
$dompdf->setPaper('A4', 'landscape');
// Render the HTML as PDF
$dompdf->render();
// displays the generated PDF in a browser
$dompdf->stream("sample.pdf",array("Attachment" => false));
// Outputs the generated PDF to Browser downloads directly
// $dompdf->stream();
exit(0);
}
?>
Index.php
PDF-Output:
Comments
Post a Comment