zebra-zpl 1.0.2 → 1.1.3

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 (50) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +71 -0
  3. data/CONTRIBUTING.md +49 -0
  4. data/Gemfile +6 -0
  5. data/README.md +348 -89
  6. data/docs/example.rb +290 -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/image_manipulation.png +0 -0
  14. data/docs/images/images.png +0 -0
  15. data/docs/images/justification.png +0 -0
  16. data/docs/images/qrcode.png +0 -0
  17. data/docs/images/rotation.png +0 -0
  18. data/docs/images/text.png +0 -0
  19. data/lib/zebra/print_job.rb +19 -17
  20. data/lib/zebra/zpl.rb +26 -18
  21. data/lib/zebra/zpl/barcode.rb +29 -12
  22. data/lib/zebra/zpl/barcode_type.rb +4 -1
  23. data/lib/zebra/zpl/box.rb +16 -4
  24. data/lib/zebra/zpl/comment.rb +15 -0
  25. data/lib/zebra/zpl/datamatrix.rb +76 -0
  26. data/lib/zebra/zpl/font.rb +1 -1
  27. data/lib/zebra/zpl/graphic.rb +91 -0
  28. data/lib/zebra/zpl/image.rb +97 -0
  29. data/lib/zebra/zpl/label.rb +3 -14
  30. data/lib/zebra/zpl/pdf417.rb +50 -0
  31. data/lib/zebra/zpl/qrcode.rb +6 -2
  32. data/lib/zebra/zpl/raw.rb +25 -0
  33. data/lib/zebra/zpl/text.rb +44 -20
  34. data/lib/zebra/zpl/version.rb +1 -1
  35. data/spec/fixtures/default.jpg +0 -0
  36. data/spec/spec_helper.rb +6 -2
  37. data/spec/zebra/print_job_spec.rb +13 -18
  38. data/spec/zebra/zpl/barcode_spec.rb +70 -63
  39. data/spec/zebra/zpl/box_spec.rb +27 -31
  40. data/spec/zebra/zpl/character_set_spec.rb +7 -7
  41. data/spec/zebra/zpl/comment_spec.rb +18 -0
  42. data/spec/zebra/zpl/datamatrix_spec.rb +124 -0
  43. data/spec/zebra/zpl/graphics_spec.rb +227 -0
  44. data/spec/zebra/zpl/image_spec.rb +146 -0
  45. data/spec/zebra/zpl/label_spec.rb +41 -53
  46. data/spec/zebra/zpl/pdf417_spec.rb +108 -0
  47. data/spec/zebra/zpl/qrcode_spec.rb +92 -92
  48. data/spec/zebra/zpl/text_spec.rb +60 -55
  49. data/zebra-zpl.gemspec +12 -12
  50. metadata +63 -27
