Class: PDFLabelPage

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

Overview

— require ‘breakpoint’

Constant Summary collapse

@@gt =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of PDFLabelPage.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/pdf_label_page.rb', line 10

def initialize(template_name, pdf_opts = {})
  @@gt || PDFLabelPage.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]
  @pdf = PDF::Writer.new(pdf_opts)
  @pdf.margins_pt(0, 0, 0, 0)
end

Instance Attribute Details

#gtObject

Returns the value of attribute gt.



8
9
10
# File 'lib/pdf_label_page.rb', line 8

def gt
  @gt
end

#labelObject

Returns the value of attribute label.



8
9
10
# File 'lib/pdf_label_page.rb', line 8

def label
  @label
end

#pdfObject

Returns the value of attribute pdf.



8
9
10
# File 'lib/pdf_label_page.rb', line 8

def pdf
  @pdf
end

#templateObject

Returns the value of attribute template.



8
9
10
# File 'lib/pdf_label_page.rb', line 8

def template
  @template
end

Class Method Details

.all_template_namesObject



32
33
34
35
# File 'lib/pdf_label_page.rb', line 32

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

.load_template_set(template_set_file = nil) ⇒ Object



27
28
29
30
# File 'lib/pdf_label_page.rb', line 27

def PDFLabelPage.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_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.


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/pdf_label_page.rb', line 46

def add_label(options = {})
  if position = options[:position]
    label_x, label_y = position_to_x_y(position)
  elsif((label_x = options[:x]) && (label_y = options[:y]))
  else
    label_x, label_y = position_to_x_y(0)
  end
  #line wrap margin
  label_width = label_x + @label.width.as_pts
 
  if (use_margin = options[:use_margin]).nil?
    use_margin = true
  end
  if use_margin
    @label.markupMargins.each {|margin|
      label_x = label_x + margin.size.as_pts
      label_y = label_y - margin.size.as_pts
      label_width = label_width - margin.size.as_pts
    }
  end
  
  if offset = options[:offset_x]
    label_x = label_x + offset
    label_width = label_width + offset
  end
  if offset = options[:offset_y]
    label_y = label_y + offset
  end
  text = options[:text] || "[#{label_x / 72}, #{label_y / 72}]"
  arg_hash = {:justification => (options[:justification] || :left),
              :font_size => (options[:font_size] || 12)}

  arg_hash = arg_hash.merge :absolute_left => label_x,
             :absolute_right => 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


87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/pdf_label_page.rb', line 87

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

#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



104
105
106
107
108
109
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
# File 'lib/pdf_label_page.rb', line 104

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



143
144
145
# File 'lib/pdf_label_page.rb', line 143

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