Created
December 28, 2011 21:35
-
-
Save haldun/1529895 to your computer and use it in GitHub Desktop.
bingo card generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'prawn' | |
PrawnSplitWord = Class.new(StandardError) | |
module Prawn | |
module Core | |
module Text | |
module Formatted #:nodoc: | |
class LineWrap #:nodoc: | |
def wrap_by_char(segment) | |
raise PrawnSplitWord | |
end | |
end | |
end | |
end | |
end | |
end | |
module Prawn | |
module Text | |
module Formatted | |
class Box | |
def shrink_to_fit(text) | |
begin | |
wrap(text) | |
rescue PrawnSplitWord | |
@everything_printed = false | |
end | |
until @everything_printed || @font_size <= @min_font_size | |
@font_size = [@font_size - 0.5, @min_font_size].max | |
@document.font_size = @font_size | |
begin | |
wrap(text) | |
rescue PrawnSplitWord | |
@everything_printed = false | |
end | |
end | |
end | |
end | |
end | |
end | |
end | |
word_list = " | |
A Beautiful Mind | |
A Mighty Heart | |
Apollo 13 | |
Black Hawk Down | |
Braveheart | |
Brian's Song | |
Catch Me If You Can | |
Coal Miner's Daughter | |
Flags of Our Fathers | |
Gandhi | |
La Bamba | |
Lean On Me | |
Men of Honor | |
Norma Rae | |
Patch Adams | |
Ray | |
Remember The Titans | |
Rudy | |
Schindler's List | |
The Aviator | |
The Elephant Man | |
The Miracle Worker | |
The Pianist | |
The Pursuit of Happyness | |
Walk the Line | |
".strip.split("\n") | |
def generate_pdf(word_list, output_file, options = {}) | |
# Default options | |
opts = { | |
show_header: true, | |
header: "BINGO", | |
pages: 1 | |
}.merge(options) | |
header = opts[:header].each_char.to_a | |
Prawn::Document.generate(output_file) do | |
font "Times-Roman" | |
table_options = { | |
header: true, | |
cell_style: { | |
width: 90, | |
height: 90, | |
align: :center, | |
valign: :center, | |
size: 32, | |
overflow: :shrink_to_fit, | |
min_font_size: 10, | |
padding: 12 | |
}, | |
position: :center | |
} | |
1.upto(opts[:pages]) do |page| | |
move_down 80 | |
table_data = word_list.shuffle.each_slice(5).to_a | |
if opts[:show_header] | |
table_data = [header] + table_data | |
end | |
table table_data, table_options do | |
row(0).size = 64 | |
end | |
start_new_page | |
end | |
end | |
end | |
generate_pdf word_list, '/home/hb/deneme.pdf', pages: 10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment