summaryrefslogtreecommitdiffstats
path: root/lib/pdf_form.rb
blob: c472fd9972e4a2bf6af24be23a6ae4b435a9a73d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
88
89
90
91
require 'prawn'
require 'prawn/measurement_extensions'

class PDFForm
  def initialize(band)
    @band = band
    @document = Prawn::Document.new(:page_size => "A4")
  end

  def render
    heading

    field("Bandnavn:", @band.name)
    field("Hjemsted:", @band.city)
    field("Webside:", @band.website)
    field("Plateselskap:", @band.label)
    field("Kort bio:", @band.short_bio)

    next_line
    field("Kontaktperson:", @band.contact.name)
    field("Postadresse:", @band.contact.addr.split("\n"))
    field("Telefon:", @band.contact.phone)
    field("Epost:", @band.contact.email)

    next_line
    field("Medlemmer:", @band.members)

    next_line
    subheading("Låter:")
    @band.songs.each do |song|
      field("Tittel", song.to_s)
      field("ISRC-kode:", song.isrc)
      field("Utøvere:", song.performers)
      field("Komponister:", song.composers)
      field("Merknad:", song.notes)
      next_line
      @document.stroke { @document.horizontal_rule }
    end

    @document.render_file "#{@band.name}.pdf"
  end

  private

  def one_line
    @one_line ||= @document.font_size.inspect.to_f * 1.4
  end

  def label_width
    3.2.cm
  end

  def heading
    @document.text "Påmelding til Norsk Urskog 2016", :size => 20
  end

  def subheading(text)
    @document.text text, :size => 16, :style => :bold
    @document.stroke { @document.horizontal_rule }
  end

  def next_line
    @document.move_down one_line
  end

  def field(title, value)
    next_line
    @document.text_box title,
      :at => [@document.bounds.left, @document.cursor],
      :width => label_width,
      :style => :bold

    if value.class == String
      @document.text_box value,
        :at => [label_width, @document.cursor],
        :width => @document.bounds.right - label_width,
        :overflow => :expand
    elsif value.class == Array
      first = true
      value.each do |v|
        next_line unless first

        @document.text_box v.to_s,
          :at => [label_width, @document.cursor],
          :width => @document.bounds.right - label_width,
          :overflow => :expand

        first = false
      end
  end
end