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'
|
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-30 17:14:09 +00:00
|
|
|
# NOTE: You can use either local printer or IPP printer, but not both
|
|
|
|
LOCAL_PRINTER_NAME = ENV.fetch('LABELMAKER_LOCAL_PRINTER_NAME', 'DYMO_LabelWriter_450')
|
|
|
|
IPP_PRINTER_URL = ENV.fetch('LABELMAKER_IPP_PRINTER_URL', '')
|
2017-05-28 00:53:46 +00:00
|
|
|
|
2024-01-30 23:04:55 +00:00
|
|
|
def render_label()
|
|
|
|
short_id = params[:id]
|
|
|
|
name = params[:name]
|
|
|
|
owner = params[:owner]
|
2017-05-28 00:53:46 +00:00
|
|
|
|
2024-01-30 23:04:55 +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
|
|
|
|
2024-01-30 23:04:55 +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
|
2020-05-20 22:33:57 +00:00
|
|
|
qr_size = [bounds.height / 2, 27].max
|
2017-05-28 22:51:24 +00:00
|
|
|
|
|
|
|
# Right side
|
2020-05-20 22:33:57 +00:00
|
|
|
bounding_box([bounds.right - qr_size, bounds.top], width: qr_size) do
|
2024-01-30 23:04:55 +00:00
|
|
|
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
|
2020-05-20 22:33:57 +00:00
|
|
|
|
2024-01-30 23:04:55 +00:00
|
|
|
owner_text = owner && !owner.empty? ? "owner: #{owner}\n\n" : ""
|
2020-05-20 22:33:57 +00:00
|
|
|
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
|
2020-05-20 22:33:57 +00:00
|
|
|
bounding_box(bounds.top_left, width: bounds.width - qr_size) do
|
2024-01-30 23:04:55 +00:00
|
|
|
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
|
|
|
|
|
2024-01-30 23:04:55 +00:00
|
|
|
get '/api/2/preview.pdf' do
|
2017-05-28 00:53:46 +00:00
|
|
|
headers["Content-Type"] = "application/pdf; charset=utf8"
|
2024-01-30 23:04:55 +00:00
|
|
|
render_label
|
2017-05-28 00:53:46 +00:00
|
|
|
end
|
|
|
|
|
2024-01-31 08:24:57 +00:00
|
|
|
post '/api/2/print' do
|
2017-05-28 00:53:46 +00:00
|
|
|
temp = Tempfile.new('labelmaker')
|
2024-01-30 23:04:55 +00:00
|
|
|
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
|
|
|
else
|
|
|
|
status 404
|
|
|
|
return "No printer configured"
|
|
|
|
end
|
2017-05-28 00:53:46 +00:00
|
|
|
end
|