Add Bangla Font (ex: solaimanlipi) or other ttf font to mpdf

MPDF is a great tool to work with UTF-8 complex texts such as Bangla and indic scripts. You can add your own fonts to MPDF and use it with CSS properties as well.

1. just add this line in your config_fonts.php file inside the mpdf folder (user 6.0 or latest) “solaimanlipi” => array( ‘R’ => “SolaimanLipi.ttf”, ‘useOTL’ => 0xFF, ),

2. copy the solaimanlipi.ttf inside the ttfonts folder.

3. change your css styles for the PDF scripts.

body, p, div { font-size: 14pt; font-family: solaimanlipi;}

h3 { font-size: 15pt; margin-bottom:0; font-family: solaimanlipi; }

4. now run it, you will have the solaimanlipi integrated in mpdf.

 

you can do it with other unicode font as well

Ask PHP/MySQL/Javascript/Web questions!!!

Are you looking for PHP,MySQL,Javascript,Web related answers?

Have you faced difficult questions in your interview?

Are you planning for certification exams and you have questions unanswered?

do you want to correct your reasoning?

Then add questions to this post and you can add as many questions as you want. Every week I will answer or compile the answer for the questions. So lets make the question bank a bigger one.

Atlast our dream project has started!!!

Yes our dream project, “Bangla PHP book” has started last week. Me, Hasin, Emran, Shureed and all those enthusiast developer from PHPXperts group really making it happen. Thanks to Hasin for his initiative and we are very optimistic to bring out a quality book very soon. If anyone is interested, you can join our project. more information on this project can be found here.

http://groups.google.com/group/phpbook/

My hats off to all the contributors and well wishers of this project. Keep up the good works.

How to SWAP values of two variables without using a third variable (integer) ?

Many times I asked this particular question in the interview session and many people failed to answer. This is just to see the analytic ability of a candidate and how dynamic thinking they have. The logic is same for all programming language, so if you have the logic right then you can answer it for any particular language such as PHP, JAVA, C++, C etc. but in this tutorial We will use PHP 🙂

What if we can use the third variable:

$a = 100;

$b = 200;

now we need to swap the values so that $a becomes 2oo and $b becomes 1oo.

the basic technique is to introduce a third variable $tmp

$tmp = $a;

$a = $b;

$b = $tmp;

isn’t it straight forward. introducing a third variable it is so easy. but what will be our approach if there is no third variable? is it possible?

The answer is a big YES. There are several ways and they fall in two types of solution.

1. Arithmetic operation

2. Bitwise operation.

Arithmetic Operation:

we can use addition-deduction and multiply-division method to solve the problem.

let’s see how addition – deduction method works.

<?php

$x = 10;

$y = 20;
$x = $x+$y;
$y = $x – $y;
$x = $x – $y;
?>

it is also very simple, isn’t it. first we are adding the two numbers and keeping the result in one of the variable. in the above example its $x and it contains $x+$y. in the second operation, we are deducting $y from $x and assigning it to $y. now lets break the equation logically

$y = $x – $y => $x + $y – $y => $x  // [ since $x = $x+$y ]

cool, we swapped one of the variable. and following the same operation we are swapping the second one in the third line. so that’s it. It is so easy but just requires some logical thinking. do not try to memorize it rather than understand the simple logic here.

now the multiply-division method:

<?php

$x = 10;

$y = 20;
$x = $x*$y;
$y = $x / $y;
$x = $x / $y;
?>

it is same as the addition-deduction method and logic is similar.

now do we see any problem in the above logic? that is the question i ask if someone can answer the question correctly and show me the above solution. 🙂

the answer for that is very simple, it is the number overflow as we always have upper limit for integers. so when we add or multiply two big numbers then the result can cause overflow and might not show desired results.

so what can be a good solution with keeping the overflow fact in mind? the answer is bitwise operation

Bitwise operation:

in order to swap two values we can use the XOR bitwise operator.

Note: A bitwise exclusive or takes two bit patterns of equal length and performs the logical XOR operation on each pair of corresponding bits. The result in each position is 1 if the two bits are different, and 0 if they are the same

the algorithm is

x = x XOR y

y = x XOR y

x = x XOR y

if we write them in computer language using caret (^) then it will look like

x = x ^ y

y = x ^ y

x = x ^ y

in shorter version:

x ^= y

y ^= x

x ^= y

in one line

x ^= y ^= x ^= y

so here is the command in php:

$x ^= $y ^= $x ^= $y

now how it works:

so now we know how to swap two values without using a third variable. ask me any questions you have 🙂

PHP ini issues for scripts running in CLI or executing shell_exec() in *nix

