zebra-zpl 1.0.5 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +62 -0
  3. data/CONTRIBUTING.md +49 -0
  4. data/Gemfile +6 -0
  5. data/README.md +259 -76
  6. data/docs/example.rb +249 -0
  7. data/docs/images/barcode.png +0 -0
  8. data/docs/images/datamatrix.png +0 -0
  9. data/docs/images/earth.jpg +0 -0
  10. data/docs/images/graphics.png +0 -0
  11. data/docs/images/image.png +0 -0
  12. data/docs/images/image_inverted.png +0 -0
  13. data/docs/images/images.png +0 -0
  14. data/docs/images/justification.png +0 -0
  15. data/docs/images/qrcode.png +0 -0
  16. data/docs/images/rotation.png +0 -0
  17. data/docs/images/text.png +0 -0
  18. data/lib/zebra/print_job.rb +7 -13
  19. data/lib/zebra/zpl.rb +20 -13
  20. data/lib/zebra/zpl/barcode.rb +29 -12
  21. data/lib/zebra/zpl/barcode_type.rb +4 -1
  22. data/lib/zebra/zpl/box.rb +16 -4
  23. data/lib/zebra/zpl/comment.rb +15 -0
  24. data/lib/zebra/zpl/datamatrix.rb +76 -0
  25. data/lib/zebra/zpl/graphic.rb +91 -0
  26. data/lib/zebra/zpl/image.rb +91 -0
  27. data/lib/zebra/zpl/label.rb +2 -13
  28. data/lib/zebra/zpl/pdf417.rb +50 -0
  29. data/lib/zebra/zpl/qrcode.rb +2 -2
  30. data/lib/zebra/zpl/text.rb +44 -20
  31. data/lib/zebra/zpl/version.rb +1 -1
  32. data/spec/fixtures/default.jpg +0 -0
  33. data/spec/spec_helper.rb +6 -2
  34. data/spec/zebra/print_job_spec.rb +13 -18
  35. data/spec/zebra/zpl/barcode_spec.rb +70 -63
  36. data/spec/zebra/zpl/box_spec.rb +27 -31
  37. data/spec/zebra/zpl/character_set_spec.rb +7 -7
  38. data/spec/zebra/zpl/comment_spec.rb +18 -0
  39. data/spec/zebra/zpl/datamatrix_spec.rb +124 -0
  40. data/spec/zebra/zpl/graphics_spec.rb +227 -0
  41. data/spec/zebra/zpl/image_spec.rb +113 -0
  42. data/spec/zebra/zpl/label_spec.rb +40 -52
  43. data/spec/zebra/zpl/pdf417_spec.rb +108 -0
  44. data/spec/zebra/zpl/qrcode_spec.rb +92 -92
  45. data/spec/zebra/zpl/text_spec.rb +57 -55
  46. data/zebra-zpl.gemspec +14 -15
  47. metadata +71 -22
