Skip to content
dvodvo edited this page May 11, 2022 · 3 revisions

This page is dedicated of document used examples of Barby. If you have one, please add it here, and help the community.

In order to support both Code 39 and Code 39 Extended you can just use as follow

require 'barby/barcode/code_39'
require 'barby/outputter/png_outputter'
barcode39 = Barby::Code39.new(barcode_data) # Default value is false
barcode39_ext = Barby::Code39.new(barcode_data, true)

# And if you want it PNG style
File.open('barcode39.png', 'w'){|f| f.write barcode39.to_png }
File.open('barcode39_ext.png', 'w'){|f| f.write barcode39_ext.to_png }

This example is chosen as there is an assumption made by the gem on source data.
Assuming: Item has_many :barcodes

require 'barby/barcode/ean_13'

items.each do |item|  
  item_barcode = item.barcodes.first.barcode  
  barcode = Barby::EAN13.new(item_barcode[0...-1])  
  barcode.to_html.html_safe  
end

The library ean_13.rb specifies

#EAN-13 barcodes have 12 digits + check digit  
    FORMAT = /^\d{12}$/

Thus the data submitted should exclude the check digit. This library then calculates and produces the check digit.
Submitting 13 characters will result in a data not valid error.

As opposed to :

require 'barby/barcode/data_matrix'

matrix_barcode = Barby::DataMatrix.new(item_barcode) which accepts a 13 character input