Permalink
Browse files

Started on getting svg rendering working.

  • Loading branch information...
1 parent d8349f1 commit ce88898b414b267af3d168559494f8deb0583844 @commondream committed Sep 23, 2012
Showing with 107 additions and 19 deletions.
  1. +52 −0 assets/tiger.svg
  2. +2 −1 lib/payday.rb
  3. +16 −9 lib/payday/config.rb
  4. +7 −7 lib/payday/invoice.rb
  5. +9 −2 lib/payday/pdf_renderer.rb
  6. +1 −0 payday.gemspec
  7. +20 −0 test/invoice_test.rb
View

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -3,6 +3,7 @@
require 'time'
require 'bigdecimal'
require 'prawn'
+require 'prawn-svg'
require 'money'
require 'payday/version'
@@ -12,4 +13,4 @@
require 'payday/line_item'
require 'payday/pdf_renderer'
require 'payday/invoiceable'
-require 'payday/invoice'
+require 'payday/invoice'
View
@@ -1,28 +1,35 @@
module Payday
-
+
# Configuration for Payday. This is a singleton, so to set the company_name you would call
# Payday::Config.default.company_name = "Awesome Corp".
class Config
attr_accessor :invoice_logo, :company_name, :company_details, :date_format, :currency
-
- # Sets the page size to use. See the
- # {http://prawn.majesticseacreature.com/docs/0.10.2/Prawn/Document/PageGeometry.html Prawn documentation} for valid
+
+ # Sets the page size to use. See the
+ # {http://prawn.majesticseacreature.com/docs/0.10.2/Prawn/Document/PageGeometry.html Prawn documentation} for valid
# page_size values.
attr_accessor :page_size
-
+
# Returns the default configuration instance
def self.default
@@default ||= new
end
-
- private
- def initialize
+
+ # Internal: Resets a config object back to its default settings.
+ #
+ # Primarily intended for use in our tests.
+ def reset
self.invoice_logo = File.join(File.dirname(__FILE__), "..", "..", "assets", "default_logo.png")
self.company_name = "Awesome Corp"
self.company_details = "awesomecorp@commondream.net"
self.date_format = "%B %e, %Y"
self.currency = "USD"
self.page_size = "LETTER"
- end
+ end
+
+ # Internal: Contruct a new config object.
+ def initialize
+ reset
+ end
end
end
View
@@ -1,12 +1,12 @@
module Payday
-
+
# Basically just an invoice. Stick a ton of line items in it, add some details, and then render it out!
class Invoice
include Payday::Invoiceable
-
+
attr_accessor :invoice_number, :bill_to, :ship_to, :notes, :line_items, :shipping_rate, :shipping_description,
:tax_rate, :tax_description, :due_at, :paid_at, :currency, :invoice_details
-
+
def initialize(options = {})
self.invoice_number = options[:invoice_number] || nil
self.bill_to = options[:bill_to] || nil
@@ -22,15 +22,15 @@ def initialize(options = {})
self.currency = options[:currency] || nil
self.invoice_details = options[:invoice_details] || []
end
-
- # The tax rate that we're applying, as a BigDecimal
+
+ # The tax rate that we're applying, as a BigDecimal
def tax_rate=(value)
@tax_rate = BigDecimal.new(value.to_s)
end
-
+
# Shipping rate
def shipping_rate=(value)
@shipping_rate = BigDecimal.new(value.to_s)
end
end
-end
+end
View
@@ -56,7 +56,14 @@ def self.stamp(invoice, pdf)
def self.company_banner(invoice, pdf)
# render the logo
- logo_info = pdf.image(invoice_or_default(invoice, :invoice_logo), :at => pdf.bounds.top_left)
+ image = invoice_or_default(invoice, :invoice_logo)
+ if File.extname(image) == ".svg"
+ logo_info = pdf.svg(File.read(image), :at => pdf.bounds.top_left)
+ logo_height = logo_info[:height]
+ else
+ logo_info = pdf.image(image, :at => pdf.bounds.top_left)
+ logo_height = logo_info.scaled_height
+ end
# render the company details
table_data = []
@@ -69,7 +76,7 @@ def self.company_banner(invoice, pdf)
table.draw
end
- pdf.move_cursor_to(pdf.bounds.top - logo_info.scaled_height - 20)
+ pdf.move_cursor_to(pdf.bounds.top - logo_height - 20)
end
def self.bill_to_ship_to(invoice, pdf)
View
@@ -14,6 +14,7 @@ Gem::Specification.new do |s|
s.add_dependency("prawn", "~> 0.12.0")
s.add_dependency("money", "~> 3.6.1")
+ s.add_dependency("prawn-svg", "~> 0.9.1")
s.add_dependency("i18n", ">= 0.5.0")
s.add_development_dependency("minitest")
View
@@ -2,6 +2,9 @@
module Payday
class InvoiceTest < MiniTest::Unit::TestCase
+ def setup
+ Config.default.reset
+ end
test "that setting values through the options hash on initialization works" do
i = Invoice.new(:invoice_number => 20, :bill_to => "Here", :ship_to => "There",
@@ -150,5 +153,22 @@ class InvoiceTest < MiniTest::Unit::TestCase
refute_nil i.render_pdf
end
+
+ test "rendering with an svg logo" do
+ Payday::Config.default.invoice_logo = "assets/tiger.svg"
+ i = Invoice.new(:tax_rate => 0.1, :notes => "These are some crazy awesome notes!", :invoice_number => 12,
+ :due_at => Date.civil(2011, 1, 22), :paid_at => Date.civil(2012, 2, 22),
+ :bill_to => "Alan Johnson\n101 This Way\nSomewhere, SC 22222", :ship_to => "Frank Johnson\n101 That Way\nOther, SC 22229")
+
+ 3.times do
+ i.line_items << LineItem.new(:price => 20, :quantity => 5, :description => "Pants")
+ i.line_items << LineItem.new(:price => 10, :quantity => 3, :description => "Shirts")
+ i.line_items << LineItem.new(:price => 5, :quantity => 200.0, :description => "Hats")
+ end
+
+ i.render_pdf_to_file("tmp/testing.pdf")
+
+ refute_nil i.render_pdf
+ end
end
end

0 comments on commit ce88898

Please sign in to comment.