spejstore-labelmaker/main.rb

127 lines
3.1 KiB
Ruby
Raw Normal View History

2017-05-28 00:53:46 +00:00
require 'rubygems'
require 'sinatra'
require 'rqrcode'
2017-05-28 00:53:46 +00:00
require 'prawn'
require 'prawn/measurements'
require 'prawn/qrcode'
require 'prawn-svg'
require 'color'
require 'excon'
require 'rmagick'
require 'json'
require 'zlib'
include Prawn::Measurements
# module Prawn
# module Text
# module Formatted #:nodoc:
# # @private
# class LineWrap #:nodoc:
# def whitespace()
# # Wrap by these special characters as well
# "&:/\\" +
# "\s\t#{zero_width_space()}"
# end
# end
# end
# end
# end
2017-09-25 11:37:13 +00:00
2017-05-28 00:53:46 +00:00
module Excon
class Response
def json!()
2017-09-25 11:37:13 +00:00
# Convenience function
2017-05-28 00:53:46 +00:00
JSON.parse(body)
end
end
end
2017-05-28 22:51:24 +00:00
BACKEND_URL = 'https://inventory.waw.hackerspace.pl/api/1/'
2017-05-28 00:53:46 +00:00
CODE_PREFIX = "HTTP://I/"
def api(uri)
Excon.get(BACKEND_URL + uri + "/").json!
end
def render_identicode(data, id, extent)
pts = [[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]]
4.times do |n|
color = Color::HSL.from_fraction((id % 6) / 6.0, 1.0, 0.3).html[1..6]
id /= 6
save_graphics_state do
soft_mask do
fill_color 'ffffff'
polygon = [pts[n], [0.5, 0.5], pts[n+1]].map{ |v| [v[0]*bounds.height, v[1]*bounds.height] }
fill_polygon(*(polygon))
end
print_qr_code data, stroke: false,
extent: extent, foreground_color: color,
pos: [bounds.left, bounds.top]
end
end
fill_color '000000'
end
def render_label(item_or_label_id, size = [89, 36])
item = api("items/#{item_or_label_id}")
2017-05-28 00:53:46 +00:00
pdf = Prawn::Document.new(page_size: size.map { |x| mm2pt(x) },
2017-05-28 22:51:24 +00:00
margin: [2, 2, 2, 6].map { |x| mm2pt(x) }) do
2017-05-28 00:53:46 +00:00
font_families.update("DejaVuSans" => {
normal: "fonts/DejaVuSans.ttf",
italic: "fonts/DejaVuSans-Oblique.ttf",
bold: "fonts/DejaVuSans-Bold.ttf",
bold_italic: "fonts/DejaVuSans-BoldOblique.ttf"
})
font 'DejaVuSans'
2017-05-28 22:51:24 +00:00
# Width of right side
rw = bounds.height/2
# Right side
bounding_box([bounds.right - rw, bounds.top], width: rw) do
print_qr_code CODE_PREFIX + item['short_id'], stroke: false,
2017-05-28 22:51:24 +00:00
foreground_color: '000000',
extent: bounds.width, margin: 0, pos: bounds.top_left
end
# Left side
bounding_box(bounds.top_left, width: bounds.width - rw) do
text_box item['name'],
2017-09-25 11:37:13 +00:00
size: 40, align: :center, valign: :center, width: bounds.width-10,
inline_format: true, overflow: :shrink_to_fit, disable_wrap_by_char: true
2017-05-28 22:51:24 +00:00
end
2017-05-28 00:53:46 +00:00
end
pdf.render
end
2017-05-28 20:25:38 +00:00
set :bind, '0.0.0.0'
2017-05-28 00:53:46 +00:00
get '/api/1/preview/:label.pdf' do
headers["Content-Type"] = "application/pdf; charset=utf8"
render_label params["label"]
end
get '/api/1/preview/:label.png' do
headers["Content-Type"] = "image/png"
img = Magick::ImageList.new()
img = img.from_blob(render_label(params["label"])){ self.density = 200 }.first
img.format = 'png'
img.background_color = 'white'
img.to_blob
end
post '/api/1/print/:label' do
temp = Tempfile.new('labelmaker')
temp.write(render_label(params["label"]))
temp.close
system("lpr -P DYMO_LabelWriter_450 #{temp.path}")
end