Guest User

Untitled

a guest
Apr 25th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. ## error
  2. NoMethodError in InvoicesController#print
  3.  
  4. You have a nil object when you didn't expect it!
  5. You might have expected an instance of Array.
  6. The error occurred while evaluating nil.each
  7. lib/invoice_drawer.rb:58:in `draw'
  8. lib/invoice_drawer.rb:36:in `new'
  9. lib/invoice_drawer.rb:36:in `draw'
  10. app/controllers/invoices_controller.rb:81:in `print'
  11.  
  12. ## /lib/invoice_drawer.rb
  13. class InvoiceDrawer
  14. def self.draw(invoice)
  15. pdf = PDF::Writer.new(:paper => "A4")
  16. pdf.select_font "Times-Roman"
  17. pdf.text("Page One Technologies Pty. Ltd.", :font_size => 16, :justification => :left)
  18. pdf.text("A.B.N 85 074 597 103", :font_size => 8, :justification => :left)
  19. pdf.text("Suite 4, No. 1 Shaw Road", :font_size => 10, :justification => :left)
  20. pdf.text("Ingleburn NSW 2565", :font_size => 10, :justification => :left)
  21. pdf.text("Ph: (02) 9829 3151 Fax: (02) 9829 2713", :font_size => 10, :justification => :left)
  22. pdf.text("\n")
  23. pdf.text("Job/Invoice ##{"%05.0f" % invoice.id}", :font_size => 10, :justification => :right)
  24. PDF:: SimpleTable.new do |billto|
  25. billto.column_order.push(*%w(billto))
  26.  
  27. billto.columns["billto"] = PDF::SimpleTable::Column.new("billto") { |col|
  28. col.heading = "BILL TO"
  29. }
  30. billto.show_lines = :outer
  31. billto.show_headings = true
  32. billto.position = :left
  33. billto.orientation = 20
  34. billto.shade_rows = :none
  35. billto.width = 170
  36. data = [
  37. { "billto" => "#{invoice.client.name}" },
  38. { "billto" => "#{invoice.client.address}" },
  39. { "billto" => "#{invoice.client.suburb} #{invoice.client.state} #{invoice.client.postcode}" }
  40. ]
  41.  
  42. billto.data.replace data
  43. billto.render_on(pdf)
  44.  
  45. pdf.text("\n")
  46.  
  47. end
  48. PDF:: SimpleTable.new do |invoice|
  49. invoice.column_order.push(*%w(description quantity rate amount))
  50.  
  51. invoice.columns["description"] = PDF::SimpleTable::Column.new("description") { |col|
  52. col.heading = "Description"
  53. }
  54. invoice.columns["quantity"] = PDF::SimpleTable::Column.new("quantity") { |col|
  55. col.heading = "Quantity"
  56. }
  57. invoice.columns["rate"] = PDF::SimpleTable::Column.new("rate") { |col|
  58. col.heading = "Rate"
  59. }
  60. invoice.columns["amount"] = PDF::SimpleTable::Column.new("amount") { |col|
  61. col.heading = "Amount"
  62. }
  63. invoice.show_lines = :outer
  64. invoice.show_headings = true
  65. invoice.position = :left
  66. invoice.orientation = 20
  67. invoice.shade_rows = :none
  68. invoice.width = 792
  69.  
  70.  
  71. !! I recommend this goes in your controller to stop slowing down your pdf exporting.
  72. @activities = @invoices.activities.find(:all)
  73.  
  74. data = @activities.collect do |o|
  75. { "description" => "#{o.comments}", "quantity" => "#{hour_format(o.minutes)}", "rate" => "#{number_to_currency(o.rate)}" }, "amount" => "#{number_to_currency(o.price)}"}
  76. end
  77.  
  78. invoice.data.replace data
  79. invoice.render_on(pdf)
  80. end
  81. end
  82. pdf.render
  83.  
  84. end
  85. end
  86.  
  87. ## invoices_controller.rb
  88. def print
  89. @invoice = Invoice.find(params[:id])
  90. send_data InvoiceDrawer.draw(@invoice), :filename => 'invoice.pdf', :type => 'application/pdf', :disposition => 'inline'
  91. end
Add Comment
Please, Sign In to add comment