Convert HTML to PDF using PHP code

Convert HTML to PDF using PHP code (HTML2FPDF Class library)

In web development, you often need to create PDF (Portable Document Format) to view or download/ print a document.

 

Here is a simple class that will provide you a simple way to do create PDF.HTML2FPDF Class library consist of three classes namely PDF, HTML2FPDF and FPDF (modified FPDF class). The class PDF extends the class HTML2FPDF that extends the class FPDF.

 

Convert a sample html page into a PDF file using HTML2FPDF Library by following below simple steps.

[php]

<?php
require(‘html2fpdf.php’);
$pdf=new HTML2FPDF();

$pdf->AddPage();
$fp = fopen("scriptarticle-sample.html","r");
$strContent = fread($fp, filesize("scriptarticle-sample.html"));
fclose($fp);

$pdf->WriteHTML($strContent);
$pdf->Output("sample.pdf");
echo "PDF file has been generated successfully!";

?>

[/php]

 

The HTML2FPDF class library will work best with the XHTML 1.0.
Download the full code by the link below.

html2pdf-generator.zip

 

21 thoughts on “Convert HTML to PDF using PHP code”

  1. I am using inline css and want to increase page width. But it seems like content is not center aligned. Is there any option that I can manage the content align center

    1. Please have a look on the main class, you will find the function and parameters values for achieving the same.

  2. I used this code get warning like this plz verify it and help asap AddPage(); $fp = fopen(“scriptarticle-sample.html”,”r”); $strContent = fread($fp, filesize(“scriptarticle-sample.html”)); fclose($fp); $pdf->WriteHTML($strContent); $pdf->Output(“sample.pdf”); echo “PDF file is generated successfully!”; ?>

    1. Shrinivas..can you let me know what warning you are getting? It will better to configure if you paste the warning also.

  3. I run index file and get this result on my browser AddPage(); $fp = fopen(“scriptarticle-sample.html”,”r”); $strContent = fread($fp, filesize(“scriptarticle-sample.html”)); fclose($fp); $pdf->WriteHTML($strContent); $pdf->Output(“sample.pdf”); echo “PDF file is generated successfully!”; ?>
    Plz help… Need very urgent. Thanks

    1. Jyoti, Open file “index.php”, in that there is a tag “<?” change that to “<?php”, I think this is the only problem you have, or you should enable the “short_open_tag” in your php.ini settings. Let me know if you still face any issue.

    1. What code you are using.. the sample zipped file creating the border as well for me.. tested today itself? let me know how can I help you?

  4. I downloaded your code and its working fine with the sample example.But When i use external php file link its not working . Please suggest me.

    1. Veera, this may be due to not including the class files, can you let me know what error or warning you are getting during code execution?

  5. Hi,

    Thanks for sharing the code. This is really helpful for my project. If possible, pls share how to show the pdf file instead of save it other location.

    i changed the output tag but nothing works.

    Thanks in advance.

  6. it is ok with html but when i using a php file to read then the generated pdf file contains the inner php code , does not contain the output of the php..

    1. Either you have picked the data from buffer or NOWDOC. You should take the HTML data in a string that will definitely work.

    1. I have visited the above URL and it have a lot of notices of Undefined index, Can you let me know what exactly issue you have facing? “Undefined index” issue is due to using an array element that has not be initialized.

Comments are closed.