1<?php
2//install Dompdf library at link below:
3//https://github.com/dompdf/dompdf
4use Dompdf\Dompdf;
5session_start();
6
7 // Include the database
8 $servername = "";
9 $username = "";
10 $password = "";
11 $dbname = "";
12
13 // Create connection
14 $conn = new mysqli($servername, $username, $password, $dbname);
15 // Check connection
16 if ($conn->connect_error) {
17 die("Connection failed: " . $conn->connect_error);
18 }
19
20 $html = '<table border=1>';
21 $html .= '<thead>';
22 $html .= '<tr>';
23 $html .= '<th>ID</th>';
24 $html .= '<th>Collum1</th>';
25 $html .= '<th>Collum2</th>';
26 $html .= '<th>Collum3</th>';
27 $html .= '</tr>';
28 $html .= '</thead>';
29 $html .= '<tbody>';
30
31 $sql = "SELECT * FROM tableName";
32 $sql = mysqli_query($conn, $sql);
33 while($row = mysqli_fetch_assoc($sql)){
34 $html = ''; // your html code
35 }
36
37 // include autoloader
38 require_once("dompdf/autoload.inc.php");
39
40 //Create instance
41 $dompdf = new DOMPDF();
42
43 // Upload your HTML code
44 $dompdf->load_html('
45 <h1 style="text-align: center;">RentCar</h1>
46 '. $html .'
47 ');
48
49 //Render html
50 $dompdf->render();
51
52 //Create and output the pdf
53 $pdf = $dompdf->output();
54
55 //Visualize the page
56 $dompdf->stream(
57 "form.pdf",
58 array(
59 "Attachment" => false //To download turn it to true, to preview pdf turn it to false
60 )
61 );
62
63?>
1Important: Please note that this answer was written in 2009 and it might not be the most cost-effective solution today in 2019. Online alternatives are better today at this than they were back then.
2
3Here are some online services that you can use:
4
5PDFShift
6Restpack
7PDF Layer
8DocRaptor
9HTMLPDFAPI
10HTML to PDF Rocket
11Have a look at PrinceXML.
12
13It's definitely the best HTML/CSS to PDF converter out there, although it's not free (But hey, your programming might not be free either, so if it saves you 10 hours of work, you're home free (since you also need to take into account that the alternative solutions will require you to setup a dedicated server with the right software)
14
15Oh yeah, did I mention that this is the first (and probably only) HTML2PDF solution that does full ACID2 ?
16
17PrinceXML Samples