# Barby 0.1 - Ruby barcode generator that doesn't suck (that much)

**URL:** <https://www.ruby-forum.com/t/barby-0-1-ruby-barcode-generator-that-doesnt-suck-that-much/135389>\
**Category:** Ruby\
**Created:** [April 21, 2008, 8:11pm UTC](https://www.ruby-forum.com/t/barby-0-1-ruby-barcode-generator-that-doesnt-suck-that-much/135389 "2008-04-21T20:11:41Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tore\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecb155/32.png) [@Tore\_D](https://www.ruby-forum.com/u/Tore_D)\
**Post date:** [April 21, 2008, 8:11pm UTC](https://www.ruby-forum.com/t/barby-0-1-ruby-barcode-generator-that-doesnt-suck-that-much/135389/1 "2008-04-21T20:11:41Z")

</div>

This is something I released a while ago, but I thought I’d announce  
it here too, as I’m sure not everyone in the target group reads my  
blog.

Barby is a pure Ruby barcode generator. It doesn’t rely on 3rd party  
libraries (well, unless you want it to do anything useful) and it’s  
easily extensible. It currently supports the following symbologies:

- Code128
- GS1128 (aka EAN/UCC-128)
- EAN-13 (aka UPC-A)
  - Bookland
  - EAN-8

- Code39

The logic for generating graphical representations of the barcodes is  
split into “outputters”. There are currently two outputters, one that  
uses RMagick and another that annotates a PDF/Writer document, and  
creating new ones is easy.

Some sample code:

require ‘barby’  
require ‘barby/outputter/rmagick\_outputter’

barcode = Barby::Code128B.new(‘IM-IN-UR-BARCODE’)

File.open(‘test.png’, ‘w’) do |f|  
f.write barcode.to\_png  
end

Barby is available as a gem (gem install barby) and from

> **[GitHub - toretore/barby: The Ruby barcode generator](https://github.com/toretore/barby/tree/master)**
>
> master

If you feel like adding support for more symbologies, let me know and  
I’ll put it into the “official” repo. If you spec it too, that’s  
doubly awesome.

---

<div class="post-metadata">

**Author:** ![Tore\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecb155/32.png) [@Tore\_D](https://www.ruby-forum.com/u/Tore_D)\
**Post date:** [April 22, 2008, 12:14pm UTC](https://www.ruby-forum.com/t/barby-0-1-ruby-barcode-generator-that-doesnt-suck-that-much/135389/2 "2008-04-22T12:14:32Z")

</div>

On Tue, Apr 22, 2008 at 4:05 AM, Tore D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> barcode = Barby::Code128B.new(‘IM-IN-UR-BARCODE’)  
> File.open(‘test.png’, ‘w’) do |f|  
> f.write barcode.to\_png  
> end

Heh… I just showed this to a work associate of mine who has been  
writing barcodes out directly to an epson printer comm spec.

He almost fell over in shock.

I love Ruby 🙂

By the way:

baci:~ mikel$ date  
Tue Apr 22 20:12:57 EST 2008  
baci:~ mikel$ sudo gem install barby  
Password:  
ERROR: could not find barby locally or in a repository

Maybe still flowing into the gem servers?

Mikel

---

<div class="post-metadata">

**Author:** ![Tore\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecb155/32.png) [@Tore\_D](https://www.ruby-forum.com/u/Tore_D)\
**Post date:** [April 23, 2008, 10:11am UTC](https://www.ruby-forum.com/t/barby-0-1-ruby-barcode-generator-that-doesnt-suck-that-much/135389/3 "2008-04-23T10:11:33Z")

</div>

On Apr 22, 12:13 pm, Mikel L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Maybe still flowing into the gem servers?
> 
> Mikel

Hm, it’s a while now since I uploaded it to RF, so it should work.  
This is my first gem on RF, so I might not have done everything right,  
but it works for me now, and has before too.. Hopefully (or, hopefully  
not, probably) it’s just some sort of server hickup.

The gem file is here:  
[http://rubyforge.org/frs/?group\_id=5831&release\_id=20294](http://rubyforge.org/frs/?group_id=5831&release_id=20294)

Tore

---

<div class="post-metadata">

**Author:** ![Tore\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecb155/32.png) [@Tore\_D](https://www.ruby-forum.com/u/Tore_D)\
**Post date:** [April 23, 2008, 9:08pm UTC](https://www.ruby-forum.com/t/barby-0-1-ruby-barcode-generator-that-doesnt-suck-that-much/135389/4 "2008-04-23T21:08:59Z")

</div>

Does it work with PDF ??? how ??

I try to run this

require ‘barby’  
require ‘barby/outputter/rmagick\_outputter’

barcode = Barby::Code128B.new(‘IM-IN-UR-BARCODE’)

File.open(‘test.png’, ‘w’) do |f|  
f.write barcode.to\_png  
end

but ruby says me that  
barby.rb:8: uninitialized constant Barby::Code128B (NameError)  
what I doing wrong??

Tore D. wrote:

> On Apr 22, 12:13 pm, Mikel L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Maybe still flowing into the gem servers?
> > 
> > Mikel
> 
> Hm, it’s a while now since I uploaded it to RF, so it should work.  
> This is my first gem on RF, so I might not have done everything right,  
> but it works for me now, and has before too.. Hopefully (or, hopefully  
> not, probably) it’s just some sort of server hickup.
> 
> The gem file is here:  
> [http://rubyforge.org/frs/?group\_id=5831&release\_id=20294](http://rubyforge.org/frs/?group_id=5831&release_id=20294)
> 
> Tore

---

<div class="post-metadata">

**Author:** ![Tore\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecb155/32.png) [@Tore\_D](https://www.ruby-forum.com/u/Tore_D)\
**Post date:** [April 25, 2008, 8:35pm UTC](https://www.ruby-forum.com/t/barby-0-1-ruby-barcode-generator-that-doesnt-suck-that-much/135389/5 "2008-04-25T20:35:52Z")

</div>

Please somebody can help me , I’m new with ruby , can anybdy tell me  
how use barby with PDF’s documents ?

Thanks

---

<div class="post-metadata">

**Author:** ![Tore\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecb155/32.png) [@Tore\_D](https://www.ruby-forum.com/u/Tore_D)\
**Post date:** [June 13, 2008, 8:15am UTC](https://www.ruby-forum.com/t/barby-0-1-ruby-barcode-generator-that-doesnt-suck-that-much/135389/6 "2008-06-13T08:15:45Z")

</div>

Foreero I. wrote:

> Please somebody can help me , I’m new with ruby , can anybdy tell me  
> how use barby with PDF’s documents ?

It’s not the only way, but if you’re using Ruby FPDF, I’ve written a  
small extension that can print Barby codes into FPDF-generated PDFs:

[http://rca.vmk.zhdk.ch/blog/software/](http://rca.vmk.zhdk.ch/blog/software/)

Or straight in svn:

[http://code.zhdk.ch/svn/leihs/trunk/lib/fpdf/fpdf\_barby.rb](http://code.zhdk.ch/svn/leihs/trunk/lib/fpdf/fpdf_barby.rb)

The next feature I’ll add is that you can specify the maximum width of  
barcodes, as some applications apparently require this.

The code is not very Rubyish as you’ll see, but we can’t really help it.  
With Ruby FPDF we’re stuck in a world where Ruby speaks PHP, so which  
way around should an FPDF extension think? This one is messed up in its  
head and tries to do both, with variables in all sorts of casings. I’ll  
have to clean that up. Also, some more error handling would be nice.

We’ll be starting to use this in production with several ten thousand  
barcodes in Code 128A and Code 39, because we soon need to put together  
a batch labeling feature for our inventory system, so I’m sure it’ll  
improve a bit then 🙂

Let me know how and if it works for you, against all odds.

Ramon

---

<div class="post-metadata">

**Author:** ![Tore\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecb155/32.png) [@Tore\_D](https://www.ruby-forum.com/u/Tore_D)\
**Post date:** [July 12, 2008, 6:25pm UTC](https://www.ruby-forum.com/t/barby-0-1-ruby-barcode-generator-that-doesnt-suck-that-much/135389/7 "2008-07-12T18:25:51Z")

</div>

On Jun 13, 8:15 am, Ramon C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Foreero I. wrote:
> 
> > Please somebody can help me , I’m new with ruby , can anybdy tell me  
> > how use barby with PDF’s documents ?
> 
> It’s not the only way, but if you’re using Ruby FPDF, I’ve written a  
> small extension that can print Barby codes into FPDF-generated PDFs:

Also, if you’re using PDF::Writer, there’s built-in support for that:

require ‘barby’  
require ‘barby/outputter/pdfwriter\_outputter’

pdf = PDF::Writer.new  
barcode = Barby::Code128B.new(‘humbaba’)  
barcode.annotate\_pdf(pdf)

The CairoOutputter can generate PDFs from scratch if you for some  
reason want that:

require ‘barby/outputter/cairo\_outputter’

File.open(‘test.pdf’){|f| f \<\< barcode.to\_pdf }

In the git repo there’s an experimental PrawnOutputter that can both  
annotate and generate PDFs using Prawn.

Tore

---

<div class="post-metadata">

**Author:** ![Tore\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecb155/32.png) [@Tore\_D](https://www.ruby-forum.com/u/Tore_D)\
**Post date:** [December 19, 2008, 12:48am UTC](https://www.ruby-forum.com/t/barby-0-1-ruby-barcode-generator-that-doesnt-suck-that-much/135389/8 "2008-12-19T00:48:30Z")

</div>

Hate to resurrect an old thread, but I got this to work with Prawn, and  
it’s  
pretty slick.  
(This is in a prawnto/Rails view)

require ‘barby’  
require ‘barby/outputter/prawn\_outputter’

pdf.bounding\_box [450,700], :width =\> 100 do  
barcode = Barby::Code39.new(@order.order\_identifier)  
barcode.annotate\_pdf(pdf, :height =\> 30)  
end

Plops a nice vector barcode right there. Really slick!

Kevin

---

<div class="post-metadata">

**Author:** ![Tore\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecb155/32.png) [@Tore\_D](https://www.ruby-forum.com/u/Tore_D)\
**Post date:** [February 7, 2011, 7:37pm UTC](https://www.ruby-forum.com/t/barby-0-1-ruby-barcode-generator-that-doesnt-suck-that-much/135389/9 "2011-02-07T19:37:19Z")

</div>

To whoever wrote this application/extension, I’d like to express my  
gratitude. I’m facing an issue where barcodes could be the solution…  
especially how these barcodes can be outputted to PDF is hopeful to me,  
so I’m looking forward putting it to the test… anyway, without your  
collective efforts, I’d had little to test, so thanks a bunch.

Regards,  
A.

---

<div class="post-metadata">

**Author:** ![Tore\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecb155/32.png) [@Tore\_D](https://www.ruby-forum.com/u/Tore_D)\
**Post date:** [February 8, 2011, 3:52am UTC](https://www.ruby-forum.com/t/barby-0-1-ruby-barcode-generator-that-doesnt-suck-that-much/135389/10 "2011-02-08T03:52:03Z")

</div>

On Tue, Apr 22, 2008 at 2:05 AM, Tore D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> This is something I released a while ago, but I thought I’d announce

Hi, you mentioned a ver 0.1 but we got a ver0.4.2. Are they the same  
gem?

thanks for barby (very nice name btw)  
best regards -botp