@@ -0,0 +1,290 @@
1
+ require_relative '../lib/zebra/zpl'
2
+
3
+ def print_zpl_str(name, label)
4
+ zpl = ''
5
+ label.dump_contents zpl
6
+ puts "\n#{name}:\n#{zpl}\n\n"
7
+ end
8
+
9
+ def new_label
10
+ Zebra::Zpl::Label.new(
11
+ width: 600,
12
+ length: 305,
13
+ print_speed: 6,
14
+ print_density: 5,
15
+ copies: 1
16
+ )
17
+ end
18
+
19
+ ################################################################################
20
+ # Text
21
+ ################################################################################
22
+ label = new_label
23
+ text = Zebra::Zpl::Text.new(
24
+ data: "Hello, printer!",
25
+ position: [10, 125],
26
+ font_size: Zebra::Zpl::FontSize::SIZE_5,
27
+ justification: Zebra::Zpl::Justification::CENTER
28
+ )
29
+ box = Zebra::Zpl::Graphic.new(
30
+ graphic_type: 'B',
31
+ position: [10,5],
32
+ graphic_width: label.width,
33
+ graphic_height: label.length-10,
34
+ line_thickness: 1,
35
+ rounding_degree: 1
36
+ )
37
+ label << text
38
+ label << box
39
+ print_zpl_str('text', label)
40
+
41
+ ################################################################################
42
+ # Raw ZPL
43
+ ################################################################################
44
+ label = new_label
45
+ zpl_string = "^GFA,1300,1300,13,,::::::O01IFE,N01KFE,N0MFC,M03NF,M0OFC,L03PF,L0QF8,K01QFE,K03RF,K07RF8,J01SFC,J03F9OFE7E,J07F01MFE03F,J07E007LF803F8,J0FE003KFE001FC,I01FEI0FC00FC001FE,I03FCI04M01FF,I07FCQ01FF,I07FCQ01FF8,I0FFCQ01FFC,I0FFEQ01FFC,001FFEQ01FFE,:003FFEQ03IF,003FFEQ01IF,007FFCR0IF,007FFCR0IF8,007FF8R07FF8,00IFS03FF8,00IFS03FFC,:00FFES01FFC,01FFES01FFC,01FFES01FFE,:01FFCS01FFE,:::::01FFES01FFE,::01FFES03FFE,01IFS03FFE,01IFS03FFC,01IF8R07FFC,00IF8R0IFC,00IFCR0IFC,00IFEQ01IFC,00JFQ03IF8,007IF8P07IF8,007IFCP0JF8,007JFO03JF,003F87FCN0KF,003F81FFM03JFE,001F80FFEK01KFE,001FC07FFCJ0LFC,I0FF03FF8J0LFC,I0FF03FF8J07KF8,I07F81FF8J07KF8,I03F807F8J07KF,I03FC004K07JFE,I01FCN07JFE,J0FEN07JFC,J07FN07JF8,J03F8M07JF,J01FCM07IFE,K0FFM07IFC,K07IF8J07IF8,K03IF8J07IF,L0IF8J07FFC,L07FF8J07FF8,L01FF8J07FE,M0FF8J07F8,M01F8J07E,N07K038,,::::::::::::::^FS"
46
+ raw_zpl = Zebra::Zpl::Raw.new(
47
+ data: zpl_string,
48
+ position: [50, 50],
49
+ )
50
+ label << raw_zpl
51
+ print_zpl_str('raw_zpl', label)
52
+
53
+ ################################################################################
54
+ # Barcode
55
+ ################################################################################
56
+ label = new_label
57
+ barcode = Zebra::Zpl::Barcode.new(
58
+ data: 'F112358',
59
+ position: [80, 50],
60
+ height: 150,
61
+ width: 4,
62
+ print_human_readable_code: true,
63
+ type: Zebra::Zpl::BarcodeType::CODE_128_AUTO
64
+ )
65
+ label << barcode
66
+ print_zpl_str('barcode', label)
67
+
68
+ ################################################################################
69
+ # QR Code
70
+ ################################################################################
71
+ label = new_label
72
+ qrcode = Zebra::Zpl::Qrcode.new(
73
+ data: 'www.github.com',
74
+ position: [200, 45],
75
+ scale_factor: 8,
76
+ correction_level: 'H',
77
+ )
78
+ label << qrcode
79
+ print_zpl_str('qrcode', label)
80
+
81
+ ################################################################################
82
+ # Data Matrix
83
+ ################################################################################
84
+ label = new_label
85
+ datamatrix = Zebra::Zpl::Datamatrix.new(
86
+ data: 'www.github.com',
87
+ position: [225, 75],
88
+ symbol_height: 10,
89
+ aspect_ratio: 1
90
+ )
91
+ label << datamatrix
92
+ print_zpl_str('datamatrix', label)
93
+
94
+ ################################################################################
95
+ # Graphics
96
+ ################################################################################
97
+ label = new_label
98
+ box = Zebra::Zpl::Graphic.new(
99
+ graphic_type: 'B',
100
+ position: [10,10],
101
+ graphic_width: 80,
102
+ graphic_height: 80,
103
+ line_thickness: 2,
104
+ rounding_degree: 2
105
+ )
106
+ circle = Zebra::Zpl::Graphic.new(
107
+ graphic_type: 'C',
108
+ position: [100,10],
109
+ graphic_width: 80,
110
+ line_thickness: 3
111
+ )
112
+ diagonal1 = Zebra::Zpl::Graphic.new(
113
+ graphic_type: 'D',
114
+ position: [190,10],
115
+ graphic_width: 80,
116
+ graphic_height: 80,
117
+ line_thickness: 3,
118
+ orientation: 'R'
119
+ )
120
+ diagonal2 = diagonal1.dup
121
+ diagonal2.orientation = 'L'
122
+ ellipse = Zebra::Zpl::Graphic.new(
123
+ graphic_type: 'E',
124
+ position: [280,10],
125
+ graphic_width: 40,
126
+ graphic_height: 80,
127
+ line_thickness: 3
128
+ )
129
+ symbol = Zebra::Zpl::Graphic.new(
130
+ graphic_type: 'S',
131
+ symbol_type: 'B',
132
+ position: [335,10],
133
+ graphic_width: 80,
134
+ graphic_height: 80
135
+ )
136
+
137
+ label << box
138
+ label << circle
139
+ label << diagonal1
140
+ label << diagonal2
141
+ label << ellipse
142
+ label << symbol
143
+ label.elements.each { |e| e.position = [e.x + 110 , e.y + 90] }
144
+ print_zpl_str('graphics', label)
145
+
146
+ ################################################################################
147
+ # Images
148
+ ################################################################################
149
+ label = new_label
150
+ image = Zebra::Zpl::Image.new(
151
+ path: File.expand_path('./images/earth.jpg', File.dirname(__FILE__)),
152
+ position: [145, 0],
153
+ width: 305,
154
+ height: 305
155
+ )
156
+ label << image
157
+ print_zpl_str('image', label)
158
+
159
+ # inverted image
160
+ label = new_label
161
+ image = Zebra::Zpl::Image.new(
162
+ path: File.expand_path('./images/earth.jpg', File.dirname(__FILE__)),
163
+ position: [145, 0],
164
+ width: 305,
165
+ height: 305,
166
+ invert: true
167
+ )
168
+ label << image
169
+ print_zpl_str('image_inverted', label)
170
+
171
+ ################################################################################
172
+ # Image Manipulation
173
+ ################################################################################
174
+ label = new_label
175
+ image = Zebra::Zpl::Image.new(
176
+ path: File.expand_path('./images/ruby.png', File.dirname(__FILE__)),
177
+ position: [0, 0],
178
+ width: 305,
179
+ height: 305,
180
+ black_threshold: 0.65
181
+ )
182
+ src = image.source
183
+ src.background('white').flatten
184
+
185
+ # perform edge detection on the image
186
+ MiniMagick::Tool::Convert.new do |convert|
187
+ convert << src.path
188
+ convert << '-colorspace' << 'gray'
189
+ convert << '-edge' << '4'
190
+ convert << '-negate'
191
+ convert << src.path
192
+ end
193
+
194
+ src.trim
195
+
196
+ label << image
197
+ print_zpl_str('image_manipulation', label)
198
+
199
+ ################################################################################
200
+ # Justification
201
+ ################################################################################
202
+ label = new_label
203
+ t1 = Zebra::Zpl::Text.new(
204
+ data: "ZPL",
205
+ position: [10, 25],
206
+ font_size: Zebra::Zpl::FontSize::SIZE_5,
207
+ justification: Zebra::Zpl::Justification::LEFT
208
+ )
209
+ t2 = Zebra::Zpl::Text.new(
210
+ data: "ZPL",
211
+ position: [10, 65],
212
+ font_size: Zebra::Zpl::FontSize::SIZE_5,
213
+ justification: Zebra::Zpl::Justification::CENTER
214
+ )
215
+ t3 = Zebra::Zpl::Text.new(
216
+ data: "ZPL",
217
+ position: [10, 105],
218
+ font_size: Zebra::Zpl::FontSize::SIZE_5,
219
+ justification: Zebra::Zpl::Justification::RIGHT
220
+ )
221
+ t4 = Zebra::Zpl::Text.new(
222
+ data: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua",
223
+ position: [10, 180],
224
+ font_size: Zebra::Zpl::FontSize::SIZE_4,
225
+ justification: Zebra::Zpl::Justification::JUSTIFIED
226
+ )
227
+ box = Zebra::Zpl::Graphic.new(
228
+ graphic_type: 'B',
229
+ position: [10,5],
230
+ graphic_width: label.width,
231
+ graphic_height: label.length-10,
232
+ line_thickness: 1,
233
+ rounding_degree: 1
234
+ )
235
+ label << t1
236
+ label << t2
237
+ label << t3
238
+ label << t4
239
+ label << box
240
+ print_zpl_str('justification', label)
241
+
242
+ ################################################################################
243
+ # Rotation
244
+ ################################################################################
245
+ label = new_label
246
+ t1 = Zebra::Zpl::Text.new(
247
+ data: "Zero",
248
+ position: [10, 125],
249
+ font_size: Zebra::Zpl::FontSize::SIZE_5,
250
+ rotation: Zebra::Zpl::Rotation::NO_ROTATION,
251
+ max_lines: 1
252
+ )
253
+ t2 = Zebra::Zpl::Text.new(
254
+ data: "90",
255
+ position: [100, 125],
256
+ font_size: Zebra::Zpl::FontSize::SIZE_5,
257
+ rotation: Zebra::Zpl::Rotation::DEGREES_90,
258
+ max_lines: 1
259
+ )
260
+ t3 = Zebra::Zpl::Text.new(
261
+ data: "180",
262
+ position: [175, 125],
263
+ font_size: Zebra::Zpl::FontSize::SIZE_5,
264
+ rotation: Zebra::Zpl::Rotation::DEGREES_180,
265
+ justification: Zebra::Zpl::Justification::RIGHT,
266
+ max_lines: 1
267
+ )
268
+ t4 = Zebra::Zpl::Text.new(
269
+ data: "270",
270
+ position: [275, 125],
271
+ font_size: Zebra::Zpl::FontSize::SIZE_5,
272
+ rotation: Zebra::Zpl::Rotation::DEGREES_270,
273
+ justification: Zebra::Zpl::Justification::RIGHT,
274
+ max_lines: 1
275
+ )
276
+ box = Zebra::Zpl::Graphic.new(
277
+ graphic_type: 'B',
278
+ position: [10,5],
279
+ graphic_width: label.width,
280
+ graphic_height: label.length-10,
281
+ line_thickness: 1,
282
+ rounding_degree: 1
283
+ )
284
+ label << t1
285
+ label << t2
286
+ label << t3
287
+ label << t4
288
+ label.elements.each { |e| e.position = [e.x + 150, e.y] }
289
+ label << box
290
+ print_zpl_str('rotation', label)
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -1,35 +1,37 @@
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
- def print(label, ip)
10
+ def print(label, ip, print_service: "")
18
11
  @remote_ip = ip
