• Convert HTML to PDF file using PHP

    For convert HTML code to PDF, we can use dompdf library.

    Let’s get started:

    Step 1 : Download the domepdf package from below link.

    Download : https://code.google.com/p/dompdf/downloads/list
    Unzip the zip file and put it into your localhost folder.

    Step 2 : Put code for generate HTML code to PDF file

    Create one php file and put below code into it. And replace variables as your convenience. The parameters are,
    Filename.pdf – Replace with your suitable name of pdf file that you want.
    $base_path – set the path where you want to save pdf file.
    $buff – set the html code you want to generate in pdf file.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    
    <?
    include('cppdf/dompdf_config.inc.php');
     
                global $_dompdf_show_warnings;
                global $_dompdf_debug;
                global $_DOMPDF_DEBUG_TYPES;
     
                $outfile = 'Filename.pdf'; // set the file name
                $save_file = TRUE; // Save the file or not
     
                $buff = '<b>Testing</b>

    hello

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    
    ';
     
                $dompdf = new DOMPDF();
                $base_path = $_SERVER['DOCUMENT_ROOT'].'/this/pdf/html/'; // path of file
     
                $dompdf-&gt;load_html($buff);
                if ( isset($base_path) ) {
                    $dompdf-&gt;set_base_path($base_path);
                }
                $dompdf-&gt;render();
     
                if ( $_dompdf_show_warnings ) {
                    global $_dompdf_warnings;
                    foreach ($_dompdf_warnings as $msg) {
                        echo $msg . "\n";
                    }
                    echo $dompdf-&gt;get_canvas()-&gt;get_cpdf()-&gt;messages;
                    flush();
                }
    //
                if ( $save_file ) {
                    // if ( !is_writable($outfile) )
                        // throw new DOMPDF_Exception("'$outfile' is not writable.");
     
                    if ( strtolower(DOMPDF_PDF_BACKEND) == "gd" ) {
                        $outfile = str_replace(".pdf", ".png", $outfile);
                    }
     
                    list($proto, $host, $path, $file) = explode_url($outfile);
     
                    if ( $proto != "" ) // i.e. not file://
                        $outfile = $file; // just save it locally, FIXME? could save it like wget: ./host/basepath/file
     
                    $outfile = dompdf_realpath($outfile);
                    // if ( strpos($outfile, DOMPDF_CHROOT) !== 0 )
                        // throw new DOMPDF_Exception("Permission denied.");
                    file_put_contents($outfile, $dompdf-&gt;output( array("compress" =&gt; 0) ));
                }
     
                if (!headers_sent() ) 
                    $dompdf-&gt;stream($outfile);
     
    ?&gt;

    Step 3 : Run above php file

    Now, run your php file as created in above steps, by that you will found one popup for saving the file.
    You can save generated pdf file. and that’s all we want.

    Tags:

Comments on this post

Leave a Reply

  • Security Code :


    − 2 = six