Home » PHP » Convert HTML into PDF using PHP

Convert HTML into PDF using PHP

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.


This is my index.php file.


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


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)


Similar posts

  • Mohittanwat111

    how to download this file…………….many times i have click download button but it si redirecting to either net love or jabong 

    • After redirected that page,wait 5 seconds and then click “skip ad” button in the top right corner.Then source code would be automatically download.

      • Mohittanwat111

         thanks sumit i have not seen right corner thanks a lot

  • After redirected that page,wait 5 seconds and then click “skip ad” button in the top right corner.Then source code would be automatically download.

  • Rajagonda

    thanks  its working… 

  • Rajagonda Nyalakanti


  • Prasad

    Hi,Its working awesome thanks for sharing the code.

  • Dillibabu K

    Hi, That is working very well. But I tried to convert google chart into pdf. After conversion, i’m getting empty pdf file. So, Will you Please tell me the reason and Solution..?

    Thanks in advance..:-DG)

    • It getting blank page, because of first execute PDF generate process before the generate and display the chart in that page. So you need to check if page is fully loaded or that chart content is fully loaded.Then execute the PDF process.

  • Sunil Omrey

    not working well??????/plz help

    • This is should be working.First you must check all files exist,paths, config details,…etc.
      Can you send the issues details.


  • lokesh

    after skip add This website/URL has been blocked until further notice either pursuant
    to Court orders or on the Directions issued by the Department of

  • Thanks its working.

  • H?nh Phan Ng?c

    How to fit only for one A4 size…

    • Use this code.

      $dompdf->set_paper(“A4”, “portrait”);


      • H?nh Phan Ng?c

        Thanks 🙂

  • hithu

    “Failed to Load pdf document ” this shows the browser when i click the ‘convert’ button…
    i down load your compress file and extract run useing wamp server …!
    plz help me

    • Hi,
      If you really do have spaces before your PHP opening tag, they will become part of the PDF file and corrupt it. Load up your “bad” pdf, do a file->save, and opening the file in a text/hex editor (NOT a pdf reader) and see what you’re getting. Any PHP errors/warnings or other output before the actual PDF data will cause the PDF readers to see it as corrupted.


      • Php Dotnet

        hi i m getting the same problem . i just downloaded you file and put it into htdocs folder of xampp . pls help me . i m waiting for ur reply

    • Php Dotnet

      hi i m getting the same problem . i just downloaded you file and put it into htdocs folder of xampp . pls help me

  • suneth

    Hi Sumith,

    I am trying to convert in to pdf which has new line texts. But when i convert it in to pdf all text are in single line. I want the text in same format.

    I am trying this for several hours. please help me to proceed. Thank you.

    • Hi Suneth,
      I updated the post. You can see the working solution at the bottom of the post. Let know is that work or not.


      • Suneth

        Hi Sumith,

        Thank you very mush for the response.

        I changed my code according to your suggestion. But I get following error. And I am using codeigniter for my development.

        Fatal error: Uncaught exception ‘DOMPDF_Exception’ with message
        ‘Frame not found in cellmap’ in
        Renderer->render(Object(Table_Cell_Frame_Decorator), false)
        Renderer->render(Object(Table_Row_Frame_Decorator), false)
        #5 C:xampphtdocsdurdanslabapplicationthird_part in C:xampphtdocsdurdanslabapplicationthird_partydompdfincludecellmap.cls.php on line 212

        • Hi,
          As you did last time undo the changes above. Then put your not worked code back. then change the following lines.

          Replace with,

          And add this code before $dompdf->stream(“mypage.pdf”, array(“Attachment” => false));



          • Suneth

            Hi Sumith,
            below is my changed code according to you. But still give the same error.

            function pdf_create($html, $filename, $stream=TRUE, $orientation=”portrait”)
            require_once APPPATH . ‘third_party/dompdf/dompdf_config.inc.php’;
            $html = stripslashes($html);
            $dompdf = new DOMPDF();
            $dompdf->set_paper(“a4”, $orientation);

            if ($stream) {
            $dompdf->stream($filename . “.pdf”);
            } else {
            write_file(“pdf_report/”.$filename.”.pdf”, $dompdf->output());

          • Try this. Also check correctly added all files paths.

            require_once APPPATH . ‘third_party/dompdf/dompdf_config.inc.php’;

            function pdf_create($html, $filename, $stream=TRUE, $orientation=”portrait”)


            $html = stripslashes($html);
            $dompdf = new DOMPDF();
            $dompdf->set_paper(“letter”, $orientation); // $size ‘letter’, ‘legal’, ‘A4’, etc , $orientation ‘portrait’ or ‘landscape’

            $dompdf->stream($filename.”.pdf”, array(“Attachment” => false));

          • Suneth

            Hi Sumith,

            Your code was working.

            Thank you very much.

            But the problem is my codeigniter code. Because I load the $html file from several views($this->load-view()). Once I get $html, only from view which generate the is working. But when load other views to $html is not working. I will find out a solution for that issue.

            So thank you again..

          • Glad to hear that. Good luck !

  • subodh

    i have create two form one form name index.php and in that past you program
    ok then dompdf_config,inc.php in that past your code even if inthat one create problem

    Fatal error: Class ‘DOMPDF’ not found in D:AppServwwwpdfdompdf_config.inc.php on line 14

  • Ashu Pandit

    Hello sir,
    How can i convert my whole forms elements into pdf ? in example it is giving value for only a single textarea.


  • vivek kumar

    Hey Sumith how can i download your script

    • Hi,
      Go to the download link and wait 5 seconds. after then you can see skip this add button top tight corner. click it. do this in next page also. after that you can download the fully source code.

      Thank you