Did you try to run php from command line and faced with php exception like safe mood is on, memory limit exceeded, maximum execution time exceeded etc. A general solution is to edit the php.ini and made those changes and restart apache server. At least i found these solutions online 🙂

But that is not gonna solve your problem. when you are running CLI or shell_exec in *nix environment, php uses a different php.ini file located inside CLI folder. until you edit that the problem wont be solved. So if you are constantly getting errors after altering your php.ini file used by your web server then you should try your CLIs’ php.ini file for any issues regarding cron/CLI.

Bangla Currency (Taka) Formatting

Today i wrote a simple code to convert a number to Bangladeshi currency (taka) format. the php format_number puts a comma (“,”) after every 3 digits which is not same for Bangladeshi currency. here is the simple function to do the job for yo. it can handle integer and decimal numbers.

function taka_format($amount = 0)
{
$tmp = explode(“.”,$amount); // for float or double values
$strMoney = “”;
$divide = 1000;
$amount = $tmp[0];
$strMoney .= str_pad($amount%$divide,3,”0″,STR_PAD_LEFT);
$amount = (int)($amount/$divide);
while($amount>0)
{
$divide = 100;
$strMoney = str_pad($amount%$divide, 2,”0″,STR_PAD_LEFT).”,”.$strMoney;
$amount = (int)($amount/$divide);
}

if(substr($strMoney, 0, 1) == “0”)
$strMoney = substr($strMoney,1);

if(isset($tmp[1])) // if float and double add the decimal digits here.
{
return $strMoney.”.”.$tmp[1];
}
return $strMoney;
}

an alternate way of doing this is via substr

function taka_format($amount = 0)
{
$tmp = explode(“.”,$amount);  // for float or double values
$strMoney = “”;
$amount = $tmp[0];
$strMoney .= substr($amount, -3,3 ) ;
$amount = substr($amount, 0,-3 ) ;
while(strlen($amount)>0)
{
$strMoney = substr($amount, -2,2 ).”,”.$strMoney;
$amount = substr($amount, 0,-2 );
}

if(isset($tmp[1]))         // if float and double add the decimal digits here.
{
return $strMoney.”.”.$tmp[1];
}
return $strMoney;
}

Code Igniter & maani.us

Many of us feels that a report without a graph is totally incomplete. I also agree with it completely. We need to represent the data in reports to be meaningful and also representable. A table with rows of data does not give the reader a clear idea about what is actually happening. So it is better to show a chart or a graph to the user before showing the long tables of data.

In PHP, one of the most popular graph tool is php/swf charts from maani.us. This tool provides some cool looking graphs with easy to integrate with your application. The integration becomes much easier if you are using Oode Igniter. In this article I am going to describe you, how to integrate maani.us php/swf charts with your Code Igniter application.

Detail Steps to Install the application:

1. download php/swf charts from http://www.maani.us

2. Create a directory named charts or any appropriate name in your root folder (not inside the Code Igniter system folder). it is better if you keep the CI system folder and charts folder on same level.

3. put the unzipped content (except the charts.php) file of the downloaded file in this folder.

Writing Code Igniter code:

You can integrate the maani.us code by either creating a library in Code Igniter or by simply creating a helper. I am going to show you the process by creating a helper file. In order to create a helper file in Code Igniter, you can either create a helper file in system\helpers or system\application\helpers file. In the later case you have to create the directory as Code Igniter does not create it by default. It is always better to keep the core files and folders of Code Igniter to be intact. Code Igniter gives you the flexibility to extend any of its helpers, libraries, plugins etc to be extended from the application folder.

here are the detail steps for creating and showing graph in a view:

1. first create a helper named graph_helper.php and copy paste the content of charts.php in this file. save the file.

2. now load the helper file from your controller where you want to show the graph. there are two parts in the helper file. One for inserting the chart and in order to show a chart, we need to define the data and chart properties. the SendChartData function defines the properties of the chart and InsertChart shows the Data. in InsertChart function we have to define few things

  • location of the flash file (.swf) , in our case in the charts folder in base directory.
  • location of the charts library, in our case in the charts folder in base directory.
  • PHP source location for ChartData or SendChartData function, in our case a controller.
  • width, height, background color, transparency and license information

here is the code inside a controller looks like.

$this->load->helper(“graph”);
$url = site_url().”/reports/showsummary/”;
$charturl = base_url().”/charts/charts.swf”;
$chartlib = base_url().”/charts/charts_library”;

$data[‘chart’] = InsertChart( $charturl, $chartlib, $url ,650,300,”cccccc” ); // passing the chart information to view.

