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
29
30
31
32
33
34
35
36
37
# File 'lib/easyzpl/label.rb', line 17

def initialize(params = {})
  # Create the array that will hold the data
  self.label_data = []

  # If we got parameters, store them
  # Remember, we only need them for Prawn
  self.label_width = params[:width] || 0
  self.label_height = params[:height] || 0

  # Set the default quantity to one
  self.quantity = 1

  # Start creating a Prawn document in memory,
  # this can later be saved as a PDF and also
  # an image
  return unless label_height && label_width
  self.pdf = Prawn::Document.new

  # The start of the zpl label
  label_data.push('^XA')
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) ⇒ Object

Prints a bar code in barcode39 font



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/easyzpl/label.rb', line 85

def bar_code_39(bar_code_string, x, y)
  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
  pdf.bounding_box [x, Integer(label_width) - y - 40], width: 50 do
    barcode = Barby::Code39.new(bar_code_string)
    barcode.annotate_pdf(pdf)
  end
end

#change_quantity(q) ⇒ Object

Set the number of labels to print



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

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



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

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')

  # PDF creation if the label height & width has been set
  return unless label_height && label_width
  pdf.stroke_axis
  pdf.stroke do
    pdf.rectangle [x, label_width - y - width], height, width * -1
  end
end

#home_position(x, y) ⇒ Object

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



48
49
50
51
52
# File 'lib/easyzpl/label.rb', line 48

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



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

def text_field(text, x, y, params = {})
  x = 0 unless numeric?(x)
  y = 0 unless numeric?(y)
  options = { height: 10.to_s, width: 10.to_s }.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 && label_width
  pdf.text_box text, at: [x, label_width - y]
end

#to_pdf(filename) ⇒ Object



104
105
106
107
# File 'lib/easyzpl/label.rb', line 104

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

#to_sObject

Renders the ZPL code as a string



99
100
101
102
# File 'lib/easyzpl/label.rb', line 99

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