Class: RubyDanfe::Document

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

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Document

Returns a new instance of Document.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/ruby_danfe/document.rb', line 3

def initialize(opts = {})
  default_opts = {
    :page_size => 'A4',
    :page_layout => :portrait,
    :left_margin => 0,
    :right_margin => 0,
    :top_margin => 0,
    :botton_margin => 0
  }

  @document = Prawn::Document.new(default_opts.merge(opts))

  @document.font "Times-Roman"
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



18
19
20
# File 'lib/ruby_danfe/document.rb', line 18

def method_missing(method_name, *args, &block)
  @document.send(method_name, *args, &block)
end

Instance Method Details

#box(at, w, h, title = '', info = '', options = {}) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ruby_danfe/document.rb', line 52

def box(at, w, h, title = '', info = '', options = {})
  options = {
    :align => :left,
    :size => 10,
    :style => nil,
    :valign => :top,
    :border => 1
  }.merge(options)
  self.stroke_rectangle at, w, h if options[:border] == 1
  self.text_box title, :size => 6, :style => :italic, :at => [at[0] + 2, at[1] - 2], :width => w - 4, :height => 8 if title != ''
  self.text_box info, :size => options[:size], :at => [at[0] + 2, at[1] - (title != '' ? 14 : 4) ], :width => w - 4, :height => h - (title != '' ? 14 : 4), :align => options[:align], :style => options[:style], :valign => options[:valign]
end

#ibarcode(h, w, x, y, info) ⇒ Object



30
31
32
33
# File 'lib/ruby_danfe/document.rb', line 30

def ibarcode(h, w, x, y, info)
  info = info.gsub(/\D/, '')
  Barby::Code128C.new(info).annotate_pdf(self, :x => x.cm, :y => Helper.invert(y.cm), :width => w.cm, :height => h.cm) if info != ''
end

#ibox(h, w, x, y, title = '', info = '', options = {}) ⇒ Object



43
44
45
# File 'lib/ruby_danfe/document.rb', line 43

def ibox(h, w, x, y, title = '', info = '', options = {})
  box [x.cm, Helper.invert(y.cm)], w.cm, h.cm, title, info, options
end

#idate(h, w, x, y, title = '', info = '', options = {}) ⇒ Object



47
48
49
50
# File 'lib/ruby_danfe/document.rb', line 47

def idate(h, w, x, y, title = '', info = '', options = {})
  tt = info.gsub(/T.*/, '').split('-')
  ibox h, w, x, y, title, "#{tt[2]}/#{tt[1]}/#{tt[0]}", options
end

#inumeric(h, w, x, y, title = '', info = '', options = {}) ⇒ Object



65
66
67
# File 'lib/ruby_danfe/document.rb', line 65

def inumeric(h, w, x, y, title = '', info = '', options = {})
  numeric [x.cm, Helper.invert(y.cm)], w.cm, h.cm, title, info, options
end

#iqrcode(x, y, info, size = nil) ⇒ Object



35
36
37
# File 'lib/ruby_danfe/document.rb', line 35

def iqrcode(x, y, info, size = nil)
  Barby::QrCode.new(info, :level => :q, :size => size).annotate_pdf(self, :x => x.cm, :y => Helper.invert(y.cm)) if info != ''
end

#irectangle(h, w, x, y) ⇒ Object



39
40
41
# File 'lib/ruby_danfe/document.rb', line 39

def irectangle(h, w, x, y)
  self.stroke_rectangle [x.cm, Helper.invert(y.cm)], w.cm, h.cm
end

#itable(h, w, x, y, data, options = {}, &block) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/ruby_danfe/document.rb', line 75

def itable(h, w, x, y, data, options = {}, &block)
  self.bounding_box [x.cm, Helper.invert(y.cm)], :width => w.cm, :height => h.cm do
    self.table data, options do |table|
      yield(table)
    end
  end
end

#ititle(h, w, x, y, title) ⇒ Object



26
27
28
# File 'lib/ruby_danfe/document.rb', line 26

def ititle(h, w, x, y, title)
  self.text_box title, :size => 10, :at => [x.cm, Helper.invert(y.cm) - 2], :width => w.cm, :height => h.cm, :style => :bold
end

#numeric(at, w, h, title = '', info = '', options = {}) ⇒ Object



69
70
71
72
73
# File 'lib/ruby_danfe/document.rb', line 69

def numeric(at, w, h, title = '', info = '', options = {})
  options = {:decimals => 2}.merge(options)
  info = Helper.numerify(info, options[:decimals]) if info != ''
  box at, w, h, title, info, options.merge({:align => :right})
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/ruby_danfe/document.rb', line 22

def respond_to_missing?(method_name, include_private = false)
  @document.respond_to?(method_name, include_private) || super
end