html php to pdf code example
Example 1: php to pdf
<?php
use Dompdf\Dompdf;
session_start();
$servername = "";
$username = "";
$password = "";
$dbname = "";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$html = '<table border=1>';
$html .= '<thead>';
$html .= '<tr>';
$html .= '<th>ID</th>';
$html .= '<th>Collum1</th>';
$html .= '<th>Collum2</th>';
$html .= '<th>Collum3</th>';
$html .= '</tr>';
$html .= '</thead>';
$html .= '<tbody>';
$sql = "SELECT * FROM tableName";
$sql = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($sql)){
$html = '';
}
require_once("dompdf/autoload.inc.php");
$dompdf = new DOMPDF();
$dompdf->load_html('
<h1 style="text-align: center;">RentCar</h1>
'. $html .'
');
$dompdf->render();
$pdf = $dompdf->output();
$dompdf->stream(
"form.pdf",
array(
"Attachment" => false
)
);
?>
Example 2: convert html to pdf php
Important: 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.
Here are some online services that you can use:
PDFShift
Restpack
PDF Layer
DocRaptor
HTMLPDFAPI
HTML to PDF Rocket
Have a look at PrinceXML.
It'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)
Oh yeah, did I mention that this is the first (and probably only) HTML2PDF solution that does full ACID2 ?
PrinceXML Samples