Created
August 3, 2011 15:39
-
-
Save mlc/1122932 to your computer and use it in GitHub Desktop.
quick barcode generation in Rails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# put into your Gemfile | |
gem 'chunky_png' | |
gem 'barby', :require => ['barby', 'barby/outputter/chunky_png_outputter'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%p | |
= image_tag(@barcode_path) | |
%br | |
%span.barcode_numer&= @obj.barcode_number |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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