Thursday, 19 March 2015

Convert HTML to PDF in PHP with fpdf


HTML to PDF conversion is always a problem for PHP Programmers and all the time they search for suitable solutions so after reviewing this article you will not take more than 10 minutes to configure HTML to PDF, I have used a library fpdf open source and very useful library for developers here is a simple tutorial on how to convert How to Convert HTML to PDF with fpdf.

You have to download fpdf library and  include it in your PHP file below settings and how to show tags, fonts and images in your pdf file. With fpdf library we used HTMLparser library contributed by programmers and all other libraries available here you can download and use as per your requirement.

Index.html

In this file I have created a simple contact form data on submit it show that submitted data on PDF format:

 
<html>
<head>
<link href="css/bootstrap.min.css" rel="stylesheet" media="screen">
<link href="css/bootstrap-responsive.min.css" rel="stylesheet" media="screen">
<script type="text/javascript" src="js/jquery-1.8.0.min.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>
<title>Form</title>
</head>
 
<body>
<h2>How to create Contact Form using Bootstrap example.&nbsp;&nbsp;&nbsp;=> <a href="http://www.phpgang.com/">Home</a> | <a href="http://demo.phpgang.com/">More Demos</a></h2>
<div class="container">
      <form class="contact-us form-horizontal" action="actionpdf.php" method="post">
        <legend>Fill Form and submit to generate PDF</legend>        
        <div class="control-group">
            <label class="control-label">Name</label>
            <div class="controls">
                <div class="input-prepend">
                <span class="add-on"><i class="icon-user"></i></span>
                    <input type="text" class="input-xlarge" name="name" placeholder="Name">
                </div>
            </div>
        </div>
        <div class="control-group">
            <label class="control-label">Email</label>
            <div class="controls">
                <div class="input-prepend">
                <span class="add-on"><i class="icon-envelope"></i></span>
                    <input type="text" class="input-xlarge" name="email" placeholder="Email">
                </div>
            </div>    
        </div>
        <div class="control-group">
            <label class="control-label">Url</label>
            <div class="controls">
                <div class="input-prepend">
                <span class="add-on"><i class="icon-globe"></i></span>
                    <input type="text" id="url" class="input-xlarge" name="url" placeholder="http://www.example.com">
                </div>
            </div>
        </div>
        <div class="control-group">
            <label class="control-label">Comment</label>
            <div class="controls">
                <div class="input-prepend">
                <span class="add-on"><i class="icon-pencil"></i></span>
                    <textarea name="comment" class="span4" rows="4" cols="80" placeholder="Comment (Max 200 characters)"></textarea>
                </div>
            </div>
        </div>
        <div class="control-group">
          <div class="controls">
            <button type="submit" class="btn btn-primary">Submit</button>
            <button type="button" class="btn">Cancel</button>
          </div>    
        </div>
      </form>
</div>
</body>
</html>
 

 Actionpdf.php

This file contain PHP code to generate pdf file and show your submitted data on that file.
 
<?php
require('WriteHTML.php');
 
$pdf=new PDF_HTML();
 
$pdf->AliasNbPages();
$pdf->SetAutoPageBreak(true, 15);
 
$pdf->AddPage();
$pdf->Image('logo.png',18,13,33);
$pdf->SetFont('Arial','B',14);
$pdf->WriteHTML('<para><h1>PHPGang Programming Blog, Tutorials, jQuery, Ajax, PHP, MySQL and Demos</h1><br>
Website: <u>www.phpgang.com</u></para><br><br>How to Convert HTML to PDF with fpdf example');
 
$pdf->SetFont('Arial','B',7); 
$htmlTable='<TABLE>
<TR>
<TD>Name:</TD>
<TD>'.$_POST['name'].'</TD>
</TR>
<TR>
<TD>Email:</TD>
<TD>'.$_POST['email'].'</TD>
</TR>
<TR>
<TD>URl:</TD>
<TD>'.$_POST['url'].'</TD>
</TR>
<TR>
<TD>Comment:</TD>
<TD>'.$_POST['comment'].'</TD>
</TR>
</TABLE>';
$pdf->WriteHTML2("<br><br><br>$htmlTable");
$pdf->SetFont('Arial','B',6);
$pdf->Output(); 
?>
 
In this file we add page and auto page break true if your content increase single page area then it will automatically add 2nd page and process.

$pdf->Image('logo.png',18,13,33);
$pdf->SetFont('Arial','B',14);
 
These lines used to add a logo and select font size for heading.

$pdf->SetFont('Arial','B',7);
 
Select small font then heading for inner content.

$pdf->WriteHTML2("<br><br><br>$htmlTable");
$pdf->Output();
 
Write HTML to pdf file and output that file on the web browser.


Thursday, 12 March 2015

HTML5 Cheat Sheet




Download .png file (Right click, “Save as”) / Size: 3508 x 2717

Download .pdf file (Right click, “Save as”) / Size: 5,8Mb

Monday, 2 March 2015

Force a Line Break in a Loooooong Word in a DIV?

This could be added to accepted answer for 'cross-broswer' solution.

CSS

.your_element{
    -ms-word-break: break-all;
    word-break: break-all;

 /* Non standard for webkit */
     word-break: break-word;

    -webkit-hyphens: auto;
       -moz-hyphens: auto;
        -ms-hyphens: auto;
            hyphens: auto;

}

Thursday, 26 February 2015

Get Back To Previous Page

<button type="button" onclick="history.back();">Back</button>

Other Things

back()                 Loads the previous URL in the history list.

forward()          Loads the next URL in the history list.

go()                         Loads a specific URL from the history list.

Side note: this is straight Javascript, not jQuery. There are completely history plugins designed using the jQuery plugin framework, but they are meant to handle ajax and your history (to not break the back button on a single page that behaves as many pages)

Tuesday, 24 February 2015

Clear All Input Fields in a Specific div With JQuery

I have added two buttons in the Fiddle to illustrate how you can insert or clear values in those input fields through buttons. You just capture the onClick event and call the function.


//Fires when the Document Loads, clears all input fields
$(document).ready(function() {
  $('.fetch_results').find('input:text').val('');    
});


//Custom Functions that you can call
function resetAllValues() {
  $('.fetch_results').find('input:text').val('');
}


function addSomeValues() {
  $('.fetch_results').find('input:text').val('Lala.');
}


This function is used to clear all the elements in the form including radio button, check-box, select, multiple select, password, text, textarea, file.


function clear_form_elements(class_name) {
  jQuery("."+class_name).find(':input').each(function() {
    switch(this.type) {
        case 'password':
        case 'text':
        case 'textarea':
        case 'file':
        case 'select-one':
        case 'select-multiple':
            jQuery(this).val('');
            break;
        case 'checkbox':
        case 'radio':
            this.checked = false;
    }
  });

}

Saturday, 21 February 2015

Php String Function

You can create own function in PHP Language but , PHP has many predefined function like string function , array function , numeric function etc.
PHP all String functions as bellow table…

Php Function

PHP functions are comparable to other programming languages. A function is a portion of code which takes one more input in the form of parameter and does some processing and returns a value.

PHP have already built-in functions but PHP gives you option to create your own functions as well.

Readmore.