Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## error
- NoMethodError in InvoicesController#print
- You have a nil object when you didn't expect it!
- You might have expected an instance of Array.
- The error occurred while evaluating nil.each
- lib/invoice_drawer.rb:58:in `draw'
- lib/invoice_drawer.rb:36:in `new'
- lib/invoice_drawer.rb:36:in `draw'
- app/controllers/invoices_controller.rb:81:in `print'
- ## /lib/invoice_drawer.rb
- class InvoiceDrawer
- def self.draw(invoice)
- pdf = PDF::Writer.new(:paper => "A4")
- pdf.select_font "Times-Roman"
- pdf.text("Page One Technologies Pty. Ltd.", :font_size => 16, :justification => :left)
- pdf.text("A.B.N 85 074 597 103", :font_size => 8, :justification => :left)
- pdf.text("Suite 4, No. 1 Shaw Road", :font_size => 10, :justification => :left)
- pdf.text("Ingleburn NSW 2565", :font_size => 10, :justification => :left)
- pdf.text("Ph: (02) 9829 3151 Fax: (02) 9829 2713", :font_size => 10, :justification => :left)
- pdf.text("\n")
- pdf.text("Job/Invoice ##{"%05.0f" % invoice.id}", :font_size => 10, :justification => :right)
- PDF:: SimpleTable.new do |billto|
- billto.column_order.push(*%w(billto))
- billto.columns["billto"] = PDF::SimpleTable::Column.new("billto") { |col|
- col.heading = "BILL TO"
- }
- billto.show_lines = :outer
- billto.show_headings = true
- billto.position = :left
- billto.orientation = 20
- billto.shade_rows = :none
- billto.width = 170
- data = [
- { "billto" => "#{invoice.client.name}" },
- { "billto" => "#{invoice.client.address}" },
- { "billto" => "#{invoice.client.suburb} #{invoice.client.state} #{invoice.client.postcode}" }
- ]
- billto.data.replace data
- billto.render_on(pdf)
- pdf.text("\n")
- end
- PDF:: SimpleTable.new do |invoice|
- invoice.column_order.push(*%w(description quantity rate amount))
- invoice.columns["description"] = PDF::SimpleTable::Column.new("description") { |col|
- col.heading = "Description"
- }
- invoice.columns["quantity"] = PDF::SimpleTable::Column.new("quantity") { |col|
- col.heading = "Quantity"
- }
- invoice.columns["rate"] = PDF::SimpleTable::Column.new("rate") { |col|
- col.heading = "Rate"
- }
- invoice.columns["amount"] = PDF::SimpleTable::Column.new("amount") { |col|
- col.heading = "Amount"
- }
- invoice.show_lines = :outer
- invoice.show_headings = true
- invoice.position = :left
- invoice.orientation = 20
- invoice.shade_rows = :none
- invoice.width = 792
- !! I recommend this goes in your controller to stop slowing down your pdf exporting.
- @activities = @invoices.activities.find(:all)
- data = @activities.collect do |o|
- { "description" => "#{o.comments}", "quantity" => "#{hour_format(o.minutes)}", "rate" => "#{number_to_currency(o.rate)}" }, "amount" => "#{number_to_currency(o.price)}"}
- end
- invoice.data.replace data
- invoice.render_on(pdf)
- end
- end
- pdf.render
- end
- end
- ## invoices_controller.rb
- def print
- @invoice = Invoice.find(params[:id])
- send_data InvoiceDrawer.draw(@invoice), :filename => 'invoice.pdf', :type => 'application/pdf', :disposition => 'inline'
- end
Add Comment
Please, Sign In to add comment