Convert HTML into PDF using PHP

35

How do I convert html file into pdf file using php.In the past, I found everywhere to convert html file into pdf file using php.Finally I found easy method to convert html page into pdf.In this article is describing how to convert html content or page in to pdf without worrying.

DOMPDF can be use to convert html file into pdf easily.
Here is a dompdf home page link.You can download dompdf library file from there.

First of all you must download dompdf library.Then follow these steps.

Step-1

This is my index.php file.

Step-2

Then I adding links to pdf library files into this page.

Step-3

Creating pdf generating process.

Put this all together.

How to convert specific content into PDF

Now I’m going to explain how to do this and I’m not going to describes everything because essential part is explained above.
Here is my php page.

When your are working in the live server there may ba a error generating pdf file.So then follow these steps.

  • Check you are correctly give the link path to the library config file.
  • Check you are correctly wrote scripts like above.
  • Add the following scripts in your page top.

Convert textarea contents with formatting

textarea to pdf

If you try to do the textarea contents convert with it’s formatting may be that will not work with above solutions.
That means textarea has line breaks, that will not showing in the PDF file. Here is the solution for that.

Hope this would be help.

You can convert table,page,contents, and inserting images, changing fonts,changing paper size….etc to visiting dompdf usages page.

Download Convert HTML into PDF using PHP Source(476 KB)

Share.

About Author

Sumith Harshan is the Web Engineer Specializes in Java Script, JQuery, HTML4/5, CSS2/3, PHP, CodeIgniter, CakePHP, J2SE, J2EE, XML, Hacking & Security, Mobile Interface Developing, Magento, Wordpress, Joomla, Drupal,PHPBB,Facebook App Developing and Open cart...etc.