data/docs/example.rb ADDED
@@ -0,0 +1,249 @@
1
+
2
+ def print_zpl_str(name, label)
3
+ zpl = ''
4
+ label.dump_contents zpl
5
+ puts "\n#{name}:\n#{zpl}\n\n"
6
+ end
7
+
8
+ def new_label
9
+ Zebra::Zpl::Label.new(
10
+ width: 600,
11
+ length: 305,
12
+ print_speed: 6,
13
+ print_density: 5,
14
+ copies: 1
15
+ )
16
+ end
17
+
18
+ ################################################################################
19
+ # Text
20
+ ################################################################################
21
+ label = new_label
22
+ text = Zebra::Zpl::Text.new(
23
+ data: "Hello, printer!",
24
+ position: [10, 125],
25
+ font_size: Zebra::Zpl::FontSize::SIZE_5,
26
+ justification: Zebra::Zpl::Justification::CENTER
27
+ )
28
+ box = Zebra::Zpl::Graphic.new(
29
+ graphic_type: 'B',
30
+ position: [10,5],
31
+ graphic_width: label.width,
32
+ graphic_height: label.length-10,
33
+ line_thickness: 1,
34
+ rounding_degree: 1
35
+ )
36
+ label << text
37
+ label << box
38
+ print_zpl_str('text', label)
39
+
40
+ ################################################################################
41
+ # Barcode
42
+ ################################################################################
43
+ label = new_label
44
+ barcode = Zebra::Zpl::Barcode.new(
45
+ data: 'F112358',
46
+ position: [80, 50],
47
+ height: 150,
48
+ width: 4,
49
+ print_human_readable_code: true,
50
+ type: Zebra::Zpl::BarcodeType::CODE_128_AUTO
51
+ )
52
+ label << barcode
53
+ print_zpl_str('barcode', label)
54
+
55
+ ################################################################################
56
+ # QR Code
57
+ ################################################################################
58
+ label = new_label
59
+ qrcode = Zebra::Zpl::Qrcode.new(
60
+ data: 'www.github.com',
61
+ position: [200, 45],
62
+ scale_factor: 8,
63
+ correction_level: 'H',
64
+ )
65
+ label << qrcode
66
+ print_zpl_str('qrcode', label)
67
+
68
+ ################################################################################
69
+ # Data Matrix
70
+ ################################################################################
71
+ label = new_label
72
+ datamatrix = Zebra::Zpl::Datamatrix.new(
73
+ data: 'www.github.com',
74
+ position: [225, 75],
75
+ symbol_height: 10,
76
+ aspect_ratio: 1
77
+ )
78
+ label << datamatrix
79
+ print_zpl_str('datamatrix', label)
80
+
81
+ ################################################################################
82
+ # Graphics
83
+ ################################################################################
84
+ label = new_label
85
+ box = Zebra::Zpl::Graphic.new(
86
+ graphic_type: 'B',
87
+ position: [10,10],
88
+ graphic_width: 80,
89
+ graphic_height: 80,
90
+ line_thickness: 2,
91
+ rounding_degree: 2
92
+ )
93
+ circle = Zebra::Zpl::Graphic.new(
94
+ graphic_type: 'C',
95
+ position: [100,10],
96
+ graphic_width: 80,
97
+ line_thickness: 3
98
+ )
99
+ diagonal1 = Zebra::Zpl::Graphic.new(
100
+ graphic_type: 'D',
101
+ position: [190,10],
102
+ graphic_width: 80,
103
+ graphic_height: 80,
104
+ line_thickness: 3,
105
+ orientation: 'R'
106
+ )
107
+ diagonal2 = diagonal1.dup
108
+ diagonal2.orientation = 'L'
109
+ ellipse = Zebra::Zpl::Graphic.new(
110
+ graphic_type: 'E',
111
+ position: [280,10],
112
+ graphic_width: 40,
113
+ graphic_height: 80,
114
+ line_thickness: 3
115
+ )
116
+ symbol = Zebra::Zpl::Graphic.new(
117
+ graphic_type: 'S',
118
+ symbol_type: 'B',
119
+ position: [335,10],
120
+ graphic_width: 80,
121
+ graphic_height: 80
122
+ )
123
+
124
+ label << box
125
+ label << circle
126
+ label << diagonal1
127
+ label << diagonal2
128
+ label << ellipse
129
+ label << symbol
130
+ label.elements.each { |e| e.position = [e.x + 110 , e.y + 90] }
131
+ print_zpl_str('graphics', label)
132
+
133
+ ################################################################################
134
+ # Images
135
+ ################################################################################
136
+ label = new_label
137
+ image = Zebra::Zpl::Image.new(
138
+ path: File.expand_path('./images/earth.jpg', File.dirname(__FILE__)),
139
+ position: [145, 0],
140
+ width: 305,
141
+ height: 305
142
+ )
143
+ label << image
144
+ print_zpl_str('image', label)
145
+
146
+ label = new_label
147
+ image = Zebra::Zpl::Image.new(
148
+ path: File.expand_path('./images/earth.jpg', File.dirname(__FILE__)),
149
+ position: [145, 0],
150
+ width: 305,
151
+ height: 305,
152
+ invert: true
153
+ )
154
+ label << image
155
+ print_zpl_str('image_inverted', label)
156
+
157
+
158
+ ################################################################################
159
+ # Justification
160
+ ################################################################################
161
+ label = new_label
162
+ t1 = Zebra::Zpl::Text.new(
163
+ data: "ZPL",
164
+ position: [10, 25],
165
+ font_size: Zebra::Zpl::FontSize::SIZE_5,
166
+ justification: Zebra::Zpl::Justification::LEFT
167
+ )
168
+ t2 = Zebra::Zpl::Text.new(
169
+ data: "ZPL",
170
+ position: [10, 65],
171
+ font_size: Zebra::Zpl::FontSize::SIZE_5,
172
+ justification: Zebra::Zpl::Justification::CENTER
173
+ )
174
+ t3 = Zebra::Zpl::Text.new(
175
+ data: "ZPL",
176
+ position: [10, 105],
177
+ font_size: Zebra::Zpl::FontSize::SIZE_5,
178
+ justification: Zebra::Zpl::Justification::RIGHT
179
+ )
180
+ t4 = Zebra::Zpl::Text.new(
181
+ data: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua",
182
+ position: [10, 180],
183
+ font_size: Zebra::Zpl::FontSize::SIZE_4,
184
+ justification: Zebra::Zpl::Justification::JUSTIFIED
185
+ )
186
+ box = Zebra::Zpl::Graphic.new(
187
+ graphic_type: 'B',
188
+ position: [10,5],
189
+ graphic_width: label.width,
190
+ graphic_height: label.length-10,
191
+ line_thickness: 1,
192
+ rounding_degree: 1
193
+ )
194
+ label << t1
195
+ label << t2
196
+ label << t3
197
+ label << t4
198
+ label << box
199
+ print_zpl_str('justification', label)
200
+
201
+ ################################################################################
202
+ # Rotation
203
+ ################################################################################
204
+ label = new_label
205
+ t1 = Zebra::Zpl::Text.new(
206
+ data: "Zero",
207
+ position: [10, 125],
208
+ font_size: Zebra::Zpl::FontSize::SIZE_5,
209
+ rotation: Zebra::Zpl::Rotation::NO_ROTATION,
210
+ max_lines: 1
211
+ )
212
+ t2 = Zebra::Zpl::Text.new(
213
+ data: "90",
214
+ position: [100, 125],
215
+ font_size: Zebra::Zpl::FontSize::SIZE_5,
216
+ rotation: Zebra::Zpl::Rotation::DEGREES_90,
217
+ max_lines: 1
218
+ )
219
+ t3 = Zebra::Zpl::Text.new(
220
+ data: "180",
221
+ position: [175, 125],
222
+ font_size: Zebra::Zpl::FontSize::SIZE_5,
223
+ rotation: Zebra::Zpl::Rotation::DEGREES_180,
224
+ justification: Zebra::Zpl::Justification::RIGHT,
225
+ max_lines: 1
226
+ )
227
+ t4 = Zebra::Zpl::Text.new(
228
+ data: "270",
229
+ position: [275, 125],
230
+ font_size: Zebra::Zpl::FontSize::SIZE_5,
231
+ rotation: Zebra::Zpl::Rotation::DEGREES_270,
232
+ justification: Zebra::Zpl::Justification::RIGHT,
233
+ max_lines: 1
234
+ )
235
+ box = Zebra::Zpl::Graphic.new(
236
+ graphic_type: 'B',
237
+ position: [10,5],
238
+ graphic_width: label.width,
239
+ graphic_height: label.length-10,
240
+ line_thickness: 1,
241
+ rounding_degree: 1
242
+ )
243
+ label << t1
244
+ label << t2
245
+ label << t3
246
+ label << t4
247
+ label.elements.each { |e| e.position = [e.x + 150, e.y] }
248
+ label << box
249
+ print_zpl_str('rotation', label)
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -1,32 +1,26 @@
1
1
  module Zebra
