Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to convert your html page to pdf using fpdf in cakephp

    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 6.55k
    Comment on it

    Some time we need to convert the html page to pdf dynamically.

    Like we are getting some form filled and then we want the data to be converted to a pdf file that is downloadable by the user.

    We use fpdf for that in cakephp.

    fpdf is basically a class that is used to create the pdf file.

    fpdf can be downloaded from its website and can be pasted into the vendor fpdf folder.

    After that we need to an html page that we want to convert and a php file where our convert function can come.

    Create a form html

    <!DOCTYPE html>
    
    <html>
    
    <head>
    
    <meta charset="utf-8" />
    
    <title>How to create Contact Form using Bootstrap  | </title>
    
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    
    <link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.min.css" />
    
    <link rel="stylesheet" type="text/css" href="font-awesome/css/font-awesome.min.css" />
    
    <script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
    
    <script type="text/javascript" src="bootstrap/js/bootstrap.min.js"></script>
    
    <style>
    
        .header {
    
            color: #36A0FF;
    
            font-size: 27px;
    
            padding: 10px;
    
        }
    
    </style>
    
    </head>
    
    <body>
    
    <div class="container">
    
    <div class="page-header">
    
        <h1>Convert HTML to PDF in PHP with fpdf</h1>
    
    </div>
    
    <!-- Contact Form - START -->
    
    <div class="container">
    
        <div class="row">
    
            <div class="col-md-12">
    
                <div class="well well-sm">
    
    //action page where pdf will be create and shown into the browser
    
                    <form class="form-horizontal" method="post" action="createpdf.php">
    
                        <fieldset>
    
                            <legend class="text-center header" style="width:70%;">Contact us<span><img src="images/logo.png"></span></legend>
    
                            <div class="form-group">
    
                                <div class="col-md-8">
    
                                   <input id="name" name="name" type="text" placeholder="Name" class="form-control">
    
                                </div>
    
                            </div>
    
                            <div class="form-group">
    
                                <div class="col-md-8">
    
                                    <input id="email" name="email" type="text" placeholder="Email Address" class="form-control">
    
                                </div>
    
                            </div>
    
                            <div class="form-group">
    
                                 
    
                                <div class="col-md-8">
    
                                 
    
                                    <input id="phone" name="phone" type="text" placeholder="Phone" class="form-control">
    
                                </div>
    
                            </div>
    
                            <div class="form-group">
    
                                <div class="col-md-12 text-center">
    
                                    <button type="submit" class="btn btn-primary btn-lg">Submit</button>
    
                                </div>
    
                            </div>
    
                        </fieldset>
    
                    </form>
    
                </div>
    
            </div>
    
        </div>
    
    </div>
    
    <!-- Contact Form - END -->
    
    </div>
    
    </body>
    
    </html>

    Now create a pdfcreate to convert to pdf.

    <?php
    
    require('WriteHTML.php');
    
    $pdf=new PDF_HTML();
    
    $pdf->AliasNbPages();
    
    
    ////add page page automatically for its true parameter
    
    
    $pdf->SetAutoPageBreak(true, 15);
    
    $pdf->AddPage();
    
    //add images or logo which you want
    
    $pdf->Image('images/logo.png',18,13,33);
    
    //set font style
    
    $pdf->SetFont('Arial','B',14);
    
    $pdf->WriteHTML('<para><h1>Click for Blog, Tutorials, jQuery, Ajax, PHP, MySQL and Demos</h1><br>
    
    <br><br>How to Convert HTML to PDF with fpdf example');
    
    
    //set the form of pdf
    
    
    $pdf->SetFont('Arial','B',7);
    
    
    //assign the form post value in a variable and pass it.
    
    
    $htmlTable='<TABLE>
    
    <TR>
    
    <TD>Name:</TD>
    
    <TD>'.$_POST['name'].'</TD>
    
    </TR>
    
    <TR>
    
    <TD>Email:</TD>
    
    <TD>'.$_POST['email'].'</TD>
    
    </TR>
    
    <TR>
    
    <TD>Phone:</TD>
    
    <TD>'.$_POST['phone'].'</TD>
    
    </TR>
    
    </TABLE>';
    
    //Write HTML to pdf file and output that file on the web browser.
    
    $pdf->WriteHTML2("<br><br><br>$htmlTable");
    
    $pdf->SetFont('Arial','B',6);
    
    $pdf->Output();
    
    ?>

    Now this can create the pdf of form submission data.

 1 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: