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.

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)

Tags

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

    thanks 

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

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

      Regards

  • 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
    Telecommunications”

  • Thanks its working.

  • H?nh Phan Ng?c

    How to fit only for one A4 size…

    • Use this code.

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

      Thanks

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

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

      Thanks

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

      Thanks

      • 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
        C:xampphtdocsdurdanslabapplicationthird_partydompdfincludecellmap.cls.php:212
        Stack
        trace:
        #0
        C:xampphtdocsdurdanslabapplicationthird_partydompdfincludetable_cell_renderer.cls.php(51):
        Cellmap->get_spanned_cells(Object(Table_Cell_Frame_Decorator))
        #1
        C:xampphtdocsdurdanslabapplicationthird_partydompdfincluderenderer.cls.php(283):
        Table_Cell_Renderer->render(Object(Table_Cell_Frame_Decorator))
        #2
        C:xampphtdocsdurdanslabapplicationthird_partydompdfincluderenderer.cls.php(117):
        Renderer->_render_frame(‘table-cell’,
        Object(Table_Cell_Frame_Decorator))
        #3
        C:xampphtdocsdurdanslabapplicationthird_partydompdfincluderenderer.cls.php(185):
        Renderer->render(Object(Table_Cell_Frame_Decorator), false)
        #4
        C:xampphtdocsdurdanslabapplicationthird_partydompdfincluderenderer.cls.php(185):
        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.
          $dompdf->load_html($_POST[“html”]);

          Replace with,
          $dompdf->load_html(nl2br($_POST[“html”]));

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

          $dompdf->output();

          Thanks

          • 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’;
            $this->load->helper(‘file_helper’);
            $html = stripslashes($html);
            $dompdf = new DOMPDF();
            $dompdf->set_paper(“a4”, $orientation);
            $dompdf->load_html(nl2br($html));
            $dompdf->render();

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

          • 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”)
            {

            $this->load->helper(‘file_helper’);

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

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

          • Suneth

            Hi Sumith,

            Your code was working.
            $dompdf->load_html(nl2br($html));

            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.

    Thanks

  • 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