class ReportsController < ApplicationController

  require 'fpdf'

  def index
  end

  def pdf_report

    # dane
    col_sizes = [40,20,20,20]
    data = [['Kurs','Egzamin 1','Egzamin 2','Ocena'],
            ['Angielski 101','90','87','4'],
            ['Muzyka 5A','97','100','5'],
            ['Matematyka 2','98','91','5'],
            ['Pywanie','89','84','4'],
            ['Historia 110','91','81','4']]

    send_data pdf_report_card(col_sizes, data),
              :filename => "report.pdf", 
              :type => "application/pdf" 
  end

  private
    def pdf_report_card(col_sizes, data)

      pdf = FPDF.new

      pdf.AddPage
      pdf.SetFont('Arial','B')
      pdf.SetFontSize(10)
      pdf.SetFillColor(50,50,50)
      pdf.SetTextColor(255)
      pdf.SetDrawColor(0)
      pdf.SetLineWidth(0.2)

      # Nagwek tabeli.
      i = 0   
      col_sizes.each do
        pdf.Cell(col_sizes[i],7,data[0][i],1,0,'C',1)
        i += 1
      end
      pdf.Ln()

      pdf.SetFillColor(218,206,255)
      pdf.SetTextColor(0)
      pdf.SetFont('Arial')

      fill = 0
      # Dane tabeli.
      data[1..-1].each do |row|
          pdf.Cell(col_sizes[0],6,row[0],'LR',0,'L',fill)
          pdf.Cell(col_sizes[1],6,row[1],'LR',0,'L',fill)
          pdf.Cell(col_sizes[2],6,row[2],'LR',0,'L',fill)
          pdf.Cell(col_sizes[3],6,row[3],'LR',0,'C',fill)
          pdf.Ln()
          fill = (fill-1).abs % 2
      end

      # Dolna krawd tabeli.
      total = 0
      col_sizes.each {|x| total += x}
      pdf.Cell(total,0,'','T');

      pdf.Output
    end
end