Skip to content

Instantly share code, notes, and snippets.

@karmi
Created May 19, 2010 05:33
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 20 You must be signed in to fork a gist
  • Save karmi/405984 to your computer and use it in GitHub Desktop.
Save karmi/405984 to your computer and use it in GitHub Desktop.
Script to generate PDF cards suitable for planning poker from Pivotal Tracker [http://www.pivotaltracker.com/] CSV export. Inspired by Bryan Helmkamp's http://github.com/brynary/features2cards/
.DS_Store
*.csv
*.pdf
require 'rubygems'
require 'fastercsv'
require 'ostruct'
require 'term/ansicolor'
require 'prawn'
require 'stories2cards'
class String; include Term::ANSIColor; end
file = ARGV.first
unless file
puts "[!] Please provide a path to CSV file"
exit 1
end
# --- Read the CSV file -------------------------------------------------------
stories = FasterCSV.read(file)
headers = stories.shift
# p headers
# p stories
# --- Hold story in Card class
class Card < OpenStruct
def type
@table[:type]
end
end
# --- Create cards for Features2Cards
# [http://github.com/brynary/features2cards/blob/master/lib/features2cards/prawn.rb]
cards = stories.map do |story|
attrs = { :title => story[1] || '',
:body => story[14] || '',
:type => story[6] || ''}
Card.new attrs
end
# p cards
# --- Generate PDF with Prawn
begin
outfile = File.basename(file, ".csv")
Prawn::Document.generate_cards("#{outfile}.pdf", cards)
puts ">>> Generated PDF file in '#{outfile}.pdf' with #{cards.size} stories:\n".black.on_green
cards.each do |card|
puts "* #{card.title}"
end
rescue Exception
puts "[!] There was an error while generating the PDF file... What happened was:".white.on_red
raise
end
# Adapted from http://github.com/brynary/features2cards/blob/master/lib/features2cards/prawn.rb
#
# * Changed font to DejaVuSans (default Helvetica fucks up non-ASCII chars)
# * Tweaked font size
# * Tweaked margins
class Prawn::Document
CARD_WIDTH = 72 * 5 # 5 inches
CARD_HEIGHT = 72 * 3 # 3 inches
def self.generate_cards(outfile, cards)
generate(outfile,
:page_layout => :landscape,
:margin => 50,
:page_size => 'A4') do
font "#{Prawn::BASEDIR}/data/fonts/DejaVuSans.ttf"
row = 2
col = 0
cards.each do |card|
if row == 0
start_new_page
row = 2
col = 0
end
draw_card(card, row, col)
col += 1
if col > 1
col = 0
row -= 1
end
end
end
end
def margin_box(margin, &block)
bounding_box [bounds.left + margin, bounds.top - margin],
:width => bounds.width - (margin * 2), :height => bounds.height - (margin * 2),
&block
end
def draw_card(card, row, col)
bounding_box [CARD_WIDTH * col, CARD_HEIGHT * row + ((bounds.height - (2*CARD_HEIGHT))/2)],
:width => CARD_WIDTH, :height => CARD_HEIGHT do
stroke_bounds
margin_box 8 do
text card.title, :size => 14
margin_box 32 do
text card.body, :size => 10, :align => :left
end
unless card.type.nil?
bounding_box [bounds.left, bounds.bottom + 10], :width => bounds.width, :height => 10 do
text card.type, :size => 8, :align => :right
end
end
end
end
end
end
@ruprict
Copy link

ruprict commented Jan 30, 2013

I forked this to use the standard CSV library and cleaned up the pdf.number_pages syntax here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment