This gem is a Ruby FFI binding for the libzint barcode generation library.

See the documentation for a full API description.


By default ruby-zint first tries to use libzint installed on the system. If libzint can't be found or if it isn't a supported version, then the zint version, bundled into the gem, is compiled and used. Both install methods can be enforced by using --enable-system-libzint or --disable-system-libzint options, see below.

First install CMake with your package manager (e. g. apt install cmake).

Afterwards install the gem and force builtin libzint:

$ gem install ruby-zint -- --disable-system-libzint

With system libraries

Install the libzint binary with your package manager (e. g. apt install zint or perhaps brew install zint).

Other platforms require building from source.

NOTE: It is assumed that you are using libzint with the version 2.12.

Then install this gem and enforce system libzint:

$ gem install ruby-zint -- --enable-system-libzint


Include gem "ruby-zint" in your Gemfile. Optionally set the install option by running bundler like so:

bundle config build.ruby-zint --enable-system-libzint

PNG support

Please install libpng (e. g. apt install libpng-dev) before installing the gem if you want to use the PNG format.


require "zint"

barcode = "Test", symbology: Zint::BARCODE_CODE128)

# Export to file
barcode.to_file(path: "out.png")

# Export file to memory
barcode.to_memory_file(extension: ".png")

# Write from Bitmap to own canvas
require "chunky_png"
png =, bitmap.height, ChunkyPNG::Color::TRANSPARENT)
white = ChunkyPNG::Color("white")
black = ChunkyPNG::Color("black")

bitmap = barcode.to_bitmap
bitmap.pixels.each do |pixel|
  png.compose_pixel(pixel.x, pixel.y, (pixel.colour == "1") ? black : white)

# Use vector export
vec = "Test").to_vector
png =, vec.height.to_i, ChunkyPNG::Color::WHITE)
vec.each_rectangle do |rec|
  png.rect(rec.x.to_i, rec.y.to_i,
      rec.x.to_i+rec.width.to_i-1, rec.y.to_i+rec.height.to_i-1,
      ChunkyPNG::Color::BLACK, ChunkyPNG::Color::BLACK)

# See also manual:

# Use file as input
barcode = "/tmp/test.txt")
barcode.to_file(path: "out.png")

# Use of comfort classes

ean_barcode = "012345678912")
ean_barcode.to_file(path: "ean.png")

# Code128
code_128_barcode = "012345678912")
code_128_barcode.to_file(path: "code_128.png")


Bug reports and pull requests are welcome on GitHub at


The gem is available as open source under the terms of the MIT License.


The project is based on the work and ideas of Angel Pizarro's zint. I would also like to say a big thank you to Lars Kanis for his great contributions.