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



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
# File 'lib/easyzpl/label.rb', line 21

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

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

  # Set the DPIs
  self.pdf_dpi = 72
  self.printer_dpi = params[:dots]

  # Set the field orientation
  self.field_orientation = params[:field_orientation]

  # See if invert is set to true
  self.invert = params[:invert]

  # The start of the label
  label_data.push('^XA')
  label_data.push('^POI') if invert
  label_data.push('^LT' + Integer(params[:offset] * printer_dpi).to_s) unless params[:offset].nil?
  label_data.push('^LL' + Integer(params[:height] * printer_dpi).to_s) unless params[:height].nil?
  label_data.push('^PW' + Integer(params[:width] * printer_dpi).to_s) unless params[:width].nil?
  label_data.push('^FWB') if field_orientation == :landscape

  # Initialize Prawn
  # init_prawn(params)
end

Instance Attribute Details

#field_orientationObject

Returns the value of attribute field_orientation.



18
19
20
# File 'lib/easyzpl/label.rb', line 18

def field_orientation
  @field_orientation
end

#invertObject

Returns the value of attribute invert.



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

def invert
  @invert
end

#label_dataObject

Returns the value of attribute label_data.



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

def label_data
  @label_data
end

#label_heightObject

Returns the value of attribute label_height.



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

def label_height
  @label_height
end

#label_widthObject

Returns the value of attribute label_width.



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

def label_width
  @label_width
end

#pdfObject

Returns the value of attribute pdf.



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

def pdf
  @pdf
end

#pdf_dpiObject

Returns the value of attribute pdf_dpi.



17
18
19
# File 'lib/easyzpl/label.rb', line 17

def pdf_dpi
  @pdf_dpi
end

#printer_dpiObject

Returns the value of attribute printer_dpi.



16
17
18
# File 'lib/easyzpl/label.rb', line 16

def printer_dpi
  @printer_dpi
end

#quantityObject

Returns the value of attribute quantity.



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

def quantity
  @quantity
end

Instance Method Details

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

Prints a bar code in barcode39 font



106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/easyzpl/label.rb', line 106

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

  # return unless label_height && label_width
  # options = { height: 20 }.merge!(params) { |key, v1, v2| v1 }
  # draw_bar_code_39(bar_code_string, Integer(x * pdf_dpi),
  #                  Integer(y * pdf_dpi), (options[:height] * pdf_dpi))
end

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

Prints a bar code in pdf417 font



120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/easyzpl/label.rb', line 120

def bar_code_pdf417(bar_code_string, x, y, params = {})
  x = 0 unless numeric?(x)
  y = 0 unless numeric?(y)
  label_data.push('^FO' + Integer(x * printer_dpi).to_s + ',' +
                  Integer(y * printer_dpi).to_s + '^B7N,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, Integer(x * pdf_dpi),
  #                  Integer(y * pdf_dpi), (options[:height] * pdf_dpi))
end

#change_quantity(q) ⇒ Object

Set the number of labels to print



51
52
53
54
# File 'lib/easyzpl/label.rb', line 51

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



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/easyzpl/label.rb', line 66

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' + Integer(x * printer_dpi).to_s + ',' +
                  Integer(y * printer_dpi).to_s + '^GB' +
                  Integer(height * printer_dpi).to_s +
                  ',' + Integer(width * printer_dpi).to_s + ',1^FS')

  # draw_rectangle(x * pdf_dpi, y * pdf_dpi, height * pdf_dpi, width * pdf_dpi)
end

#home_position(x, y) ⇒ Object

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



59
60
61
62
63
# File 'lib/easyzpl/label.rb', line 59

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



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/easyzpl/label.rb', line 80

def text_field(text, x, y, params = {})
  x = 0 unless numeric?(x)
  y = 0 unless numeric?(y)
  options = { height: 0.1,
              width: 0.1 }.merge!(params)
  label_data.push('^FO' + Integer(x * printer_dpi).to_s + ',' + Integer(y *
                  printer_dpi).to_s)

  if params[:orientation] == :landscape
    label_data.push('^AFB,')
  else
    label_data.push('^AFN,')
  end

  label_data.push(Integer(options[:height] * printer_dpi).to_s + ',' +
                  Integer(options[:width] * printer_dpi).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] * pdf_dpi) / 10)],
  #                    size: (options[:height] *
  #                    pdf_dpi) if label_height && label_width
end

#to_pdf(filename) ⇒ Object



139
140
141
142
# File 'lib/easyzpl/label.rb', line 139

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

#to_sObject

Renders the ZPL code as a string



134
135
136
137
# File 'lib/easyzpl/label.rb', line 134

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