Created
January 14, 2011 23:46
-
-
Save vhodges/780521 to your computer and use it in GitHub Desktop.
Print cheques from ruby using Prawn
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 'prawn' | |
require 'prawn/security' | |
require "prawn/layout" | |
require 'linguistics' # For the amount in english words. sudo gem install linguistics | |
Linguistics::use( :en ) # extends Array, String, and Numeric | |
# | |
# Quick and a little dirty first pass to be able to print cheques | |
# | |
# When I ordered my cheques, I specified Quicken as my software. The cheques | |
# I received are perforated into three sections: | |
# | |
# 1) The cheque itself | |
# 2) The stub sent to the recipient with the cheque. | |
# 3) And the stub to keep to pass onto the bookkeeper/accountant. | |
# | |
# Note, I am not sure it prints all of the required information | |
# in the required format/structure. | |
# | |
# Public domain. Hopefully you find it useful. | |
# | |
# Coords are in points (1/72 inch). | |
# Origin (0,0) is bottom left of the page. | |
# | |
Prawn.debug = true | |
ChequeComponent = Struct.new(:text, :top, :left) | |
BBox = Struct.new(:top, :width) | |
def print_cheque(chequenumber, cheque_date, amount_value, cheque_payee, note) | |
date = ChequeComponent.new("#{cheque_date}", (9.3 * 72).to_i, (5.95 * 72).to_i) | |
amount = ChequeComponent.new(amount_value, (8.94 * 72).to_i, (6.25 * 72).to_i) | |
payee = ChequeComponent.new(cheque_payee, (8.925 * 72).to_i, (0.25 * 72).to_i) | |
amount_long = ChequeComponent.new(amount_value.split(".")[0].en.numwords + " " + amount_value.split(".")[1] + " / " + "100 Dollars", (8.375 * 72).to_i, (0.25 * 72).to_i) | |
memo = ChequeComponent.new(note, (7.75 * 72).to_i, (0 * 72).to_i) | |
perf = BBox.new((6.5 * 72).to_i, (7.5 * 72)) | |
stub = BBox.new((3 * 72).to_i, (7.5 * 72)) | |
Prawn::Document.generate("cheque-#{chequenumber}.pdf") do |pdf| | |
pdf.draw_text "DATE " + date.text, :at => [date.left, date.top], :size => 8 | |
pdf.draw_text payee.text, :at => [payee.left, payee.top], :size => 10 | |
pdf.draw_text amount.text, :at => [amount.left, amount.top], :size => 10 | |
pdf.draw_text amount_long.text, :at => [amount_long.left, amount_long.top], :size => 10 | |
pdf.draw_text "RE " + memo.text, :at => [memo.left, memo.top], :size => 7 | |
pdf.bounding_box([0, perf.top], :width => perf.width) do | |
pdf.text date.text | |
pdf.text amount.text | |
pdf.text memo.text | |
end | |
pdf.bounding_box([0, stub.top], :width => stub.width) do | |
pdf.text date.text | |
pdf.text payee.text | |
pdf.text amount.text | |
pdf.text memo.text | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment