#!/usr/bin/env ruby require 'pdfkit' require 'yaml' require 'optimist' require 'date' require 'money' I18n.enforce_available_locales = false Money.locale_backend = :currency Money.rounding_mode = BigDecimal::ROUND_HALF_EVEN module InvoiceGenerator def show_yml_example_and_exit puts <#{i.join('')}" end opts[:items] = opts[:items].join end def template_path File.join(File.dirname(__FILE__), 'invoicegenerator.html') end def generate_html(opts) map_date_fields(opts) do |value| value.strftime('%B %-d, %Y') end html = File.read(opts[:template] || template_path) opts.each do |opt, value| html.gsub!("%#{opt}%", value.to_s) end html end def main opts = read_params show_yml_example_and_exit if opts[:'show-yml-example_given'] show_template_example_and_exit if opts[:'show-template-example_given'] read_yml(opts) fix_date_options(opts) add_extra_options(opts) format_items(opts) html = generate_html(opts) kit = PDFKit.new(html.encode('iso-8859-1')) name = "invoice-#{opts[:number]}.pdf" puts "Generating #{name}..." kit.to_file(name) rescue Errno::ENOENT => e abort e.message rescue RuntimeError => e abort e.message end extend self end