19
- tempfile = label.persist
20
- send_to_printer tempfile.path
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
19
+ send_to_printer(tempfile.path, print_service)
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
- def send_to_printer(path)
24
+ def send_to_printer(path, print_service)
31
25
  puts "* * * * * * * * * * * * Sending file to printer #{@printer} at #{@remote_ip} * * * * * * * * * * "
32
- `lp -h #{@remote_ip} -d #{@printer} -o raw #{path}`
26
+ case print_service
27
+ when "rlpr"
28
+ system("rlpr -H #{@remote_ip} -P #{@printer} -o #{path} 2>&1") # try printing to LPD on windows machine
29
+ when "lp"
30
+ system("lp -h #{@remote_ip} -d #{@printer} -o raw #{path}") # print to unix (CUPS) using lp
31
+ else
32
+ result = system("rlpr -H #{@remote_ip} -P #{@printer} -o #{path} 2>&1") # try printing to LPD on windows machine first
33
+ system("lp -h #{@remote_ip} -d #{@printer} -o raw #{path}") if !result # print to unix (CUPS) if rlpr failed
34
+ end
33
35
  end
34
36
  end
35
37
  end
@@ -1,18 +1,26 @@
1
- require "cups"
2
- require "tempfile"
3
- require "zebra/zpl/version"
4
- require "zebra/zpl/language"
5
- require "zebra/zpl/country_code"
6
- 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/font"
11
- require "zebra/zpl/box"
12
- 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"
17
- require "zebra/zpl/qrcode"
18
- require "zebra/zpl/justification"
1
+ require 'img2zpl'
2
+ require 'tempfile'
3
+
4
+ require 'zebra/print_job'
5
+
6
+ require 'zebra/zpl/barcode_type'
7
+ require 'zebra/zpl/barcode'
8
+ require 'zebra/zpl/box'
9
+ require 'zebra/zpl/character_set'
10
+ require 'zebra/zpl/comment'
11
+ require 'zebra/zpl/country_code'
12
+ require 'zebra/zpl/datamatrix'
13
+ require 'zebra/zpl/font'
14
+ require 'zebra/zpl/graphic'
15
+ require 'zebra/zpl/image'
16
+ require 'zebra/zpl/justification'
17
+ require 'zebra/zpl/label'
18
+ require 'zebra/zpl/language'
19
+ require 'zebra/zpl/multipliers'
20
+ require 'zebra/zpl/pdf417'
21
+ require 'zebra/zpl/print_mode'
22
+ require 'zebra/zpl/qrcode'
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