Class: LabelFactory::Batch::Base
- Inherits:
-
Object
- Object
- LabelFactory::Batch::Base
- Defined in:
- lib/label_factory/batch/base.rb
Constant Summary collapse
- DEFAULTS =
{ justification: :left, font_size: 12, font_type: 'Helvetica' }
- @@gt =
nil
Instance Attribute Summary collapse
-
#label ⇒ Object
Returns the value of attribute label.
-
#labels_per_page ⇒ Object
readonly
Returns the value of attribute labels_per_page.
-
#manual_new_page ⇒ Object
Returns the value of attribute manual_new_page.
-
#pdf ⇒ Object
Returns the value of attribute pdf.
-
#template ⇒ Object
Returns the value of attribute template.
Class Method Summary collapse
- .all_template_names ⇒ Object
- .all_templates ⇒ Object
- .gt ⇒ Object
- .load_template_set(template_set_file = nil) ⇒ Object
Instance Method Summary collapse
-
#add_label(text, options = {}) ⇒ Object
add_label takes an argument hash.
-
#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.
-
#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.
- #draw_boxes(write_coord = true, draw_markups = true) ⇒ Object
- #gt ⇒ Object
-
#initialize(template_name, pdf_opts = {}) ⇒ Base
constructor
A new instance of Base.
- #save_as(file_name) ⇒ Object
Constructor Details
#initialize(template_name, pdf_opts = {}) ⇒ Base
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/label_factory/batch/base.rb', line 12 def initialize(template_name, pdf_opts = {}) @template = gt.find_template(template_name) if @template #if the template specifies the paper type, and the user didn't use it. pdf_opts[:paper] = @template.size.gsub(/^.*-/,'') if @template.size && !pdf_opts.has_key?(:paper) @label = @template.labels['0'] @layout = @label.layouts.first @labels_per_page = [ @layout.nx, @layout.ny ].reduce(:*) @zero_based_labels_per_page = @labels_per_page - 1 # set font_dir if needed font_dir = pdf_opts.delete(:font_dir) PDF::Writer::FONT_PATH << font_dir if font_dir && ! PDF::Writer::FONT_PATH.include?( font_dir ) # set afm_dir if needed afm_dir = pdf_opts.delete(:afm_dir) PDF::Writer::FontMetrics::METRICS_PATH << afm_dir if afm_dir && ! PDF::Writer::FontMetrics::METRICS_PATH.include?( font_dir ) @pdf = PDF::Writer.new(pdf_opts) @pdf.margins_pt(0, 0, 0, 0) else raise 'Template not found!' end end |
Instance Attribute Details
#label ⇒ Object
Returns the value of attribute label.
7 8 9 |
# File 'lib/label_factory/batch/base.rb', line 7 def label @label end |
#labels_per_page ⇒ Object (readonly)
Returns the value of attribute labels_per_page.
8 9 10 |
# File 'lib/label_factory/batch/base.rb', line 8 def labels_per_page @labels_per_page end |
#manual_new_page ⇒ Object
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 |
#pdf ⇒ Object
Returns the value of attribute pdf.
7 8 9 |
# File 'lib/label_factory/batch/base.rb', line 7 def pdf @pdf end |
#template ⇒ Object
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_names ⇒ Object
55 56 57 |
# File 'lib/label_factory/batch/base.rb', line 55 def self.all_template_names gt.all_avaliable_templates end |
.all_templates ⇒ Object
59 60 61 |
# File 'lib/label_factory/batch/base.rb', line 59 def self.all_templates gt.templates.values end |
.gt ⇒ Object
42 43 44 |
# File 'lib/label_factory/batch/base.rb', line 42 def self.gt @@gt || self.load_template_set end |
.load_template_set(template_set_file = nil) ⇒ Object
50 51 52 53 |
# File 'lib/label_factory/batch/base.rb', line 50 def self.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
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.
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/label_factory/batch/base.rb', line 73 def add_label(text, = {}) unless .delete(:skip) label_x, label_y, label_width = () opts = setup(label_x, label_width, ) @pdf.y = label_y end opts ||= @pdf.select_font [:font_type] if [:font_type] text.force_encoding('UTF-8') @pdf.text(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
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/label_factory/batch/base.rb', line 91 def add_many_labels(text, = {}) if !( [:x] || [:y] ) [:position] ||= 0 if [:count] [:count].times do add_label(text, ) [: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 are used.
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/label_factory/batch/base.rb', line 111 def add_multiline_label(content = [], position = 0) opts = {} content.each_with_index do |, i| = DEFAULTS.merge() if i.zero? opts = opts[:position] = position else opts[:skip] = true opts.merge!() end text = opts.delete(:text) opts = add_label(text, opts) end end |
#draw_boxes(write_coord = true, draw_markups = true) ⇒ Object
166 167 168 169 170 171 172 173 174 |
# File 'lib/label_factory/batch/base.rb', line 166 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 |
#gt ⇒ Object
46 47 48 |
# File 'lib/label_factory/batch/base.rb', line 46 def gt self.class.gt end |
#save_as(file_name) ⇒ Object
176 177 178 |
# File 'lib/label_factory/batch/base.rb', line 176 def save_as(file_name) @pdf.save_as(file_name) end |