Skip to content
Mattieu Gamache-Asselin edited this page Feb 14, 2016 · 13 revisions

This is a list of outputters included with Barby. Note that none of these are required by default, you'll have to require the ones you need.

  • xdim: The X dimension is the width of the narrowest bar in a barcode. Thicker bars are multiples of the xdim.
  • ydim: This is the same as xdim, but for the height of the blocks in a 2D barcode.
  • height: The height of the bars. (1D only, 2D uses ydim)
  • margin: The width of the quiet zone around the barcode.

http://rdoc.info/github/toretore/barby/Barby/PngOutputter

require 'barby/outputter/png_outputter'
blob = Barby::PngOutputter.new(barcode).to_png #Raw PNG data
File.open('barcode.png', 'wb'){|f| f.write blob }
#Convenience method
File.open('barcode2.png', 'wb'){|f| f.write barcode.to_png }

Options:

All sizes in pixels.

  • xdim: (default: 1)
  • ydim: (default: same as xdim)
  • height: (default: 100)
  • margin: (default: 10)

http://rdoc.info/github/toretore/barby/Barby/PrawnOutputter

require 'barby/outputter/prawn_outputter'
outputter = Barby::PrawnOutputter.new(barcode)
doc       = outputter.to_pdf(options)  #Creates a new `Prawn::Document` and annotates it

Options:

All values in PDF points.

  • x: The starting X position (left) where the barcode will be drawn. (default: same as margin)
  • y: The starting Y position (bottom) of the barcode to be drawn. (default: same as margin)
  • xdim: (default: 1)
  • ydim: (default: same as xdim)
  • height: (default: 50)
  • margin: (default: 0)
  • document: (to_pdf only) Passed to Prawn::Document.new.

http://rdoc.info/github/toretore/barby/Barby/PdfwriterOutputter

require 'barby/outputter/pdfwriter_outputter'
outputter = Barby::PDFWriterOutputter.new(barcode)
outputter.annotate_pdf(prawn_document)
barcode.annotate_pdf(doc)

Options:

All values in PDF points.

  • x: The starting X position (left) where the barcode will be drawn. (default: same as margin)
  • y: The starting Y position (bottom) of the barcode to be drawn. (default: same as margin)
  • xdim: Also used as ydim. (default: 1)

RMagick (PNG, GIF, JPEG)

http://rdoc.info/github/toretore/barby/Barby/RmagickOutputter

require 'barby/outputter/rmagick_outputter'
outputter = Barby::RmagickOutputter.new(barcode)
outputter.to_png
outputter.to_jpg
outputter.to_gif
outputter.to_image #Returns a Magick::Image
barcode.to_png #, etc.

Options:

All sizes in pixels.

  • xdim: (default: 1)
  • ydim: (default: same as xdim)
  • height: (default: 100)
  • margin: (default: 10)

Cairo (PNG, PS, EPS, PDF, SVG)

Uses Cairo to render to various formats.

http://rdoc.info/github/toretore/barby/Barby/CairoOutputter

require 'barby/outputter/cairo_outputter'
outputter = Barby::CairoOutputter.new(barcode)
outputter.to_png
outputter.to_ps
outputter.to_eps
outputter.to_pdf
outputter.to_svg
barcode.to_png #, etc.

Options:

All sizes in whatever Cairo uses.

  • xdim: Also used as ydim. (default: 1)
  • height: (default: 50)
  • margin: (default: 10)

Renders the barcode to an HTML table where each cell is given an "on" or "off" class. Note that it does not include any styling, so you'll have to add your own CSS. The table will consist of rows (just one for 1D) with one cell for each xdim. Each cell will have an "on" or "off" class.

http://rdoc.info/github/toretore/barby/Barby/HtmlOutputter

require 'barby/outputter/html_outputter'
outputter = Barby::HtmlOutputter.new(barcode)
outputter.to_html
barcode.to_html

If you're using Barby in a Ruby on Rails view, you may have to add the following:

outputter.to_html.html_safe
barcode.to_html.html_safe

Options:

  • class_name: A class name to be given to the table in addition to "barby-barcode".

Example:

<p>Thank you for your reservation with Ryan's Beard Airlines. Please print this confirmation page and bring it with you to the check-in.</p>

<% barcode = Barby::DataMatrix.new(reservation.number) %>
<%= barcode.to_html(:class_name => 'confirmation-barcode') %>

Result:

<p>Thank you for your reservation with Ryan's Beard Airlines. Please print this confirmation page and bring it with you to the check-in.</p>

<table class="barby-barcode confirmation-barcode">
  <tbody>
     <tr class="barby-row">
       <td class="barby-cell on"></td>
       <td class="barby-cell off"></td>
       <td class="barby-cell on"></td>
     </tr>
     <tr class="barby-row">
       <td class="barby-cell off"></td>
       <td class="barby-cell on"></td>
       <td class="barby-cell off"></td>
     </tr>
     <!-- And so on and so forth -->
  </tbody>
</table>

You can't set the width of a barcode because it might not fit within the size you set. The data determines the size, not the other way around. Instead, you set the xdim, which will vary the width of the barcode. In most outputters, you can read the calculated width if you need it:

>> barcode = Barby::Code128B.new('grouse')
=> grouse
>> outputter = Barby::PngOutputter.new(barcode)
=> #<Barby::PngOutputter:0x007f862a09a3f0 @barcode=grouse>

>> outputter.xdim
=> 1
>> outputter.width
=> 101
>> outputter.xdim = 2
=> 2
>> outputter.width
=> 202
>> outputter.full_width #Includes margin
=> 222
>> outputter.margin = 20
=> 20
>> outputter.full_width
=> 242