Skip to content

Instantly share code, notes, and snippets.

@mlc
Created August 3, 2011 15:39
Show Gist options
  • Save mlc/1122932 to your computer and use it in GitHub Desktop.
Save mlc/1122932 to your computer and use it in GitHub Desktop.
quick barcode generation in Rails
# put into your Gemfile
gem 'chunky_png'
gem 'barby', :require => ['barby', 'barby/outputter/chunky_png_outputter']
%p
= image_tag(@barcode_path)
%br
%span.barcode_numer&= @obj.barcode_number
class YourController < ApplicationController
def index
@obj = # some useful object
# ... more code ...
@barcode_path = url_for(:action => :barcode, :number => @obj.barcode_number, :type => @obj.barcode_type, :only_path => true)
def barcode
case params[:type]
when "code39"
gen = Barby::Code39.new(params[:number])
# supporting more barcode types is similarly trivial
else
raise "unknown barcode type"
end
send_data gen.to_png(:height => 50), :type => "image/png", :disposition => "inline"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment