spejstore-labelmaker/main.rb

106 lines
3.0 KiB
Ruby
Raw Normal View History

2017-05-28 00:53:46 +00:00
require 'rubygems'
require 'sinatra'
require 'prawn'
require 'prawn/measurements'
require 'prawn/qrcode'
require 'json'
2024-01-30 17:14:09 +00:00
require 'shellwords'
2024-02-01 17:13:49 +00:00
require 'uri'
require 'net/http'
2017-05-28 00:53:46 +00:00
include Prawn::Measurements
2024-01-30 17:14:09 +00:00
CODE_PREFIX = ENV.fetch('LABELMAKER_CODE_PREFIX', 'https://inventory.hackerspace.pl/')
# NOTE:
# DYMO_LABEL_SIZE = [89, 36]
# ZEBRA_LABEL_SIZE = [100, 60]
LABEL_SIZE = JSON.parse(ENV.fetch('LABELMAKER_LABEL_SIZE', '[89, 36]'))
2017-05-28 00:53:46 +00:00
2024-01-31 20:47:09 +00:00
# NOTE: You can use only one of these: local printer, IPP printer, or printservant
LOCAL_PRINTER_NAME = ENV.fetch('LABELMAKER_LOCAL_PRINTER_NAME', '')
2024-01-30 17:14:09 +00:00
IPP_PRINTER_URL = ENV.fetch('LABELMAKER_IPP_PRINTER_URL', '')
2024-02-01 17:13:49 +00:00
WEBHOOK = ENV.fetch('LABELMAKER_WEBHOOK', '') # printservant-compatible print url
if LOCAL_PRINTER_NAME.empty? and IPP_PRINTER_URL.empty? and WEBHOOK.empty?
raise "No printer configured"
end
2017-05-28 00:53:46 +00:00
def render_label()
short_id = params[:id]
name = params[:name]
owner = params[:owner]
2017-05-28 00:53:46 +00:00
if short_id.nil? or name.nil?
status 400
return "Missing required parameters ?id= and ?name="
end
2017-05-28 00:53:46 +00:00
pdf = Prawn::Document.new(page_size: LABEL_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
qr_size = [bounds.height / 2, 27].max
2017-05-28 22:51:24 +00:00
# Right side
bounding_box([bounds.right - qr_size, bounds.top], width: qr_size) do
print_qr_code CODE_PREFIX + short_id, stroke: false,
2017-05-28 22:51:24 +00:00
foreground_color: '000000',
extent: bounds.width, margin: 0, pos: bounds.top_left
owner_text = owner && !owner.empty? ? "owner: #{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
2017-05-28 22:51:24 +00:00
end
# Left side
bounding_box(bounds.top_left, width: bounds.width - qr_size) do
text_box 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'
2024-01-31 08:24:57 +00:00
get '/api/2/health' do
"I'm cool"
end
get '/api/2/preview.pdf' do
2017-05-28 00:53:46 +00:00
headers["Content-Type"] = "application/pdf; charset=utf8"
render_label
2017-05-28 00:53:46 +00:00
end
2024-01-31 08:24:57 +00:00
post '/api/2/print' do
2024-02-01 17:13:49 +00:00
if not WEBHOOK.empty?
uri = URI(WEBHOOK)
reponse = Net::HTTP.post(uri, render_label)
puts reponse.body
return
end
2017-05-28 00:53:46 +00:00
temp = Tempfile.new('labelmaker')
temp.write(render_label)
2017-05-28 00:53:46 +00:00
temp.close
2024-01-30 17:14:09 +00:00
if not LOCAL_PRINTER_NAME.empty?
2024-01-30 19:20:21 +00:00
system("lpr -P #{LOCAL_PRINTER_NAME.shellescape} #{temp.path.shellescape}", exception: true)
2024-01-30 17:14:09 +00:00
elsif not IPP_PRINTER_URL.empty?
2024-01-30 19:20:21 +00:00
system("ipptool -v -tf #{temp.path.shellescape} -d filetype=application/octet-stream -I #{IPP_PRINTER_URL.shellescape} ipptool-print-job.test", exception: true)
2024-01-30 17:14:09 +00:00
end
2017-05-28 00:53:46 +00:00
end