Skip to content

Instantly share code, notes, and snippets.

@mjfreshyfresh
Created October 10, 2010 00:34
Show Gist options
  • Save mjfreshyfresh/618768 to your computer and use it in GitHub Desktop.
Save mjfreshyfresh/618768 to your computer and use it in GitHub Desktop.
# A script to write my Lover's 40th B-day present
require 'rubygems'
require 'prawn'
require 'rio'
# load up data from text file
Page = Struct.new(:content, :number)
items = rio('book.txt').read.split("\n")
pages = (items.size/2).times.collect{Page.new(items.pop, items.pop)}.reverse
# setup PDF
pdf = Prawn::Document.new(:page_size => 'A2')
pdf.font "#{Prawn::BASEDIR}/data/fonts/Dustismo_Roman.ttf"
# Calculate locations and log
h = pdf.bounds.height
w = pdf.bounds.width
puts "PAGE Height:#{h} Width:#{w}"
tx = w * 0.25
ty = h * 0.75
puts "Top Box x:#{tx} y:#{ty}"
bx = tx
by = h * 0.33
puts "Bottom Box x:#{bx} y:#{by}"
bw = w * 0.5
puts "Box width:#{bw}"
# generate each page
pages.each do |p|
pdf.bounding_box [tx, ty], :width => bw do
pdf.text p.number, :size => 200, :align => :center, :valign => :top
end
pdf.bounding_box [bx, by], :width => bw do
pdf.text p.content, :size => 30, :align => :center, :valign => :top
end
pdf.start_new_page
end
# write the pdf
pdf.render_file('final_book.pdf')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment