Compare commits

...

4 Commits

Author SHA1 Message Date
radex 9993a6b7d9
dockerify 2024-01-30 20:20:21 +01:00
radex 794b5b501b
Clean up, envify, add IPP support 2024-01-30 18:14:09 +01:00
radex 8a45d727af
add DEBUG_JSON env 2024-01-30 15:02:50 +01:00
radex 43d123f956
Update backend URL and code prefix 2024-01-30 15:00:05 +01:00
4 changed files with 80 additions and 52 deletions

18
Dockerfile Normal file
View File

@ -0,0 +1,18 @@
FROM ruby:2.7
RUN apt-get update && \
apt-get install -y cups-ipp-utils && \
rm -rf /var/lib/apt/lists/*
# throw errors if Gemfile has been modified since Gemfile.lock
RUN bundle config --global frozen 1
RUN gem install bundler:1.17.2
WORKDIR /usr/src/app
COPY Gemfile Gemfile.lock ./
RUN bundle install
COPY . .
CMD bundle exec ruby main.rb

View File

@ -12,3 +12,11 @@ GET http://localhost:4567/api/1/preview/:label.pdf
POST http://localhost:4567/api/1/print/:label
where :label is a `spejstore` label.id or item.short_id
to test without spejstore running locally, pass:
```sh
LABELMAKER_DEBUG_JSON='{"short_id":"abcdef","name":"Some long test item","owner":"testowner"}' bundle exec ruby main.rb
```
Make sure to pass either `LABELMAKER_LOCAL_PRINTER_NAME` or `LABELMAKER_IPP_PRINTER_URL`. See top of `main.rb` for all env variables available.

29
ipptool-print-job.test Normal file
View File

@ -0,0 +1,29 @@
# Print a test page using print-job
{
# The name of the test...
NAME "Print file using Print-Job"
# The operation to use
OPERATION Print-Job
# Attributes, starting in the operation group...
GROUP operation-attributes-tag
ATTR charset attributes-charset utf-8
ATTR language attributes-natural-language en
ATTR uri printer-uri $uri
ATTR name requesting-user-name $user
ATTR mimeMediaType document-format $filetype
GROUP job-attributes-tag
ATTR integer copies 1
FILE $filename
# What statuses are OK?
STATUS successful-ok
STATUS successful-ok-ignored-or-substituted-attributes
# What attributes do we expect?
EXPECT job-id
EXPECT job-uri
}

77
main.rb
View File

@ -10,67 +10,32 @@ require 'excon'
require 'rmagick'
require 'json'
require 'zlib'
require 'shellwords'
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
BACKEND_URL = ENV.fetch('LABELMAKER_BACKEND_URL', 'https://inventory.hackerspace.pl/api/1/')
CODE_PREFIX = ENV.fetch('LABELMAKER_CODE_PREFIX', 'https://inventory.hackerspace.pl/')
module Excon
class Response
def json!()
# Convenience function
JSON.parse(body)
end
end
end
# NOTE:
# DYMO_LABEL_SIZE = [89, 36]
# ZEBRA_LABEL_SIZE = [100, 60]
LABEL_SIZE = JSON.parse(ENV.fetch('LABELMAKER_LABEL_SIZE', '[89, 36]'))
BACKEND_URL = 'https://inventory.waw.hackerspace.pl/api/1/'
CODE_PREFIX = "HTTP://I/"
# 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', '')
DEBUG_JSON = ENV["LABELMAKER_DEBUG_JSON"]
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
if DEBUG_JSON
JSON.parse(DEBUG_JSON)
else
JSON.parse(Excon.get(BACKEND_URL + uri + "/"))
end
fill_color '000000'
end
DYMO_LABEL_SIZE = [89, 36]
ZEBRA_LABEL_SIZE = [100, 60]
def render_label(item_or_label_id, size: DYMO_LABEL_SIZE)
def render_label(item_or_label_id, size: LABEL_SIZE)
item = api("items/#{item_or_label_id}")
pdf = Prawn::Document.new(page_size: size.map { |x| mm2pt(x) },
@ -131,5 +96,13 @@ post '/api/1/print/:id' do
temp = Tempfile.new('labelmaker')
temp.write(render_label(params["id"]))
temp.close
system("lpr -P DYMO_LabelWriter_450 #{temp.path}")
if not LOCAL_PRINTER_NAME.empty?
system("lpr -P #{LOCAL_PRINTER_NAME.shellescape} #{temp.path.shellescape}", exception: true)
elsif not IPP_PRINTER_URL.empty?
system("ipptool -v -tf #{temp.path.shellescape} -d filetype=application/octet-stream -I #{IPP_PRINTER_URL.shellescape} ipptool-print-job.test", exception: true)
else
status 404
return "No printer configured"
end
end