HTML to PDF

html to pdf

In this post , I will cover as how you convert an HTML page into PDF  file on a fly.

You basically need an fpdf  library to do the task.You may download it from http://www.fpdf.org/en/download.php

We will be using HTML2FPDF class of fpdf library.

Now let us see, how to convert a sample HTML page into a PDF file using HTML2FPDF Library.

Below is the code for the conversion.

<?php
 require('html2fpdf.php');
 $pdf=new HTML2FPDF();
 $pdf->AddPage();
 $fp = fopen("sample.html","r");
 $strContent = fread($fp, filesize("sample.html"));
 fclose($fp);
 $pdf->WriteHTML($strContent);
 $pdf->Output("sample.pdf");
 echo "PDF file is generated successfully!";
 ?>

Just that simple.You just need to include html2fpdf.php.Here I have passed sample.html page,the pdf generated will have same content of sample.html page,however in the pdf format.

Advertisements

One thought on “HTML to PDF

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s