Skip to content

Instantly share code, notes, and snippets.

@jdlich
Created August 27, 2011 04:13
Show Gist options
  • Save jdlich/1174964 to your computer and use it in GitHub Desktop.
Save jdlich/1174964 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'prawn'
class String
def self.street_num_first
self.match(/(\D+)(\d+)(\s.{2,}$)?(\s\D{1}$)?/) do
street = $1 if $1 # => "Test Drive "
num = $2 if $2 # => "1234"
suite = $3 if $3 # => "#A"
abbr = $4 if $4 # => "N"
"#{num} #{abbr} #{street}#{suite}".squeeze(' ').strip
end
end
end
class Postcard
def initialize data
@address, @price, @desc = File.read(data).split("\n")[0..2]
# photo dimensions: 938px x 1275px
@photo = Dir["images/*"].select { |p| p.match(data.split("/").last) }
end
def path_to_output
"/Users/Jacob/Desktop/#{@address}.pdf"
end
def options
{ :template => "template.pdf",
:left_margin => 0,
:bottom_margin => 62 }
end
def generate
Prawn::Document.new(options) do
font_families.update(
"Georgia" => { :normal => "/Library/Fonts/Georgia.ttf",
:bold => "/Library/Fonts/Georgia Bold.ttf",
:italic => "/Library/Fonts/Georgia Italic.ttf" })
image @photo,
:at => [0, 244],
:width => 225.1
bounding_box([244,148], :width => 177) do
# address
font "Georgia"
fill_color "ffffff"
text @address.street_num_first,
:size => 10,
:leading => 4
# price
font "Georgia", :style => :bold
text @price, :size => 10
# just sold
fill_color "e3d17c"
draw_text "JUST SOLD!",
:size => 10,
:at => [(width_of(@price, :size => 10) + 3), 1.75]
move_down 10
# description
font "Georgia"
fill_color "ffffff"
text @desc,
:leading => 4,
:size => 10,
:overflow => :shrink_to_fit
end
end.render_file(path_to_output)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment