Class: LabelFactory::Batch::Base
- Inherits:
-
Object
- Object
- LabelFactory::Batch::Base
- Extended by:
- Forwardable
- 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.
- #render ⇒ Object
- #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.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/label_factory/batch/base.rb', line 33 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.
9 10 11 |
# File 'lib/label_factory/batch/base.rb', line 9 def label @label end |
#manual_new_page ⇒ Object
Returns the value of attribute manual_new_page.
9 10 11 |
# File 'lib/label_factory/batch/base.rb', line 9 def manual_new_page @manual_new_page end |
#pdf ⇒ Object
Returns the value of attribute pdf.
9 10 11 |
# File 'lib/label_factory/batch/base.rb', line 9 def pdf @pdf end |
#template ⇒ Object
Returns the value of attribute template.
9 10 11 |
# File 'lib/label_factory/batch/base.rb', line 9 def template @template end |
Class Method Details
.all_template_names ⇒ Object
23 24 25 |
# File 'lib/label_factory/batch/base.rb', line 23 def all_template_names gt.all_avaliable_templates end |
.all_templates ⇒ Object
27 28 29 |
# File 'lib/label_factory/batch/base.rb', line 27 def all_templates gt.templates.values end |
.gt ⇒ Object
14 15 16 |
# File 'lib/label_factory/batch/base.rb', line 14 def gt @@gt || self.load_template_set end |
Instance Method Details
#add_label(text, options = {}) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/label_factory/batch/base.rb', line 82 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
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/label_factory/batch/base.rb', line 99 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.
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/label_factory/batch/base.rb', line 119 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
135 136 137 138 139 140 141 142 143 |
# File 'lib/label_factory/batch/base.rb', line 135 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
52 53 54 |
# File 'lib/label_factory/batch/base.rb', line 52 def gt self.class.gt end |
#render ⇒ Object
149 150 151 |
# File 'lib/label_factory/batch/base.rb', line 149 def render @pdf.render end |
#save_as(file_name) ⇒ Object
145 146 147 |
# File 'lib/label_factory/batch/base.rb', line 145 def save_as(file_name) @pdf.save_as(file_name) end |
#set_afm_fonts(afm_dir = nil) ⇒ Object
78 79 80 |
# File 'lib/label_factory/batch/base.rb', line 78 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.
74 75 76 |
# File 'lib/label_factory/batch/base.rb', line 74 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
56 57 58 59 60 61 62 63 64 |
# File 'lib/label_factory/batch/base.rb', line 56 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 |