-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlabels.rb
More file actions
125 lines (112 loc) · 3.24 KB
/
labels.rb
File metadata and controls
125 lines (112 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# labels.rb
require 'rubygems'
require 'sinatra'
require 'sinatra/url_for'
require 'prawn'
require 'prawn/measurement_extensions'
require 'json'
set :port, 5656
# read paper styles from json file
paper_styles = JSON.parse(File.read('./public/paper.json'))['paper_styles']
##
# get action for initial url
#
get '/' do
redirect '/0'
end
##
# get action to show the input form
#
get '/:paper' do
# TODO: change paperstyle
paper = params[:paper].to_i
erb :index,
locals: {
paper_styles: paper_styles,
paper: paper,
rows: paper_styles[paper]['rows'].to_i,
columns: paper_styles[paper]['columns'].to_i
}
end
##
# post action to generate the pdf
#
post '/:paper' do
# paper style
paper = params[:paper].to_i
# TODO: multiple paper_styles in json
margin = [
paper_styles[paper]['margin_top'].to_f.mm,
paper_styles[paper]['margin_right'].to_f.mm,
paper_styles[paper]['margin_bottom'].to_f.mm,
paper_styles[paper]['margin_left'].to_f.mm
]
height = paper_styles[paper]['height'].to_f.mm
width = paper_styles[paper]['width'].to_f.mm
size_standard = paper_styles[paper]['size_standard'].to_i
size_big = paper_styles[paper]['size_big'].to_i
rows = paper_styles[paper]['rows'].to_i
columns = paper_styles[paper]['columns'].to_i
# variable used for all cells
rotation = 0
post_params = params
# call prawn document generator
Prawn::Document.generate 'labels.pdf',
margin: margin,
page_size: paper_styles[0]['page_size'] do
(0..(rows - 1)).each do |row|
(0..(columns - 1)).each do |column|
size = size_standard
# POST parameters from form
orientation = post_params['orientation' + row.to_s + '_' + column.to_s]
cell = post_params['field' + row.to_s + '_' + column.to_s]
height_after_rotation = height
width_after_rotation = width
dx = 0.mm
dy = 0.mm
# get orientation from post parameters
case orientation
when 'vertical', 'verticalBig'
rotation = 90
# swap height and width due to rotation
height_after_rotation = width
width_after_rotation = height
# location adjustments due to rotation
# TODO: calculate from height and width
dx = 12.9.mm
dy = 12.9.mm
# uppercase and big fontsize fro verticalBig
if orientation == 'verticalBig'
cell = to_upcase_vertical(cell)
size = size_big
end
else
rotation = 0
end
# create the textbox
text_box cell,
at: [(column * width) + dx , ((row + 1) * height) + dy],
height: height_after_rotation,
width: width_after_rotation,
size: size,
align: :center,
valign: :center,
rotate_around: :center,
rotate: rotation,
style: :bold
end
end
end
# return the odf file as the correct type
send_file 'labels.pdf', type: 'application/pdf', disposition: 'inline'
end
##
# method to make a text uppercase and vertical
#
def to_upcase_vertical(cell_text)
text_vertical = ''
cell_text.split('').each do |letter|
text_vertical << "#{letter.upcase}\n"
end
return text_vertical
end