Prepare for zebra, add owner name if present

master
radex 2020-05-21 00:33:57 +02:00 committed by Piotr Dobrowolski
parent c43b83282a
commit b49426a9df
1 changed files with 19 additions and 10 deletions

29
main.rb
View File

@ -67,7 +67,10 @@ def render_identicode(data, id, extent)
fill_color '000000'
end
def render_label(item_or_label_id, size = [89, 36])
DYMO_LABEL_SIZE = [89, 36]
ZEBRA_LABEL_SIZE = [100, 60]
def render_label(item_or_label_id, size: DYMO_LABEL_SIZE)
item = api("items/#{item_or_label_id}")
pdf = Prawn::Document.new(page_size: size.map { |x| mm2pt(x) },
@ -82,17 +85,23 @@ def render_label(item_or_label_id, size = [89, 36])
font 'DejaVuSans'
# Width of right side
rw = bounds.height/2
qr_size = [bounds.height / 2, 27].max
# Right side
bounding_box([bounds.right - rw, bounds.top], width: rw) do
bounding_box([bounds.right - qr_size, bounds.top], width: qr_size) do
print_qr_code CODE_PREFIX + item['short_id'], stroke: false,
foreground_color: '000000',
extent: bounds.width, margin: 0, pos: bounds.top_left
owner_text = item["owner"] ? "owner: #{item['owner']}\n\n" : ""
metadata_text = owner_text # todo: creation date?
text_box metadata_text,
at: [bounds.right - qr_size, -7], size: 8, align: :right, overflow: :shrink_to_fit
end
# Left side
bounding_box(bounds.top_left, width: bounds.width - rw) do
bounding_box(bounds.top_left, width: bounds.width - qr_size) do
text_box item['name'],
size: 40, align: :center, valign: :center, width: bounds.width-10,
inline_format: true, overflow: :shrink_to_fit, disable_wrap_by_char: true
@ -104,23 +113,23 @@ end
set :bind, '0.0.0.0'
get '/api/1/preview/:label.pdf' do
get '/api/1/preview/:id.pdf' do
headers["Content-Type"] = "application/pdf; charset=utf8"
render_label params["label"]
render_label params["id"]
end
get '/api/1/preview/:label.png' do
get '/api/1/preview/:id.png' do
headers["Content-Type"] = "image/png"
img = Magick::ImageList.new()
img = img.from_blob(render_label(params["label"])){ self.density = 200 }.first
img = img.from_blob(render_label(params["id"])){ self.density = 200 }.first
img.format = 'png'
img.background_color = 'white'
img.to_blob
end
post '/api/1/print/:label' do
post '/api/1/print/:id' do
temp = Tempfile.new('labelmaker')
temp.write(render_label(params["label"]))
temp.write(render_label(params["id"]))
temp.close
system("lpr -P DYMO_LabelWriter_450 #{temp.path}")
end