Monday, 10 April 2017

Convert HTML to PDF in PHP using Dompdf



In this post we have seen one more PHP PDF converter library, here we will make discussion on Dompdf library which are used to convert HTML to PDF document. So in this blog we will check how to use Dompdf library with PHP Script to generate PDF document from HTML. First time PDF document was introduced by Adobe as fixed layout document. This type of document is used for download large amount of data in document format from dynamic website. When we have download document from website then at that time PDF document has been made from HTML that means it will convert HTML to PDF. So here we will seen How to convert HTML to PDF by using Dompdf library with PHP Script.

Dompdf library used to make dynamic PDF document in your web application which are completely based on PHP language. This library will directly display HTML and CSS data in PDF format on web browser or it can directly push to download PDF document. Dompdf library is usually used for convert HTML data to PDF document. If you have build any dynamic web application and you have to provide report in PDF format then we have to suggest go for Dompdf library for making dynamic PDF document from web application. By using this library we can easily convert HTML data to fixed layout document format like PDF.



If you want to use Dompdf library for convert HTML to PDF, then you can download latest version of this library from this link of GitHub. If you have download latest version of this library then you do not want to download other PHP library like PHP Font library and PHP SVG Library. Both library will come with latest version. You can find complete guide to how to use Dompdf library from below source.




Source Code



<?php
//index.php
//include autoloader

require_once 'dompdf/autoload.inc.php';

// reference the Dompdf namespace

use Dompdf\Dompdf;

//initialize dompdf class

$document = new Dompdf();

$html = '
 <style>
table {
    font-family: arial, sans-serif;
    border-collapse: collapse;
    width: 100%;
}

td, th {
    border: 1px solid #dddddd;
    text-align: left;
    padding: 8px;
}

tr:nth-child(even) {
    background-color: #dddddd;
}
</style>
<table>
  <tr>
    <th>Company</th>
    <th>Contact</th>
    <th>Country</th>
  </tr>
  <tr>
    <td>Alfreds Futterkiste</td>
    <td>Maria Anders</td>
    <td>Germany</td>
  </tr>
  <tr>
    <td>Centro comercial Moctezuma</td>
    <td>Francisco Chang</td>
    <td>Mexico</td>
  </tr>
  <tr>
    <td>Ernst Handel</td>
    <td>Roland Mendel</td>
    <td>Austria</td>
  </tr>
  <tr>
    <td>Island Trading</td>
    <td>Helen Bennett</td>
    <td>UK</td>
  </tr>
  <tr>
    <td>Laughing Bacchus Winecellars</td>
    <td>Yoshi Tannamuri</td>
    <td>Canada</td>
  </tr>
  <tr>
    <td>Magazzini Alimentari Riuniti</td>
    <td>Giovanni Rovelli</td>
    <td>Italy</td>
  </tr>
</table>
';

//$document->loadHtml($html);
$page = file_get_contents("cat.html");

//$document->loadHtml($page);

$connect = mysqli_connect("localhost", "root", "", "testing1");

$query = "
 SELECT category.category_name, product.product_name, product.product_price
 FROM product 
 INNER JOIN category 
 ON category.category_id = product.category_id
";
$result = mysqli_query($connect, $query);

$output = "
 <style>
table {
    font-family: arial, sans-serif;
    border-collapse: collapse;
    width: 100%;
}

td, th {
    border: 1px solid #dddddd;
    text-align: left;
    padding: 8px;
}

tr:nth-child(even) {
    background-color: #dddddd;
}
</style>
<table>
 <tr>
  <th>Category</th>
  <th>Product Name</th>
  <th>Price</th>
 </tr>
";

while($row = mysqli_fetch_array($result))
{
 $output .= '
  <tr>
   <td>'.$row["category_name"].'</td>
   <td>'.$row["product_name"].'</td>
   <td>$'.$row["product_price"].'</td>
  </tr>
 ';
}

$output .= '</table>';

//echo $output;

$document->loadHtml($output);

//set page size and orientation

$document->setPaper('A4', 'landscape');

//Render the HTML as PDF

$document->render();

//Get output of generated pdf in Browser

$document->stream("Webslesson", array("Attachment"=>0));
//1  = Download
//0 = Preview


?>

0 comments:

Post a Comment