Student Of Fortune

Creating a Report (Report) PDF with PHP and HTML2FPDF

Share on :
For those of us who frequently engaged in the world of web development, certainly never asked to make an application in which there is the report module. In terms of making the report, we sometimes had difficulty in determining the format and libraries that will be used. Some frequently used report format is HTML (and CSS), PDF, Image, CSV and Excel (Spreadsheet). Each format has advantages and limitations of each.

PDF
PDF (Portable Document Format) is one of the options report format often used in web-based applications. Those advantages include a standard format and can be displayed in all operating system platforms (cross platform). In addition, more secure PDF format from the side of safety and validity of the data presented. PDF is also more easily stored or archived for in the form of a file. But on the other hand, deficiency of the current PDF is still seldom available web-based PDF creation libraries are pretty easy to use but powerful.

HTML2FPDF
After doing a search on the internet, I finally found one class (library) PHP that is reliable and easy in terms of making a PDF file. Library is named HTML2FPDF, and is located at http://html2fpdf.sourceforge.net/. In essence, how the work done by the library is changing the HTML commands into the PDF. So, for us who will make the report quite make it through the HTML commands which of course we are more familiar. Next we live "ordered" library HTML2FPDF to turn it into a PDF file. 

HTML2FPDF itself use FPDF library in terms of making a PDF file. So in addition to his classes, we also require the FPDF library on our servers. HTML2FPDF can be obtained from the official website at http://html2fpdf.sourceforge.net/ free of charge (free). But the site does not include library FPDF, so we have to add it yourself. As an alternative, you can get the full version of the library HTML2FPDF (including FPDF and some examples of the program) at the following address:

HTML2FPDF (327.4 KiB, 8,821 hits)  

Example Usage HTML2FPDF
To create a PDF report with the class (library) HTML2FPDF, basically can be done in 2 (two) ways. The first way is to create an HTML file first and then read the contents and send it to the class to converted to PDF. Then the second way is to create HTML files directly in the program code (PHP). Both are basically the same.

Here's an example program to convert files "sample.html" to PDF.
See The Code :
  1. <?
  2. require('html2fpdf/html2fpdf.php');
  3. $pdf=new HTML2FPDF();
  4. $pdf->AddPage();
  5. $fp = fopen("sample.html","r");
  6. $strContent = fread($fp, filesize("sample.html"));
  7. fclose($fp);
  8. $pdf->WriteHTML($strContent);
  9. $pdf->Output("sample.pdf","I");
  10. //echo "PDF file is generated successfully!";
  11. ?>
And here are examples of programs to generate a PDF where the HTML code directly written in PHP.
See The Code :
  1. <?php
  2. require('html2fpdf/html2fpdf.php');
  3. $pdf=new HTML2FPDF();
  4. $pdf->AddPage();
  5. $strContent = "<h1 align=center>Hello World!</h1>
    <p>Now I can easily make a PDF report</p>";
  6. $pdf->WriteHTML($strContent);
  7. $pdf->Output("sample2.pdf","I");
  8. //echo "PDF file is generated successfully!";
  9. ?>
Hopefully useful
Reference
http://html2fpdf.sourceforge.net, HTML2FPDF Official Site
HTML2FPDF using PHP
http://achmatim.net/2008/11/20/membuat-laporan-report-pdf-dengan-php-dan-html2fpdf/#codesyntax_2

0 comments on Creating a Report (Report) PDF with PHP and HTML2FPDF :

Post a Comment and Don't Spam!

Dont Spam please

 
Recommended Post Slide Out For Blogger

Recent Comments

My Rank