How to generate Intelligent Mail® Barcode (OneCode) via PHP

5.7k views Asked by At

I have a request to provide CASS certificated addresses on a PDF document. This involves using a certified mailing address, and needs to be printed using the barcode.

How can I generate such a barcode? Might exists PHP libraries that already does this?

4

There are 4 answers

0
charlesbridge On BEST ANSWER

The USPS has libraries and helper code you can download for all major platforms. They don't have a PHP class, but you could exec or system out to the OS to generate the code.

0
Jonathan Oliver On

One thing to remember is that certification on a CASS-certified address expires after 95 days. This means that you will need to recertify your (or your client's) database about every three months on a minimum. Without that, you can't generate an IMB (intelligent mail barcode) because the IMB is composed of a number of elements including the 12-digit delivery point barcode.

I would imagine that you already have a place to certify your addresses, but for those who read this Stack Overflow question and don't have certified and standardized addresses, you can do a Google search and find a handful of providers.

In the interest of full disclosure, I'm the founder of SmartyStreets. We offer address verification and validation which gives back the 12-digit delivery point barcode from which the IMB is derived. I would be happy to personally answer any additional codes you have related to address verification and standardization.

0
Alex Cook On

I tried a couple approaches from some of the other answers here and they didn't work for me:

  1. The official libraries and helper code from the USPS aren't very clear. They're also platform dependent and seem to be most useful if you're planning to do a mail merge in Excel or some other desktop app. If you're trying to integrate with a PHP web app, I'm not sure this is a very good route.

  2. It seems like tc-lib-barcode doesn't use the official USPS font. It doesn't look correct when compared side-by-side and when I tried validating a barcode using this tool, it failed.

I ended up using this IMB Converter class instead. You can download the USPS official font USPSIMBStandard and then display your bar code hash using the font (it should be in the fonts/scalable/trueType folder). To generate a bar code hash, you first need to generate a code. This is outlined on page 2 of this PDF from the USPS. Once you have a code (e.g. 0027012345678955555590210), you can run it through the converter class:

$barcode = IMB::Convert('0027012345678955555590210');

This returns TFDAAFAADDTDDFDFDATADDDATDAFAFDATADADADFATAFAFAFDADATFTFDAFDTADDT. You can then inject this directly into a PDF using the USPSIMBStandard font, or display it in HTML.

<p style="font-family:'USPSIMBStandard'; color:#000; font-size: 100px;"><?php echo $barcode; ?></p>

USPS Bar Code Example

When generating bar code hashes, it's unclear how accurate IMB::Convert is in comparison to the official USPS encoder. I tested a few combinations and they all seem to match. If you do want to use the official USPS encoder, you can run it on the command line like this:

$ IMBshell 0027012345678955555590210
0
Nicola Asuni On

The tc-lib-barcode (https://github.com/tecnickcom/tc-lib-barcode) PHP software library allows you to generate "IMB - Intelligent Mail Barcode - Onecode - USPS-B-3200" barcodes.

The source code is fully PSR-2 compliant and can be easily added to your PHP projects using Composer.

The original code has been ported and refactored from TCPDF and already used in billions of documents.