Topic: Generating barcodes with rails

I have tried barcode generator with gbarcode and it didn't work. my rails server hung up while converting. I would like to know if anyone can use this.

I've also tried someone's rails code that generates ean13 barcode. although it does generate the ean code, it can't not be scanned.(my barcode scanner works fine on books)

anyway...I also found a gem called barcode 0.2 by Clinton R. Nixon. I installed it, but I don't know how to apply that to rails app. can anyone teach me how?

here is it's Rdoc

class code39

Takes a string, uppercases it, and wraps it in asterisks. If any character is unavailable in Code 39 encoding, throws a vicious error.


# File barcode/code39.rb, line 70
   def initialize(text)
      text = text.upcase
      text.split(//).each do |char|
         if not @@encoding.has_key?(char)
            raise "Unencodable string."
      @text = "*#{text}*"

Public Instance methods
to_img(filename, width, height)
The overly complicated image generation code. Your barcode image width and height should be in pixels. The actual barcode will not be that width, instead it will be a multiple of your text string (plus two asterisks - @text) length * 14. White space will center the rest.


# File barcode/code39.rb, line 99
   def to_img(filename, width, height)
      bar_code_width = width - (width % ( @text.length * 14 ) )
      letter_width = bar_code_width / @text.length
      small_bar_width = letter_width / 14
      large_bar_width = ((7.0 / 3.0) * small_bar_width.to_f).to_i
      canvas =, height)
      gc =
      # Necessary to prevent massive blurry image nonsense.
      cur_x = (width - bar_code_width) / 2
      self.to_s.split(//).each do |char|
         if char.upcase == "B"
            gc.rectangle(cur_x, 0, cur_x + (char == "B" ? large_bar_width : small_bar_width), height)
         cur_x += char.match(/[BW]/) ? large_bar_width : small_bar_width

Outputs a string that represents the encoding. Not necessarily useful externally, but who knows?


# File barcode/code39.rb, line 84
   def to_s
      output =
      @text.split(//).each do |char|
         output += @@encoding[char] + " "
      return output

Re: Generating barcodes with rails

have u tried barcode-generator plugin to generate barcode images in your rails project

Last edited by anuj (2007-07-23 11:59:07)

Re: Generating barcodes with rails

i've totally redone my plugin, which now uses gbarcode and rmagick. Now you can generate barcode images in any format (png, jpg etc..) for any barcode encoding (code 39, code 128, EAN etc..)

check it out at

Last edited by anuj (2007-07-23 12:00:48)

Re: Generating barcodes with rails

you migh want to have a look at this solution: