Skip to content

Instantly share code, notes, and snippets.

@hidakatsuya
Last active April 23, 2019 09:04
Show Gist options
  • Save hidakatsuya/1347095 to your computer and use it in GitHub Desktop.
Save hidakatsuya/1347095 to your computer and use it in GitHub Desktop.
ThinReports Example: Barcode
# coding: utf-8
require 'rubygems'
require 'thinreports'
# Load libraries of barby.
require 'barby'
require 'barby/barcode/ean_13'
require 'barby/barcode/ean_8'
require 'barby/barcode/qr_code'
require 'barby/outputter/png_outputter'
# NOTE:
# The following libraries are also required
# to use barby/outputter/png_outputter and barby/barcode/qr_code
#
# * chunky_png: http://rubygems.org/gems/chunky_png
# * rqrcode: http://rubygems.org/gems/rqrcode
def barcode(type, data, png_opts = {})
code = case type
when :ean_13
Barby::EAN13.new(data)
when :ean_8
Barby::EAN8.new(data)
when :qr_code
Barby::QrCode.new(data)
end
image = StringIO.new(code.to_png(png_opts))
image.respond_to?(:set_encoding) ? image.set_encoding('UTF-8') : image
end
ThinReports::Report.generate_file('barcode.pdf', :layout => 'barcode') do
start_new_page
# JAN13
page.item(:jan_13).src(barcode(:ean_13, '491234567890'))
# JAN8
page.item(:jan_8).src(barcode(:ean_8, '4512345'))
# QR Code
page.item(:qr_code).src(barcode(:qr_code, 'http://www.thinreports.org/',
:ydim => 5, :xdim => 5))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment