#!/usr/bin/env ruby begin require 'elegant' rescue LoadError require 'rubygems' require 'elegant' end ##################### def asset(path) File.expand_path "../../tmp/#{path}", __FILE__ end def ttf(family) {normal: asset("#{family}-Regular.ttf"), bold: asset("#{family}-Bold.ttf")} end Elegant.configure do |config| config.author = 'Fullscreen' config.creator = 'Channel+' config.producer = 'Channel+ by Fullscreen' config.watermark = asset 'images/logo.png' config.fonts = { 'Helvetica' => ttf('fonts/HelveticaWorld'), 'Sans Serif' => ttf('fonts/ProximaNova'), 'Fallback' => ttf('fonts/ArialUnicode'), } end ##################### def icon_for(name) asset "images/icons/#{name}.png" end class Section attr_accessor :title, :page, :alignment def initialize(title:, page:, alignment:) @title, @page, @alignment = title, page, alignment end include Prawn::View def render_to(pdf) @document = pdf render end def render header body footer end private def header if alignment == :top and page > 1 start_new_page end move_down 10 formatted_text_box [header_text_box], height: 15, at: [0, cursor] move_down 30 # 15 of the float box, and 15 real end def body bounding_box [0, cursor], width: bounds.width, height: 170 do stroke_bounds end end def footer unless alignment == :bottom document.move_down 25 document.stroke_horizontal_rule end end def sans_serif 'Sans Serif' if document.font_families['Sans Serif'] end def header_text_box { text: title.upcase, font: sans_serif, styles: [:bold], valign: :center, color: '556270', size: 14 } end end sections = [ Section.new(title: 'Lifetime Metrics', page: 1, alignment: :top), Section.new(title: 'Monetization', page: 1, alignment: :middle), Section.new(title: 'Month over month view tracking', page: 1, alignment: :bottom), Section.new(title: 'Lifetime Metrics', page: 2, alignment: :top), Section.new(title: 'Month over month view tracking', page: 2, alignment: :middle), ] ##################### header = {text: 'March 2015', logo: {url: asset('images/thumbnail.png')}} footer = {text: "Report for Channel+ (2015/02/12 – 2015/10/12)"} filename = 'elegant.pdf' Elegant::Document.new(header: header, footer: footer) do |pdf| sections.each do |section| section.render_to pdf if pdf.cursor != 15.0 && section.alignment == :bottom p "CURSOR #{pdf.cursor}" end end end.render_file filename `open #{filename}`