summaryrefslogblamecommitdiffstats
path: root/lib/pdf_form.rb
blob: 7d0aa930533b17108f57f49332ff5a2daa5f77b9 (plain) (tree)



































































                                                                                                              
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)
    next_line
    field("Telefon:", @band.contact.phone)
    field("Epost:", @band.contact.email)

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

    field("Låter:", @band.songs)

    @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 next_line
    @document.move_down one_line
  end

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

    if value.class == String
      @document.text_box value, :at => [label_width, y_pos], :width => @document.bounds.right - label_width
    elsif value.class == Array
      value.each do |v|
        @document.text_box v.to_s, :at => [label_width, y_pos], :width => @document.bounds.right - label_width
        y_pos -= one_line
        next_line
      end
    end

  end
end