如何生成智能邮件®通过 PHP 的条形码 (OneCode)
我需要在 PDF 文档中提供 CASS 认证地址。这涉及使用经过认证的邮寄地址,并且需要使用条形码打印。
如何生成这样的条形码?是否存在已经执行此操作的 PHP 库?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
要记住的一件事是,CASS 认证地址的认证将在 95 天后过期。这意味着您需要至少每三个月重新认证您(或您客户的)数据库。否则,您将无法生成 IMB(智能邮件条形码),因为 IMB 由许多元素组成,其中包括 12 位投递点条形码。
我想您已经有一个地方可以验证您的地址,但是对于那些阅读了此 Stack Overflow 问题并且没有经过认证和标准化地址的人,您可以进行 Google 搜索并找到一些提供商。
为了充分披露,我是 SmartyStreets 的创始人。我们提供地址验证和验证,它会返回派生 IMB 的 12 位投递点条形码。我很乐意亲自回答您与地址验证和标准化相关的任何其他代码。
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.
tc-lib-barcode (https://github.com/tecnickcom/tc-lib-barcode) PHP 软件库允许您生成“IMB - 智能邮件条形码 - Onecode - USPS-B-3200" 条形码。
源代码完全符合 PSR-2 标准,可以使用 轻松添加到您的 PHP 项目中作曲家。
原始代码已从 TCPDF 移植和重构,并已在数十亿文档中使用。
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.
USPS 拥有库和帮助程序代码,您可以为所有主要平台下载。它们没有 PHP 类,但您可以通过
exec
或system
到操作系统来生成代码。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
orsystem
out to the OS to generate the code.我尝试了这里其他一些答案中的几种方法,但它们对我不起作用:
来自 USPS 的官方库和帮助程序代码不是很清楚。它们还依赖于平台,如果您计划在 Excel 或其他桌面应用程序中进行邮件合并,它们似乎最有用。如果您尝试与 PHP Web 应用程序集成,我不确定这是一条非常好的路线。
似乎tc-lib-barcode不使用官方USPS字体。当并排比较以及当我尝试使用 此工具验证条形码时,它看起来不正确,失败了。
我最终使用这个IMB转换器类来代替。您可以下载 USPS 官方字体 USPSIMBStandard,然后使用该字体显示您的条形码哈希值(它应该位于 fonts/scalable/trueType 文件夹中)。要生成条形码哈希,您首先需要生成代码。 此 PDF 的第 2 页对此进行了概述美国邮政局。一旦您有了代码(例如 0027012345678955555590210),您就可以通过转换器类运行它:
这将返回 TFDAAFAADDTDDFDFDATADDDATDAFAFDATADADADFATAFAFAFDADATFTFDAFDTADDT。然后,您可以使用 USPSIMBStandard 字体将其直接插入到 PDF 中,或以 HTML 形式显示。
生成条形码哈希值时,不清楚 IMB::Convert 与官方 USPS 编码器相比有多准确。我测试了几种组合,它们似乎都匹配。如果您确实想使用官方的 USPS 编码器,您可以在命令行上运行它,如下所示:
I tried a couple approaches from some of the other answers here and they didn't work for me:
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.
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:
This returns TFDAAFAADDTDDFDFDATADDDATDAFAFDATADADADFATAFAFAFDADATFTFDAFDTADDT. You can then inject this directly into a PDF using the USPSIMBStandard font, or display it in HTML.
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: