summaryrefslogtreecommitdiffstats
path: root/lib/pdf_form.rb
blob: 90c0b61493cb4a5bfc92da174d3f0492a602e845 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
require 'prawn'
require 'prawn/measurement_extensions'

Prawn::Font::AFM.hide_m17n_warning = true

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

  def render(filename)
    heading
    intro_text

    @document.pad_bottom(5) do
      subheading("Generell info:")
      field("Band:", @band.name_and_city)
      field("Webside:", @band.website)
      field("Plateselskap:", @band.label)
      field("Kort bio:", @band.short_bio)
      field("Medlemmer:", @band.members)
    end

    @document.pad_bottom(5) do
      field("Kontaktperson:", @band.contact.name)
      field("Postadresse:", @band.contact.addr)
      field("Telefon:", @band.contact.phone)
      field("Epost:", @band.contact.email)
    end

    subheading("Låter:")
    @band.songs.each do |song|
      unless song.title.empty?
        @document.pad_bottom(10) do
          field("Tittel", song.to_s)
          field("ISRC-kode:", song.isrc)
          field("Utøvere:", song.performers)
          field("Komponister:", song.composers)
          field("Merknad:", song.notes)
        end
      end
    end

    if @document.cursor < 120
      @document.start_new_page
    end

    subheading("Fullmakt:")
    end_text

    @document.number_pages "#{@band.name} - Norsk Urskog 2018 - Side <page>", :align => :center, :at => [0, 0]
    @document.render_file filename
  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.pad_bottom(10) do
      @document.text "Påmelding til Norsk Urskog 2018", :size => 20
    end
  end

  def subheading(text)
    @document.pad(5) do
      @document.text text, :size => 16, :style => :bold
    end
  end

  def next_line
    @document.move_down one_line
  end

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

      @document.span(@document.bounds.right - label_width, :position => label_width) do
        if value.class == String
          value = " " if value.empty?
          @document.text value
        elsif value.class == Array
          first = true
          value.each do |v|
            @document.text v.to_s
          end
        elsif value.class == Float
          @document.stroke { @document.horizontal_line 0, value, :at => @document.cursor - one_line}
        end
      end
    end
  end

  def intro_text
    @document.pad_bottom(10) do
      @document.text((<<-END.gsub(/\s+/, ' ')), :inline_format => true)
        Takk for din påmelding til Norsk Urskog 2018! Nedenfor finner du de opplysningene vi har
        registrert om din påmelding. Vennligst se over at alt er riktig, skriv så ut og send skjemaet
        i underskrevet tilstand tilbake til:<br><br>

        <b>Norsk Urskog</b><br>
        v/Harald Eilertsen<br>
        Grettevegen 9<br>
        3622 Svene
      END
    end
  end

  def end_text
    @document.span(@document.bounds.right) do
      @document.pad(5) do
        @document.text((<<-END.gsub(/\s+/, ' ') + "\n\n"), :inline_format => true)
          Jeg bekrefter herved at informasjonen gitt ovenfor er riktig, og at jeg på vegne av
          #{@band.name} har fullmakt til å melde ovennevnte låter på Norsk Urskog Metal Sampler 2018:
        END
      end
      @document.pad_bottom(32) { field("Dato/Sted:", 10.cm) }
      @document.pad_bottom(32) { field("Sign:", 10.cm) }
      field("Leselig navn:", 10.cm)
    end
  end
end