HomeCoding & Programming

Convert HTML to PDF using PHP code

Like Tweet Pin it Share Share Email

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
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!";

?>

 

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

html2pdf-generator.zip

 

Comments (24)

  • nice one its working. now how i can to give the link to download this pdf file

  • Why css code not working in this library?

  • 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

    • Author

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

  • 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!”; ?>

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

  • 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

    • Author

      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.

  • table border is not working…

    • Author

      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?

  • 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.

    • Author

      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?

  • 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.

  • 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..

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

  • Thanks for the script but it is running partially you can check the URL for the errors I am getting

    http://www.witsendmosaic.com/admin/print_invoice1.php

    • 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.

  • i got “Deprecated: Function ereg() is deprecated” and “Deprecated: Function set_magic_quotes_runtime() is deprecated” errors .how to fix that.

  • I download your code But its not working it shows following message while run it.. ->
    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!”; ?>

  • How to apply external style sheet for css?

  • Hi
    Thanks for the good work.
    Please how can I display the pdf in a browser

Comments are closed.