Class: Prawml::PDF

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

Constant Summary collapse

SYMBOLOGIES =
{
  "bookland" => "Bookland",
  "code_128" => "Code128",
  "code_25" => "Code25",
  "code_25_iata" => "Code25IATA",
  "code_25_interleaved" => "Code25Interleaved",
  "code_39" => "Code39",
  "code_93" => "Code93",
  "data_matrix" => "DataMatrix",
  "ean_13" => "EAN13",
  "ean_8" => "EAN8",
  "gs1_128" => "GS1128",
  "pdf_417" => "Pdf417",
  "qr_code" => "QrCode",
  "upc_supplemental" => "UPCSupplemental"
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(yaml, options = {}) ⇒ PDF

Returns a new instance of PDF.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/prawml.rb', line 31

def initialize(yaml, options = {})
    raise "You must pass a valid YAML file or a string with YAML to generate PDF." if yaml.empty?

    begin
      rules = File.open(yaml)
    rescue
      rules = yaml
    end
    @rules = YAML::load(rules)

    @options = {
      :page_size => "A4",
      :page_layout => :portrait
    }.merge options

    template = @options[:template]
    @options.delete(:template)
	
    @pdf = Prawn::Document.new @options

    unless template.nil?
      draw_image template[:path], [template[:x], template[:y], {:width => template[:width], :height => template[:height]}]
    end
end

Instance Attribute Details

#pdfObject (readonly)

Returns the value of attribute pdf.



12
13
14
# File 'lib/prawml.rb', line 12

def pdf
  @pdf
end

Instance Method Details

#generate(collection) ⇒ Object



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
83
84
85
86
87
# File 'lib/prawml.rb', line 56

def generate(collection)
    defaults = {
        :style => :normal,
        :size => 12,
        :align => :left,
        :format => false,
        :font => 'Times-Roman',
        :type => :text,
        :color => '000000',
        :fixed => false
    }

    @rules.each do |field, draws|
        unless draws[0].is_a? Array
          draws = [draws]
        end

        draws.each do |params|
          params[2] = defaults.merge(params[2] || {})
          params[2].symbolize_keys!
          params[2][:style] = params[2][:style].to_sym

          set_options params[2]

         value = collection.respond_to?(field.to_sym) ? collection.send(field.to_sym) : collection[field.to_sym]

          send :"draw_#{params[2][:type]}", value, params unless value.nil?
        end
    end

    @pdf
end