<tutorialjinni.com/>

HTML to PDF Converter Using PHP

Posted Under: PHP, Programming, Tutorials on Mar 11, 2011
HTML to PDF Converter Using PHP
Generating PDF from HTML using PHP is fairly simple and easy using HTML2FPDF

Lets get straight to point, who has time to read and "write" the obvious stuff :P

here is the Simplest code to create HTML to PDF using PHP... First the HTML we want to convert to PDF
<table width='500px' border='1' cellpadding='0' cellspacing='0' style=' border-collapse:collapse;'>
      <tr>
 <td colspan='2' align='left'> Jhon Doe <br>
     Some where on earth :) <br>
     email@domain.com <br>
 </td>
      </tr>
      <tr>
 <td width='75%' align='center'>Description</td>
 <td width='25%' align='center' valign='middle'>Price</td>
      </tr>
       <tr>
  <td align='left'>Some purchased items</td>
  <td align='center' valign='middle'>$ 100.00</td>
       </tr>

       <tr>
  <td align='left'>Another purchased items</td>
  <td align='center' valign='middle'>$ 55.00</td>
       </tr>

       <tr>
  <td align='left'>Another purchased items</td>
  <td align='center' valign='middle'>$ 35.00</td>
       </tr>

       <tr>
  <td align='left'>Another purchased items</td>
  <td align='center' valign='middle'>$ 123.00</td>
       </tr>

       <tr>
  <td align='left'>Yet Another purchased items</td>
  <td align='center' valign='middle'>$ 307.00</td>
       </tr>

       <tr>
  <td align='left'>Another purchased items</td>
  <td align='center' valign='middle'>$ 342.00</td>
       </tr>
     <tr>
 <td align='right'>Total Amount</td>
 <td align='center' valign='middle'>$ 962</td>
      </tr>
      <tr>
 <td align='right'>Advance</td>
 <td align='center' valign='middle'>$ 0.00</td>
      </tr>
      <tr>
 <td align='right'>Total Payable Amount:</td>
 <td align='center' valign='middle'>$ 962</td>
      </tr>
    </table>
Now the PHP code to do the work done
<?php
require('html2fpdf.php');

$name="print_".time().".pdf";
// $name name of the PDF generated.

$html=getHTML();
// getHTML() function will return the above mention HTML

$pdf=new HTML2FPDF();
$pdf->AddPage();
$pdf->WriteHTML($html);

$re=$pdf->Output($name,"D");

// Genrate PDF, There few Options
// 1. D => Download the File
// 2. I => Send to standard output
// 3. F => Save to local file
// 4. S => Return as a string

?>

Image Integration

Its is very easy to integrate images in the PDF, the only thing required is that use absolute path for images, relative path of an image throws an exception.

Example

There many Other options which can be used...
Image Example HTML Page

Test #1 - RTF-like text. Click here for results.

Test #2 - Links,Colors,Forms. Click here for results.

Test #3 - Table. Click here for results.

Test #4 - CSS. Click here for results.

Test #6 - P,DIV tests. Click here for results.

A real HTML page. Click here for results.

Download

Download all the requried items from here...


imgae