Wednesday, March 16, 2011

HTML to PDF - PHP Tutorial

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 below...
html to PDF using PHP
HTML to PDF - PHP Tutorial

6 comments:

php ecommerce on March 19, 2011 at 3:00 AM said...

Great informative post having great tips! Thanks for sharing. I would like to keep visiting this site often.

Originative on April 4, 2011 at 12:55 PM said...

i like the way you comment :)

Jordan said...

I know this is an old tutorial but anyway.....
Having problems with your example above. I do not have the getHTML() function. Where is that located?
Thanks

Originative on April 11, 2012 at 2:17 PM said...

getHTML() is not shown here... it simpley mean that you have to put HTML code here... please download the sample code provided it will help...

Moizjam on March 18, 2013 at 11:07 PM said...

This post is very helpful thanks. !!!

Witrotus said...

there is an error. ican't find getHTML() ? why? i don't understand even i read the comment above :/

Post a Comment

 

Blog Info

A Pakistani Website by Originative Systems

Total Pageviews

Tutorial Jinni Copyright © 2015 WoodMag is Modified by Originative Systems