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' }
- CONVERTER =
Iconv.new( 'ISO-8859-1//IGNORE//TRANSLIT', 'utf-8')
- @@gt =
nil
Instance Attribute Summary collapse
-
#label ⇒ Object
Returns the value of attribute label.
-
#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_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
- #set_afm_fonts(afm_dir = nil) ⇒ Object
-
#set_fonts(font_dir = nil) ⇒ Object
add_label takes an argument hash.
- #set_options(opts = {}) ⇒ Object
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((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 |
#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
21 22 23 |
# File 'lib/label_factory/batch/base.rb', line 21 def all_template_names gt.all_avaliable_templates end |
.all_templates ⇒ Object
25 26 27 |
# File 'lib/label_factory/batch/base.rb', line 25 def all_templates gt.templates.values end |
.gt ⇒ Object
12 13 14 |
# File 'lib/label_factory/batch/base.rb', line 12 def gt @@gt || self.load_template_set 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, = {}) 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] @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, = {}) 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 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 |, 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
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 |
#gt ⇒ Object
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 (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 |