Wednesday, December 17, 2014

Reusable Header and Footer by extending TCPDF library in CodeIgniter

We use PDF to generate various kinds of reports and in most of the report we have fixed header and footer representing company’s information.

I will show you how to create a common header and footer function with CodeIgniter Library which extends Tcpdf library

Prerequisites:
  1. CodeIgniter Installed
  2. TCPDF library

To create a library in CodeIgniter, create a new php file under application/libraries folder. I will call it Pdfheaderfooter.php [first letter capital]. You can use whatever name you want.

Add tcpdf library where you have them added. I have added it under application/libraries folder. So in my Pdfheaderfooter.php file I will have below line:

require_once('tcpdf/tcpdf.php');
require_once('tcpdf/tcpdi.php');

If you do not have tcpdf library, you can get it from http://sourceforge.net/projects/tcpdf/files

Next is to create class that extends this library. Create a class with same name as php file:
class Pdfheaderfooter extends TCPDI {

}

Under this class you can define your header and footer functions:

<?php 
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require_once('tcpdf/tcpdf.php');
require_once('tcpdf/tcpdi.php');

class Pdfheaderfooter extends TCPDI {
 
  //Page header
  public function Header() {
    $html = 'Some Header';
 
    $this->SetFontSize(8);
    $this->WriteHTML($html, true, 0, true, 0);
  }
 
  // Page footer
  public function Footer() {
    // Position at 15 mm from bottom
    $this->SetY(-15);
    $html = 'Some Footer';
 
    $this->SetFontSize(8);
    $this->WriteHTML($html, true, 0, true, 0);
  }
}

/* End of file Pdfheaderfooter.php */


This will use custom header and footer defined in Pdfheaderfooter.php file when pdf is generated.

Now to generate a pdf with this custom header and footer, you need to load this library maybe in the controller.

$this->load->library('Pdfheaderfooter');

// create new PDF document
$pdf = new Pdfheaderfooter(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);


// set font
$pdf->SetFont('times', 'BI', 12);

// add a page
$pdf->AddPage();

// set some text to print
$txt = "This is some HTML"

// print a block of text using Write()
$pdf->WriteHTML(0, $txt, '', 0, 'C', true, 0, false, false, 0);

// ---------------------------------------------------------

//Close and output PDF document
$pdf->Output('example.pdf', 'I');

Friday, November 28, 2014

Use Drupal Location module to filter data based on Proximity


In this blog, I will show you how to filter data based on lat/long of content using Drupal Location Module.
This is helpful when you have scenario like you need to filter data within some miles of selected filter.
For e.g. If you have filter of City and you only need to show Hotels which are within 100 Miles from City center.
Pre requirement:
  1. Drupal 7 installed
  2. Ctools Module installed and enabled
  3. Views Module installed and enabled
  4. Views UI Module installed and enabled
  5. Location Module installed and enabled
  6. Location CCK Module installed and enabled

  • Add Location field to the content type in which you want to have location information.
    We will use Article content type which is available by default in Drupal 7.

To add Location field to article content type
  1. Login as Administrator
  2. Goto Structure >>  Content types
  3. Click edit under Operations column for Article
  4. Click Manage Fields on Top Right
  5. Enter Field name, here I have used “Article Location”
  6. Select Location under field type
  7. Click Save

 8. After clicking Save you will have Collection Settings screen. Select the appropriate option that you want in your content type. 
Lets select Coordinate Chooser to Allow and Click Save Settings.

  • Create some articles with below latitude/longitude
1. 40.077021/-74.413147
2. 40.094882/-74.43924
3. 40.102235/-74.436493
4. 40.114839/-74.42688
5. 40.113789/-74.392548
6. 40.104336/-74.380188
7. 36.73884/-73.994293
  • Create a VIEW to display Articles that we created above without any filter and later we will add Proximity filter in the view
  1. Login as Administrator
  2. Goto Structure >> Views
  3. Click Add new view
  4. Give any view name, here I am using List Article
  5. Click save & exit

