Created
December 23, 2016 11:44
-
-
Save thomaswitt/b60a5770087b9707d989dd6c7f041e5f to your computer and use it in GitHub Desktop.
Convert AmEx travel expense CSV output to PDF files
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
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'csv' | |
require 'bigdecimal' | |
require 'prawn' # gem install prawn | |
require 'prawn/measurement_extensions' | |
require 'prawn/table' # gem install prawn-table | |
raise "No CSV file given" if ARGV[0].nil? | |
csv = ARGV[0] | |
raise "CSV file #{csv} does not exist" if !File.exist?(csv) | |
pdf = File.join(File.dirname(csv), File.basename(csv, '.csv') + '.pdf') | |
table_data = [] | |
CSV.foreach(csv, headers: false) do |r| | |
eur = r[2].strip | |
next if eur.gsub(",",".").to_f < 0 | |
table_data << [ | |
["Date", Date.parse(r[0])], | |
["Amount", "€ #{eur}"], | |
["Creditor", r[3]], | |
["Info", r[4].gsub("€","EUR")], | |
] | |
end | |
Prawn::Document.generate(pdf, page_size: 'A4', | |
info: { Title: 'Travel Expense Report' }) do | |
font_size 8 | |
table_data.each_with_index do |row, index| | |
table(row, width: page.dimensions[2]/1.2, position: :center) do | |
cells.overflow = :shrink_to_fit | |
cells.style do |c| | |
c.background_color = 'FFFFFF' | |
c.style(:width => BigDecimal.new('1.5').cm) if c.column == 0 | |
end | |
end | |
start_new_page unless (index == table_data.size - 1) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment