CodeIgniter Labels Library

October 11th, 2010 - Posted by Steve Marks to PHP, Web Development.

CodeIgniter Labels Library

Generating labels on the web via an application is something I’ve come across numerous times in my career and never have I been able to find a reliable and versatile enough method for doing so. As a result I generated a library for the PHP framework CodeIgniter to handle the hundreds of different variations that labels come in and output them in a number of formats (Word, HTML and soon PDF). I’ve included a couple of quick samples below to demonstrate the kind of output that can be generated by using the library:

View Microsoft Word Labels Sample Output

View HTML Labels Sample Output

Download and Installing the Library

The library can be downloaded by clicking the link the below:

Download The Labels Library

The downloaded ZIP file includes one file, labels.php, which should be placed straight into the ‘libraries’ folder of your application directory.

Using the Library

Once you’ve placed the library into the ‘libraries’ folder you’re ready to begin using it. A quick example of a controller’s code that generates labels in MS Word format can be seen below:

	// Load the library
	$this->load->library('labels');
    	
	// Specify the format
	$config['format'] = 'word';
	// Specify the address layout, using HTML <br /> tags to determine line breaks
	// The elements listed here become the address array keys (see below)
	$config['layout'] = "first_name last_name<br />address_1<br />address_2<br />town<br />county<br />postcode";
	$this->labels->initialize($config);
        
	// List the addresses to used on the labels
	// Notice how the array keys correpond to the 'layout' element above
	$addresses = array(
            array(
                'first_name'=>'Steve',
                'last_name'=>'Marks',
                'address_1'=>'22 Sweet Avenue',
                'address_2'=>'Mardi Tres',
                'town'=>'Cheltenham',
                'postcode'=>'NY6 6TR'
            ),
            array(
                'first_name'=>'Bobbie',
                'last_name'=>'Marley',
                'address_1'=>'132 Reggae Lane',
                'address_2'=>'East Hunting',
                'town'=>'Northampton',
                'postcode'=>'NN2 5TR'
            ),
            array(
                'first_name'=>'James',
                'last_name'=>'Shack',
                'address_1'=>'23 Leapord Road',
                'address_2'=>'Oaklingbury',
                'town'=>'Cambridge',
                'postcode'=>'CB4 7YT'
            )
	);

	// Output the labels
	$this->labels->output($addresses);

As well as the ‘format’ and ‘labels’ configuration options we can specify a lot of other options to determine the size, font and layout of the labels as follows:

Option: format
Type: string
Default: ‘word’
Options: ‘word’, ‘html’ or ‘pdf’ (PDF support coming soon)
Description: The type of file to be produced

Option: layout
Type: string
Default: ”
Description: The layout for the label. Only ‘<br />’ tags are supported when using the ‘word’ format to specify line breaks, however standard HTML tags can used in ‘html’ format to adding bold and italic etc.

Option: labels_across
Type: integer
Default: 2
Description: The number of labels across each page in a horizontal direction

Option: labels_down
Type: integer
Default: 7
Description: The number of labels down each page in a vertical direction

Option: label_height
Type: decimal
Default: 3.81
Description: The height of each label, in centimetres

Option: label_width
Type: decimal
Default: 9.9
Description: The width of each label, in centimetres

Option: pitch_horizontal
Type: decimal
Default: 10.16
Description: The width in centimetres of the label plus, if required, the width of the space/margin to the right of each label

Option: pitch_vertical
Type: decimal
Default: 3.81
Description: The height in centimetres of the label plus, if required, the height of the space/margin underneath each label

Option: page_margin_top
Type: decimal
Default: 1.51
Description: The top page margin in centimetres. Not applicable for HTML output where you will have to adjust your print settings to obtain the required margins.

Option: page_margin_side
Type: decimal
Default: 0.47
Description: The left and right page margins in centimetres. Not applicable for HTML output where you will have to adjust your print settings to obtain the required margins.

Option: page_height
Type: decimal
Default: 29.69
Description: The height in centimetres of the page. Defaults to A4 size. Not applicable for HTML output where you will have to adjust your print settings to obtain the required paper type.

Option: page_width
Type: decimal
Default: 21
Description: The width in centimetres of the page. Defaults to A4 size. Not applicable for HTML output where you will have to adjust your print settings to obtain the required paper type.

Option: align_horizontal
Type: string
Default: ‘left’
Options: ‘left’, ‘center’ or ‘right
Description: Specifies the justification of the text within each label

Option: align_vertical
Type: string
Default: ‘center’
Options: ‘top’, center’ or ‘bottom
Description: Specifies the vertical alignment of the text within each label

Option: padding_left
Type: decimal
Default: 1
Description: The left padding in centimetres of each label

Option: padding_top
Type: decimal
Default: 0
Description: The top padding in centimetres of each label

Option: font_face
Type: string
Default: ‘Arial’
Description: The font family used for the labels

Option: font_size
Type: integer
Default: 12
Description: The font size in points used for the labels

I hope you find this library as useful as I have since building it. I’ll continue to develop it over time, however please leave any feedback, bugs or feature requests via the comments box below.

This entry was posted on Monday, October 11th, 2010 at 8:37 pm by +Steve Marks and is filed under PHP, Web Development. You can follow any responses to this entry through the RSS 2.0 feed.
Comments...

Fear not, we won't publish this

Comments (2)
  1. alan says:

    Thanks you so much for this library – it helped me a lot – I am grateful!

  2. Arie says:

    Wow, this is what I looking for. Thanks