Class: Easyzpl::Label

Inherits:
Object
  • Object
show all
Defined in:
lib/easyzpl/label.rb

Overview

This is the label object

Direct Known Subclasses

LabelTemplate, StoredLabel

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Label

Called when the new method is invoked



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/easyzpl/label.rb', line 17

def initialize(params = {})
  # Create the array that will hold the data
  self.label_data = []
  # Set the default quantity to one
  self.quantity = 1

  # The start of the zpl label
  label_data.push('^XA')

  # Initialize Prawn
  init_prawn(params)
end

Instance Attribute Details

#label_dataObject

Returns the value of attribute label_data.



10
11
12
# File 'lib/easyzpl/label.rb', line 10

def label_data
  @label_data
end

#label_heightObject

Returns the value of attribute label_height.



14
15
16
# File 'lib/easyzpl/label.rb', line 14

def label_height
  @label_height
end

#label_widthObject

Returns the value of attribute label_width.



13
14
15
# File 'lib/easyzpl/label.rb', line 13

def label_width
  @label_width
end

#pdfObject

Returns the value of attribute pdf.



12
13
14
# File 'lib/easyzpl/label.rb', line 12

def pdf
  @pdf
end

#quantityObject

Returns the value of attribute quantity.



11
12
13
# File 'lib/easyzpl/label.rb', line 11

def quantity
  @quantity
end

Instance Method Details

#bar_code_39(bar_code_string, x, y, params = {}) ⇒ Object

Prints a bar code in barcode39 font



74
75
76
77
78
79
80
81
82
83
# File 'lib/easyzpl/label.rb', line 74

def bar_code_39(bar_code_string, x, y, params = {})
  x = 0 unless numeric?(x)
  y = 0 unless numeric?(y)
  label_data.push('^FO' + x.to_s + ',' + y.to_s + '^B3N,Y,20,N,N^FD' +
                  bar_code_string + '^FS')

  return unless label_height && label_width
  options = { height: 20 }.merge(params)
  draw_bar_code_39(bar_code_string, x, y, options[:height])
end

#change_quantity(q) ⇒ Object

Set the number of labels to print



31
32
33
34
# File 'lib/easyzpl/label.rb', line 31

def change_quantity(q)
  q = 1 unless numeric?(q)
  self.quantity = q
end

#draw_border(x, y, height, width) ⇒ Object

Draws a square border on dot in width



46
47
48
49
50
51
52
53
54
55
# File 'lib/easyzpl/label.rb', line 46

def draw_border(x, y, height, width)
  return unless numeric?(height) && numeric?(width)
  x = 0 unless numeric?(x)
  y = 0 unless numeric?(y)

  label_data.push('^FO' + x.to_s + ',' + y.to_s + '^GB' + height.to_s +
                  ',' + width.to_s + ',1^FS')

  draw_rectangle(x, y, height, width)
end

#home_position(x, y) ⇒ Object

Set the home position of the label All other X and Y coordinates are relative to this



39
40
41
42
43
# File 'lib/easyzpl/label.rb', line 39

def home_position(x, y)
  x = 0 unless numeric?(x)
  y = 0 unless numeric?(y)
  label_data.push('^LH' + x.to_s + ',' + y.to_s)
end

#text_field(text, x, y, params = {}) ⇒ Object

Prints text



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/easyzpl/label.rb', line 58

def text_field(text, x, y, params = {})
  x = 0 unless numeric?(x)
  y = 0 unless numeric?(y)
  options = { height: 10, width: 10 }.merge(params)
  label_data.push('^FO' + x.to_s + ',' + y.to_s + '^AFN,' +
                  options[:height].to_s + ',' + options[:width].to_s +
                  '^FD' + text + '^FS')

  return unless label_height > 0 && label_width > 0
  pdf.text_box text,
               at: [x, label_width - y -
                    Integer(options[:height] / 10)],
               size: options[:height] if label_height && label_width
end

#to_pdf(filename) ⇒ Object



91
92
93
94
# File 'lib/easyzpl/label.rb', line 91

def to_pdf(filename)
  return unless label_height && label_width
  pdf.render_file(filename)
end

#to_sObject

Renders the ZPL code as a string



86
87
88
89
# File 'lib/easyzpl/label.rb', line 86

def to_s
  return '' unless label_data.length > 0
  label_data.map! { |l| "#{l}" }.join('') + '^PQ' + quantity.to_s + '^XZ'
end