Friday, January 23, 2015

PHP: EAN-13 Barcode generation library with Open Source license

As mentioned in my "Just Started" post, my first technical blog post is about generating EAN-13 Barcodes using a php library with Open Source license.

Recently I was allocated to a project which requires generating EAN-13 barcode. I didn't have experience in barcode generation previously. So I did a research in Internet. If you search in Google it gives you plenty of answers, but the problem I had was I wanted an "EAN-13" barcode generating library in PHP which is under LGPL license. Out of the solutions I found, below is the library that worked for me without any issues.

Library: TCPDF

  • This library supports both PDF & barcode generation.
  • This has nice & simple examples in it. For barcode generation, check under "Barcodes" section.

Steps I followed to generate an EAN 13 barcode

  1. Downloaded the tcpdf_6_2_4.zip (16.7 MB) & extracted it.
  2. Copied the extracted "tcpdf" folder to my web root directory (eg. /var/www/tcpdf)
  3. Opened the project in IDE (eg. Netbeans)
  4. Opened /var/www/tcpdf/tcpdf/examples/barcodes/example_1d_html.php & replaced 'http://www.tcpdf.org' by my preferred EAN 13 barcode ("1234567890128", Note: Make sure you give a valid EAN 13 barcode number)
  5. Necessary code snippet
  6. <?php
    // include 1D barcode class (search for installation path)
    require_once(dirname(__FILE__).'/tcpdf_barcodes_1d_include.php');
    // set the barcode content and type
    $barcodeobj = new TCPDFBarcode("1234567890128", 'EAN13');
    // output the barcode as HTML object
    echo $barcodeobj->getBarcodeHTML(2, 30, 'black');
    ?>
  7. Accessed the php file from the browser (eg. http://localhost/tcpdf/tcpdf/examples/barcodes/example_1d_html.php) 

It nicely displays the barcode. I even read it using a barcode reader using an android phone application.
Note that it doesn't display the barcode number below the barcode content. If you need to display it, you could either use this example or you will have to extend the existing code & write the logic.

So, that's it! & I hope this helps :)
Feel free to share the blog post if you find it interesting & helpful for others...

PS: Coming up next post... "PHP: send multiple events to MS Outlook 2010 calendar through one email"

Just started

Without a long introduction, let me tell you quickly, in point form why I started writing this blog ;)
  • I work as a Software Engineer at OrangeHRM. 
  • All developers know how you get stuck in middle of some project's code due to the lack of knowledge in some area & spend time looking for a solution.
  • Finally when you find the solution, you realize that if the solution was clearly written somewhere you could have saved so much time.
  • So, there it goes.. Whenever I find something that I couldn't easily solve by searching in Internet, I'll post an article with the solution I came up with, hoping that someone will not have to go through a miserable time looking for a solution.
  • Mostly, my posts will be about programming solutions, but yes! I would love to share other interesting experiences I have had time to time.
  • At last, not least I used to love reading & writing when I was schooling. Let me see whether I can prove the saying "Old habits die hard" as it goes :)
So that's all. Welcome to Hasarangi's Blog!! Hope this will help you in simplifying your life... :)

PS: My first technical blog will come out soon. Hint: It will be about bar code generation.