Class: Pdf::Label::Batch

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

Constant Summary collapse

@@gt =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Batch.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/pdf/label/batch.rb', line 12

def initialize(template_name, pdf_opts = {})
  @@gt || self.class.load_template_set
  unless @template = @@gt.find_template(template_name)
    raise "Template not found!"
  end
  #if the template specifies the paper type, and the user didn't use it.
  if @template.size && !pdf_opts.has_key?(:paper)
    pdf_opts[:paper] = @template.size.gsub(/^.*-/,'')
  end
  #TODO figure out how to cope with multiple label types on a page
  @label = @template.labels["0"]
  #TODO figure out how to handle multiple layouts
  @layout = @label.layouts[0]
  @labels_per_page = @layout.nx * @layout.ny
  @zero_based_labels_per_page = @labels_per_page - 1
  
  @pdf = PDF::Writer.new(pdf_opts)
  @pdf.margins_pt(0, 0, 0, 0)
  
  # Turn off print scaling in the generated PDF to ensure 
  # that the labels are positioned correctly when printing
# TODO This goes boom!        @pdf.viewer_preferences('PrintScaling', '/None')
end

Instance Attribute Details

#barcode_fontObject

Returns the value of attribute barcode_font.



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

def barcode_font
  @barcode_font
end

#gtObject

Returns the value of attribute gt.



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

def gt
  @gt
end

#labelObject

Returns the value of attribute label.



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

def label
  @label
end

#pdfObject

Returns the value of attribute pdf.



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

def pdf
  @pdf
end

#templateObject

Returns the value of attribute template.



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

def template
  @template
end

Class Method Details

.all_barcode_fontsObject



46
47
48
49
50
51
52
53
# File 'lib/pdf/label/batch.rb', line 46

def self.all_barcode_fonts
  {"Code128.afm" => :translation_needed,
   "Code2of5interleaved.afm" => :translation_needed,
   "Code3de9.afm" => :code39,
   "CodeDatamatrix.afm" => :translation_needed,
   "CodeEAN13.afm" => :translation_needed,
   "CodePDF417.afm" => :translation_needed}
end

.all_template_namesObject



41
42
43
44
# File 'lib/pdf/label/batch.rb', line 41

def self.all_template_names
  @@gt || self.load_template_set
  @@gt.find_all_templates
end

.load_template_set(template_set_file = nil) ⇒ Object



36
37
38
39
# File 'lib/pdf/label/batch.rb', line 36

def self.load_template_set(template_set_file=nil)
  template_set_file ||= File.expand_path(File.dirname(__FILE__) + "/../../../templates/avery-us-templates.xml")
  @@gt = GlabelsTemplate.load_from_file(template_set_file)
end

Instance Method Details

#add_barcode_label(options = {}) ⇒ 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.


158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/pdf/label/batch.rb', line 158

def add_barcode_label(options = {})
  label_x, label_y, label_width = setup_add_label_options(options)

  text = options[:text] || "[#{label_x / 72}, #{label_y / 72}]"
  
  bar_text = setup_bar_text(options, text)
  
  arg_hash = setup_arg_hash(options, label_x, label_y, label_width)
         
  bar_hash = arg_hash.clone
  bar_hash[:font_size] = options[:bar_size] || 12

  old_font = @pdf.current_font!
  @pdf.select_font(self.barcode_font)

  @pdf.y = label_y
  @pdf.text(bar_text,bar_hash)
  
  @pdf.select_font(old_font)
  @pdf.text(text,arg_hash)      
end

#add_label(options = {}) ⇒ 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.


78
79
80
81
82
83
84
85
86
87
# File 'lib/pdf/label/batch.rb', line 78

def add_label(options = {})
  label_x, label_y, label_width = setup_add_label_options(options)

  text = options[:text] || "[#{label_x / 72}, #{label_y / 72}]"
  
  arg_hash = setup_arg_hash(options, label_x, label_y, label_width)

  @pdf.y = label_y
  @pdf.text(text,arg_hash)
end

#add_many_labels(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


93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/pdf/label/batch.rb', line 93

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

#code39(text) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/pdf/label/batch.rb', line 55

def code39(text)
  out = text.upcase
  raise "Characters Not Encodable in Code3of9" unless out.match(/^[0-9A-Z\-\. \/\$\+%\*]+$/)
  out = "*" + out unless out.match(/^\*/)
  out = out + "*" unless out.match(/\*$/)
  return out
end

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

To facilitate aligning a printer we give a method that prints the outlines of the labels



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/pdf/label/batch.rb', line 110

def draw_boxes(write_coord = true, draw_markups = true)
  @layout.nx.times do |x|
    @layout.ny.times do |y|
      box_x, box_y = get_x_y(x, y)
      @pdf.rounded_rectangle(box_x,
                             box_y,
                             @label.width.as_pts, 
                             @label.height.as_pts, 
                             @label.round.as_pts).stroke
      if write_coord
        text = "#{box_x / 72}, #{box_y / 72}, #{@label.width.number}, #{label.height.number}"
        add_label(:x => box_x, :y => box_y, :text => text)
      end

      if draw_markups
        @label.markupMargins.each {|margin|
          size = margin.size.as_pts
          @pdf.rounded_rectangle(box_x + size,
                                 box_y - margin.size.as_pts,
                                 @label.width.as_pts - 2*size,
                                 @label.height.as_pts - 2*size,
                                 @label.round.as_pts).stroke
        }
        @label.markupLines.each {|line|
          @pdf.line(box_x + line.x1.as_pts,
                    box_y + line.y1.as_pts,
                    box_x + line.x2.as_pts,
                    box_y + line.y2.as_pts).stroke
        }
=begin TODO Draw cirles
        @label.markupCircles.each {|cicle|
          @pdf.
=end
      end
                             
    end
  end
end

#save_as(file_name) ⇒ Object



180
181
182
# File 'lib/pdf/label/batch.rb', line 180

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

#translation_needed(text) ⇒ Object



63
64
65
66
67
# File 'lib/pdf/label/batch.rb', line 63

def translation_needed(text)
  $stderr.puts("This barcode format does not automatically get formatted yet")
  #TODO - Rob need to add barcode formatting
  return text
end