There must be a gem for thatSure enough, the barby gem. It allows you to output your barcode as png, gif, svg, pdf, etc...
In my case I generated png barcodes and included image references to those in my view files.
Sample code
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
# Gemfile, run bundle install after adding | |
gem 'barby' | |
gem 'chunky_png' | |
# some code to generate the png file using 3 of 9 barcode style | |
require 'barby' | |
require 'barby/barcode/code_39' | |
require 'barby/outputter/png_outputter' | |
barcode_value = "099999333" | |
full_path = "/somewhere/barcode_#{barcode_value}.png" | |
barcode = Barby::Code39.new(barcode_value) | |
File.open(full_path, 'w') { |f| f.write barcode.to_png(:margin => 3, :xdim => 2, :height => 55) } |
There is no pixel setting, it uses some other type of unit to determine size; using the xdim, margin and height options you can tweak the size, but I found that it was not very precise - for my use case it was good enough.
The generated barcode does not include the value, it is just the image which can be scanned using a barcode scanner, but it would be nice if there was an option to include the value below that. This is somewhat trivial to add in your view using html.
Overall I was really pleased with this gem!
Resources