Class: LabelFactory::Batch::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/label_factory/batch/base.rb

Constant Summary collapse

DEFAULTS =
{ justification: :left, font_size: 12, font_type: 'Helvetica' }
CONVERTER =
Iconv.new( 'ISO-8859-1//IGNORE//TRANSLIT', 'utf-8')
@@gt =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template_name, pdf_opts = {}) ⇒ Base

Returns a new instance of Base.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/label_factory/batch/base.rb', line 31

def initialize(template_name, pdf_opts = {})

  @template = gt.find_template(template_name)

  if @template
    @label = @template.label

    @layout = @label.layouts.first

    @pdf = PDF::Writer.new(set_options(pdf_opts))

    @pdf.margins_pt(0, 0, 0, 0)

  else
    raise 'Template not found!'
  end

end

Instance Attribute Details

#labelObject

Returns the value of attribute label.



7
8
9
# File 'lib/label_factory/batch/base.rb', line 7

def label
  @label
end

#manual_new_pageObject

Returns the value of attribute manual_new_page.



7
8
9
# File 'lib/label_factory/batch/base.rb', line 7

def manual_new_page
  @manual_new_page
end

#pdfObject

Returns the value of attribute pdf.



7
8
9
# File 'lib/label_factory/batch/base.rb', line 7

def pdf
  @pdf
end

#templateObject

Returns the value of attribute template.



7
8
9
# File 'lib/label_factory/batch/base.rb', line 7

def template
  @template
end

Class Method Details

.all_template_namesObject



21
22
23
# File 'lib/label_factory/batch/base.rb', line 21

def all_template_names
  gt.all_avaliable_templates
end

.all_templatesObject



25
26
27
# File 'lib/label_factory/batch/base.rb', line 25

def all_templates
  gt.templates.values
end

.gtObject



12
13
14
# File 'lib/label_factory/batch/base.rb', line 12

def gt
  @@gt || self.load_template_set
end

.load_template_set(template_set_file = nil) ⇒ Object



16
17
18
19
# File 'lib/label_factory/batch/base.rb', line 16

def load_template_set(template_set_file = nil)
  template_set_file ||= File.join(TEMPLATES_PATH, Template::Base::DEFAULT)
  @@gt = Template::Glabel.load_from_file(template_set_file)
end

Instance Method Details

#add_label(text, options = {}) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/label_factory/batch/base.rb', line 80

def add_label(text, options = {})
  unless options.delete(:skip)
    label_x, label_y, label_width = setup_add_label_options(options)
    opts = setup(label_x, label_width, options)
    @pdf.y = label_y
  end
  opts ||= options
  @pdf.select_font options[:font_type] if options[:font_type]
  @pdf.text(CONVERTER.iconv(text), opts)
  opts
end

#add_many_labels(text, options = {}) ⇒ Object

You can add the same text to many labels this way, takes all the arguments of add_label, but must have position instead of x,y. Requires count.

[:count] - Number of labels to print


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/label_factory/batch/base.rb', line 97

def add_many_labels(text, options = {})
  if !( options[:x] || options[:y] )
    options[:position] ||= 0
    if options[:count]
      options[:count].times do
        add_label(text, options)
        options[:position] += 1
      end
    else
      raise 'Count required'
    end
  else
    raise "Can't use X,Y with add_many_labels, you must use position"
  end
end

#add_multiline_label(content = [], position = 0) ⇒ Object

we needed something handy to write several lines into a single label, so we provide an array with hashes inside, each hash must contain a [:text] key and

could contain optional parameters such as :justification, and :font_size if not DEFAULTS options are used.


117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/label_factory/batch/base.rb', line 117

def add_multiline_label(content = [], position = 0)
  opts = {}
  content.each_with_index do |options, i|
    options = DEFAULTS.merge(options)
    if i.zero?
      opts = options
      opts[:position] = position
    else
      opts[:skip] = true
      opts.merge!(options)
    end
    text = opts.delete(:text)
    opts = add_label(text, opts)
  end
end

#draw_boxes(write_coord = true, draw_markups = true) ⇒ Object



133
134
135
136
137
138
139
140
141
# File 'lib/label_factory/batch/base.rb', line 133

def draw_boxes(write_coord = true, draw_markups = true)
  pdf.open_object do |heading|
    pdf.save_state
    do_draw_boxes!(write_coord, draw_markups)
    pdf.restore_state
    pdf.close_object
    pdf.add_object(heading, :all_pages)
  end
end

#gtObject



50
51
52
# File 'lib/label_factory/batch/base.rb', line 50

def gt
  self.class.gt
end

#save_as(file_name) ⇒ Object



143
144
145
# File 'lib/label_factory/batch/base.rb', line 143

def save_as(file_name)
  @pdf.save_as(file_name)
end

#set_afm_fonts(afm_dir = nil) ⇒ Object



76
77
78
# File 'lib/label_factory/batch/base.rb', line 76

def set_afm_fonts(afm_dir = nil)
  PDF::Writer::FontMetrics::METRICS_PATH << afm_dir if afm_dir && ! PDF::Writer::FontMetrics::METRICS_PATH.include?( afm_dir )
end

#set_fonts(font_dir = nil) ⇒ Object

add_label takes an argument hash.

[:position]  Which label slot to print.  Positions are top to bottom, left to right so position 1 is the label in the top lefthand corner.  Defaults to 0
[:x & :y]  The (x,y) coordinates on the page to print the text.  Ignored if position is specified.
[:text] What you want to print in the label.  Defaults to the (x,y) of the top left corner of the label.
[:use_margin] If the label has a markupMargin, setting this argument to true will respect that margin when writing text.  Defaults to true.
[:justification] Values can be :left, :right, :center, :full.  Defaults to :left
[:offset_x, offset_y] If your printer doesn't want to print with out margins you can define these values to fine tune printout.


72
73
74
# File 'lib/label_factory/batch/base.rb', line 72

def set_fonts(font_dir = nil)
  PDF::Writer::FONT_PATH << font_dir if font_dir && ! PDF::Writer::FONT_PATH.include?( font_dir )
end

#set_options(opts = {}) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/label_factory/batch/base.rb', line 54

def set_options(opts = {})
  opts[:paper] = @template.size.gsub(/^.*-/,'') if @template.size && ! opts[:paper]
  # set font_dir if needed
  set_fonts(opts.delete(:font_dir)) if opts[:font_dir]
  # set afm_dir if needed
  set_afm_fonts(opts.delete(:afm_dir)) if opts[:afm_dir]

  opts
end