2
2
  class PrintJob
3
- # class UnknownPrinter < StandardError
4
- # def initialize(printer)
5
- # super("Could not find a printer named #{printer}")
6
- # end
7
- # end
8
3
 
9
4
  attr_reader :printer
10
5
 
11
6
  def initialize(printer)
12
- #check_existent_printers printer
13
-
14
7
  @printer = printer
15
8
  end
16
9
 
17
10
  def print(label, ip)
18
11
  @remote_ip = ip
19
- tempfile = label.persist
12
+ if label.is_a? String
13
+ tempfile = Tempfile.new "zebra_label"
14
+ tempfile.write label
15
+ tempfile.close
16
+ else
17
+ tempfile = label.persist
18
+ end
20
19
  send_to_printer tempfile.path
21
20
  end
22
21
 
23
22
  private
24
23
 
25
- # def check_existent_printers(printer)
26
- # existent_printers = Cups.show_destinations
27
- # raise UnknownPrinter.new(printer) unless existent_printers.include?(printer)
28
- # end
29
-
30
24
  def send_to_printer(path)
31
25
  puts "* * * * * * * * * * * * Sending file to printer #{@printer} at #{@remote_ip} * * * * * * * * * * "
32
26
  result = system("rlpr -H #{@remote_ip} -P #{@printer} -o #{path} 2>&1") # try printing to LPD on windows machine first
