Generating a barcode image within a Rails application

For a sport charity event with participants signing up beforehand online I needed to generate a barcode representation as an png file within a Rails application.
1 answer

There are multiple barcode generator gems available, i decided on 'barby'
First I adding barby to the Gemfile file and using bundler to download and install said gem
Then I had to add the needed 'require' lines to controllers and views using barby. Code-128 is the type of barcode I needed to use.

require 'barby'
require 'barby/barcode/code_128'

Finally using the following lines of code I generated the barcode as a PNG and saved it into a file:

@barcode = Barby::Code128.new("1234")
outputter = Barby::PngOutputter.new(@barcode)
outputter.xdim = 2
File.open('barcode.png','wb') {|f| f.write outputter.to_png }

Taggings:

Comments

Not a trivial problem! The gem recommendation makes the answer very informative. Also, well done on providing the code how to run this thing. Nice Gem naming :D

Boian Velitchkov - Sun, 12/02/2018 - 22:16 :::