If you visit http://{domain-name}/ list-article you will see all the articles that we recently created
Now lets explore proximity filter options in view
Edit the VIEW we recently created.
Click Add under Filter Criteria

Filter by Location under Filter Dropdown
Select Location: Distance/Proximity checkbox and click Apply (all displays) button

You will have Configure filter criterion screen.
Here you have different option under Origin.
1. Select Static Latitude/Longitude under Origin and use 40.098346/-74.412843 values for Latitude/Longitude.
2. Enter Distance 100 Miles.
3. Click Apply (all displays).
4. Click Save on right top.
If you revist the view page again http://{domain-name}/ ist-article you will only see 6 Articles intead of 7. You will not see article with lat/long 36.73884/-73.994293 as the distance between 40.098346/-74.412843 and 36.73884/-73.994293 is more than 100 miles.
Other option is “Use PHP code to determine latitude/longitude”. You can write PHP code in filter from admin without touching the code. This is very useful when you want dynamic filter based on user selection.

For more complex PHP code, you can define function in your module file and get lat/long from function defined in module file on which filter will be performed. For e.g. If you have a filter of city and you want all the hotels in that city within 100 miles, you can enter function name for e.g. “get_lat_long” under “PHP code for latitude, longitude” and pass latitude/longitude from function defined in your module file.
Click Apply (all displays)


Below is the reference function which can be in your .module file
function get_lat_long() {
        // $latitude = Get it from GET, POST or DB;
        // $longitude = Get it from GET, POST or DB;
        $array = array(
                'latitude' => (float) $latitude,
                'longitude' => (float) $longitude,
        );
    return $array;
}




Wednesday, August 13, 2014

Quick steps to set up LAMP in amazon linux os on amazon ec2

Quick Guide to set up LAMP in Amazon Linux OS on ec2
In this blog, I will explain quick steps to install LAMP on Amazon Linux OS.

Verify Access
Please check if logged in user can perform sudo commands.
e.g. sudo yum update –y

If not then, first you need to ask server admin to add user you are logged in, to /etc/sudoers file.

If you are able to fire sudo commands then you don’t need to worry and let us start with installation steps.

Install LAMP on empty Amazon ec2 server

1. sudo yum update -y

2. Install the Apache web server, MySQL, and PHP software packages
   sudo yum groupinstall -y "Web Server" "MySQL Database" "PHP Support"
   sudo yum install php-xml php-mcrypt php-mbstring php-cli

3. Install the php-mysql package.
   sudo yum install -y php-mysql

4. Start the Apache web server.
   sudo service httpd start

5. Use the chkconfig command to configure the Apache web server to start at each system boot.
   sudo chkconfig httpd on

6. Start the mysql server.
   sudo service mysqld start

7. Run mysql_secure_installation to setup root password
   sudo mysql_secure_installation
            a. By default, the root account does not have a password set, so press Enter.
            b. Type Y to remove the anonymous user accounts.
            c. Type Y to disable remote root login.
            d. Type Y to remove the test database.
            e. Type Y to reload the privilege tables and save your changes.

8. Use the chkconfig command to configure the Apache web server to start at each system boot.
   sudo chkconfig mysqld on

Install phpmyadmin

At the time of writing this, 4.2.7 was the latest version. you can change the latest version in the below url before installing
1. wget http://www.sourceforge.net/projects/phpmyadmin/files/phpMyAdmin/4.2.7/phpMyAdmin-4.2.7-all-languages.tar.bz2

2. sudo tar -jxf phpMyAdmin-4.0.3-all-languages.tar.bz2 -C /var/www/html

3. sudo mv /var/www/html/phpMyAdmin-4.0.3-all-languages /var/www/html/phpmyadmin

4. rm -rf phpMyAdmin-4.0.3-all-languages.tar.bz2

Now if you visit http://{domain_name}/phpmyadmin it should work

In case if it does not work check Allowoverride setting in /etc/httpd/conf/httpd.conf and change it to
Allowoverride all

If it still does not work, check /var/log/httpd/error_log

Thank you for referring my blog. Please share your suggestions and  feedback comments.

For more information: Solution Analysts