Created
May 19, 2010 05:33
-
-
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/
This file contains 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
.DS_Store | |
*.csv | |
This file contains 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 '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 |
This file contains 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
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I forked this to use the standard CSV library and cleaned up the pdf.number_pages syntax here