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 ="1234")
outputter =
outputter.xdim = 2'barcode.png','wb') {|f| f.write outputter.to_png }



Apparently in Rails you need to manually set the MIME-Type of attachments, otherwise their filesize, filetype and other Metadata are not properly recognized by the E-Mail client and images will not be displayed within the html e-mail.
In this case using the hash :mime_type and the corresponding value 'image/png' in the rails controller solved this problem:

attachments.inline["img.png"] = {:data =>'img.png'), :mime_type => 'image/png'}


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.

PNG transparency with IE 5.5 and IE 6

The transparency issues in IE 6 and lesser are a well-known problem. Tough the older versions are less used it is important to handle this problem if you want to make your website working in all versions. The problem occurs if you want to show a png-file with alpha transparency in IE 5.5 and 6. After creating your page, adding the png-image (with alpha transparency) and accessing it with IE 6 or less you will see that the transparency does not work. If you want to you can use to render your page on different versions of your browser (for example if you don't have a specific version of the Internet explorer) The goal is now to find a solution how to make the alpha transparency work for each version of the Internet Explorer.
Subscribe to PNG