Thursday, April 19, 2012

Converting Number to Indian Currency Number


 Converting Number to Indian Currency Number:

 $num = 1000;
 $converted_number = formatIndianStyle($num);
 print $converted_number;


//Ans: 1,000.00


 function formatIndianStyle($num){
     $pos = strpos((string)$num, ".");
     if ($pos === false) {
        $decimalpart="00";
     }
     if (!($pos === false)) {
        $decimalpart= substr($num, $pos+1, 2); $num = substr($num,0,$pos);
     }


     if(strlen($num)> 3 & strlen($num) <= 12){
         $last3digits = substr($num, -3 );
         $numexceptlastdigits = substr($num, 0, -3 );
         $formatted = makeComma($numexceptlastdigits);
         $stringtoreturn = $formatted.",".$last3digits.".".$decimalpart ;
     }elseif(strlen($num)<=3){
        $stringtoreturn = $num.".".$decimalpart ;
     }elseif(strlen($num)>12){
        $stringtoreturn = number_format($num, 2);
     }


     if(substr($stringtoreturn,0,2)=="-,"){
        $stringtoreturn = "-".substr($stringtoreturn,2 );
     }


     return $stringtoreturn;
}


     function makeComma($input){
     if(strlen($input)<= 2)
     { return $input; }
     $length=substr($input,0,strlen($input)-2);
     $formatted_input = makeComma($length).",".substr($input,-2);
     return $formatted_input;
 }

Monday, April 16, 2012

PDF Creation

PDF Creation:


First we need to download the DOMPDF [Html to Pdf converter] from [http://code.google.com/p/dompdf/].Then keep that folder inside your pdf module.



function pdf_creation_menu(){
  $items['pdf_creation'] = array(
    'page callback' => 'pdf_creation',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
);
  
  return $items;
}


function  pdf_creation () {
//You can add your own HTML or you can print your DB details as a HTML
//You can theme it by default drupal theming and pass that value to this $content
// Ex: $content = theme('create_pdf', $value);
// In this create_pdf tpl you can theme with your own styles,but you need to give the styles inside the same file itself. Like <style>your styles</style> at the top of the tpl file.



$content = '<html><body>'.
  '<p>Put your html here, or generate it with your favourite '.
  'templating system.</p>'.
  '</body></html>'; 


  require_once("dompdf/dompdf_config.inc.php");  //Path to identify the dompdf converter.
  $dompdf = new DOMPDF();
  $dompdf->load_html($content);
  $dompdf->set_paper('letter', 'portrait');
  $dompdf->render();


  $dompdf->stream("pdf_name.pdf");


  exit(0);
}


Finally give this link [pdf_creation] as your PDF downloader link.