data/lib/zebra/zpl.rb CHANGED
@@ -1,19 +1,26 @@
1
- # require 'cups'
1
+ require 'img2zpl'
2
2
  require 'tempfile'
3
- require 'zebra/zpl/version'
4
- require 'zebra/zpl/language'
5
- require 'zebra/zpl/country_code'
3
+
4
+ require 'zebra/print_job'
5
+
6
+ require 'zebra/zpl/barcode_type'
7
+ require 'zebra/zpl/barcode'
8
+ require 'zebra/zpl/box'
6
9
  require 'zebra/zpl/character_set'
7
- require 'zebra/zpl/rotation'
8
- require 'zebra/zpl/multipliers'
9
- require 'zebra/zpl/print_mode'
10
+ require 'zebra/zpl/comment'
11
+ require 'zebra/zpl/country_code'
12
+ require 'zebra/zpl/datamatrix'
10
13
  require 'zebra/zpl/font'
11
- require 'zebra/zpl/box'
14
+ require 'zebra/zpl/graphic'
15
+ require 'zebra/zpl/image'
16
+ require 'zebra/zpl/justification'
12
17
  require 'zebra/zpl/label'
13
- require 'zebra/zpl/text'
14
- require 'zebra/zpl/barcode'
15
- require 'zebra/zpl/barcode_type'
16
- require 'zebra/print_job'
18
+ require 'zebra/zpl/language'
19
+ require 'zebra/zpl/multipliers'
20
+ require 'zebra/zpl/pdf417'
21
+ require 'zebra/zpl/print_mode'
17
22
  require 'zebra/zpl/qrcode'
18
- require 'zebra/zpl/justification'
19
23
  require 'zebra/zpl/raw'
24
+ require 'zebra/zpl/rotation'
25
+ require 'zebra/zpl/text'
26
+ require 'zebra/zpl/version'
@@ -5,15 +5,20 @@ module Zebra
5
5
  class Barcode
6
6
  include Printable
7
7
 
8
+ class InvalidRatioError < StandardError; end
8
9
  class InvalidNarrowBarWidthError < StandardError; end
9
10
  class InvalidWideBarWidthError < StandardError; end
10
11
 
11
12
  attr_accessor :height