$this->load->view(“reports/dashboard”, $data);
now we have to define the controller which will send the data for the graph.

function showsummary()

{

$this->load->helper(“graph”);
$chart[ ‘axis_category’ ] = array ( ‘font’=>”arial”, ‘bold’=>true, ‘size’=>16, ‘color’=>”000000″, ‘alpha’=>60, ‘skip’=>0 ,’orientation’=>”vertical” );
$chart[ ‘axis_ticks’ ] = array ( ‘value_ticks’=>false, ‘category_ticks’=>false, ‘major_thickness’=>2, ‘minor_thickness’=>1, ‘minor_count’=>1, ‘major_color’=>”222222″, ‘minor_color’=>”222222″ ,’position’=>”centered” );
$chart[ ‘axis_value’ ] = array ( ‘font’=>”arial”, ‘bold’=>true, ‘size’=>10, ‘color’=>”000000″, ‘alpha’=>50, ‘steps’=>6, ‘prefix’=>””, ‘suffix’=>””, ‘decimals’=>0, ‘separator’=>””, ‘show_min’=>true );

$chart[ ‘chart_border’ ] = array ( ‘color’=>”000000″, ‘top_thickness’=>0, ‘bottom_thickness’=>0, ‘left_thickness’=>5, ‘right_thickness’=>0 );

$chart[ ‘chart_grid_h’ ] = array ( ‘alpha’=>10, ‘color’=>”000000″, ‘thickness’=>0 );
$chart[ ‘chart_grid_v’ ] = array ( ‘alpha’=>10, ‘color’=>”000000″, ‘thickness’=>20 );
$chart[ ‘chart_rect’ ] = array ( ‘x’=>50, ‘y’=>50, ‘width’=>540, ‘height’=>200, ‘positive_color’=>”ffffff”, ‘positive_alpha’=>30, ‘negative_color’=>”ff0000″, ‘negative_alpha’=>10 );
$chart[ ‘chart_type’ ] = “line”;

$chart[ ‘chart_data’ ] = array ( array ( “”, “2004”, “2005”, “2006”, “2007” ), array ( “region 1”, 48, 55, 80, 100 ), array ( “region 2”, -12, 10, 55, 65 ), array ( “region 3″, 27, -20, 15, 80) );

$chart[ ‘chart_value’ ] = array ( ‘prefix’=>””, ‘suffix’=>””, ‘decimals’=>0, ‘separator’=>””, ‘position’=>”cursor”, ‘hide_zero’=>true, ‘as_percentage’=>false, ‘font’=>”arial”, ‘bold’=>true, ‘size’=>12, ‘color’=>”000000″, ‘alpha’=>100 );
$chart[ ‘draw’ ] = array ( array ( ‘type’=>”text”, ‘color’=>”000033″, ‘alpha’=>25, ‘font’=>”arial”, ‘rotation’=>0, ‘bold’=>true, ‘size’=>30, ‘x’=>0, ‘y’=>5, ‘width’=>650, ‘height’=>295, ‘text’=>”Statistics”, ‘h_align’=>”right”, ‘v_align’=>”bottom” ) );
$chart[ ‘legend_label’ ] = array ( ‘layout’=>”horizontal”, ‘font’=>”arial”, ‘bold’=>true, ‘size’=>13, ‘color’=>”000000″, ‘alpha’=>50 );
$chart[ ‘legend_rect’ ] = array ( ‘x’=>25, ‘y’=>10, ‘width’=>600, ‘height’=>5, ‘margin’=>3, ‘fill_color’=>”ffffff”, ‘fill_alpha’=>10, ‘line_color’=>”000000″, ‘line_alpha’=>0, ‘line_thickness’=>0 );
$chart[ ‘series_color’ ] = array ( “ddaa41”, “88dd11”, “4e62dd”, “ff8811”, “8A2BE2”, “FF1493”, “F0E68C”, “DDA0DD”,”8B4513″,”FFFF00″,”FF6347″,”708090″,”B0E0E6″,”808000″, “20B2AA”, “FF8C00”, “338833” );
$chart[ ‘series_gap’ ] = array ( ‘bar_gap’ => 0, ‘set_gap’ => 35 );

SendChartData ($chart);
}

You can copy paste this part from maani.us graph examples according to your graph choice. Just copy the first line

$this->load->helper(“graph”);

when you run the changes, you will see that the graphs are generated and it is shown in the view as expected. I have shown how to put a chart in a variable and pass it to view. It is a good way to show more than one graph if required.

I hope that shows how easily you can add a cool featured graph tool in your Code Igniter application. Let me know any comments, questions regarding Code Igniter and PHP.