class ReportsController < ApplicationController

  require 'fpdf'

  def index
  end

  def pdf_report

    # Data
    col_sizes = [40,20,20,20]
    data = [['Course','Exam 1','Exam 2','Final'],
            ['ENGLISH 101','90','87','B'],
            ['MUSIC 5A','97','100','A'],
            ['CALC 2','98','91','A'],
            ['SWIM','89','84','B'],
            ['HIST 110','91','81','B']]

    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)

      # Table Header
      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
      # Table Data
      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

      # Bottom Table Border
      total = 0
      col_sizes.each {|x| total += x}
      pdf.Cell(total,0,'','T');

      pdf.Output
    end
end