12
- attr_reader :type, :narrow_bar_width, :wide_bar_width, :width
13
+ attr_reader :type, :ratio, :narrow_bar_width, :wide_bar_width
13
14
  attr_writer :print_human_readable_code
14
15
 
15
- def width=(width)
16
- @width = width || 0
16
+ def width=(value)
17
+ @width = value || 0
18
+ end
19
+
20
+ def width
21
+ @width || @narrow_bar_width || @wide_bar_width || 0
17
22
  end
18
23
 
19
24
  def type=(type)
@@ -21,14 +26,27 @@ module Zebra
21
26
  @type = type
22
27
  end
23
28
 
24
- def narrow_bar_width=(width)
25
- raise InvalidNarrowBarWidthError unless (1..10).include?(width.to_i)
26
- @narrow_bar_width = width
29
+ def ratio=(value)
30
+ raise InvalidRatioError unless value.to_f >= 2.0 && value.to_f <= 3.0
31
+ @ratio = value
32
+ end
33
+
34
+ def ratio
35
+ if !@wide_bar_width.nil? && !@narrow_bar_width.nil?
36
+ (@wide_bar_width.to_f / @narrow_bar_width.to_f).round(1)
37
+ else
38
+ @ratio
39
+ end
40
+ end
41
+
42
+ def narrow_bar_width=(value)
43
+ raise InvalidNarrowBarWidthError unless (1..10).include?(value.to_i)
44
+ @narrow_bar_width = value
27
45
  end
28
46
 
29
- def wide_bar_width=(width)
30
- raise InvalidWideBarWidthError unless (2..30).include?(width.to_i)
31
- @wide_bar_width = width
47
+ def wide_bar_width=(value)
48
+ raise InvalidWideBarWidthError unless (2..30).include?(value.to_i)
49
+ @wide_bar_width = value
32
50
  end
33
51
 
34
52
  def print_human_readable_code
@@ -38,7 +56,7 @@ module Zebra
38
56
  def to_zpl
39
57
  check_attributes
40
58
  human_readable = print_human_readable_code ? "Y" : "N"
41
- "^FW#{rotation}^FO#{x},#{y}^BY#{narrow_bar_width}^B#{type}#{rotation},#{height},#{human_readable}^FD#{data}^FS"
59
+ "^FW#{rotation}^FO#{x},#{y}^BY#{width},#{ratio},#{height}^B#{type}#{rotation},,#{human_readable}^FD#{data}^FS"
42
60
  end
43
61
 
44
62
  private
@@ -47,8 +65,7 @@ module Zebra
47
65
  super
48
66
  raise MissingAttributeError.new("the barcode type to be used is not given") unless @type
49
67
  raise MissingAttributeError.new("the height to be used is not given") unless @height
50
- raise MissingAttributeError.new("the narrow bar width to be used is not given") unless @narrow_bar_width
51
- raise MissingAttributeError.new("the wide bar width to be used is not given") unless @wide_bar_width
68
+ raise MissingAttributeError.new("the width to be used is not given") unless @width || @narrow_bar_width || @wide_bar_width
52
69
  end
53
70
  end
54
71
  end
@@ -11,6 +11,9 @@ module Zebra
11
11
  CODE_AZTEC_PARAMS = "O"
12
12
  CODE_UPS_MAXICODE = "D"
13
13
  CODE_QR = "Q"
14
+ CODE_UPCA = "U"
15
+ CODE_UPCE = "9"
16
+ CODE_EAN13 = "E"
14
17
 
15
18
  # Legacy (EPL) bar code suffixes
16
19
  # CODE_39 = "3"
@@ -23,7 +26,7 @@ module Zebra
23
26
  # CODABAR = "K"
24
27
 
25
28
  def self.valid_barcode_type?(type)
26
- %w(3 A C K 0 O D Q).include? type
29
+ %w(3 A C K 0 O D Q U 9 E).include? type
27
30
  end
28
31
 
29
32
  def self.validate_barcode_type(type)