Guest User

Untitled

a guest
Apr 21st, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. ## /app/controllers/invoices_controller.rb
  2.  
  3. def print
  4. @invoice = Invoice.find(params[:id])
  5. send_data InvoiceDrawer.draw(@invoice), :filename => 'invoice.pdf', :type => 'application/pdf', :disposition => 'inline'
  6. end
  7.  
  8. ## lib/invoice_drawer.rb
  9.  
  10. class InvoiceDrawer
  11. def self.draw(invoice)
  12. pdf = PDF::Writer.new
  13. pdf.select_font("Helvetica")
  14. PDF::SimpleTable.new do |tab|
  15. tab.title = "PDF User Unit Conversions"
  16. tab.column_order.push(*%w(from1 to1 from2 to2))
  17. tab.columns["from1"] = PDF::SimpleTable::Column.new("from1") { |col|
  18. col.heading = "From"
  19. }
  20. tab.columns["to1"] = PDF::SimpleTable::Column.new("to1") { |col|
  21. col.heading = "To"
  22. }
  23. tab.columns["from2"] = PDF::SimpleTable::Column.new("from2") { |col|
  24. col.heading = "From"
  25. }
  26. tab.columns["to2"] = PDF::SimpleTable::Column.new("to2") { |col|
  27. col.heading = "To"
  28. }
  29. tab.show_lines = :all
  30. tab.show_headings = true
  31. tab.orientation = :center
  32. tab.position = :center
  33. data = [
  34. { "from1" => "1 point", "to1" => "0.3528 mm",
  35. "from2" => "1 point", "to2" => "1/72”" },
  36. { "from1" => "10 mm", "to1" => "28.35 pts",
  37. "from2" => "", "to2" => "" },
  38. { "from1" => "A4", "to1" => "210 mm × 297 mm",
  39. "from2" => "A4", "to2" => "595.28 pts × 841.89 pts" },
  40. { "from1" => "LETTER", "to1" => "81/2” × 11”",
  41. "from2" => "LETTER", "to2" => "612 pts × 792 pts" },
  42. ]
  43. tab.data.replace data
  44. tab.render_on(invoice)
  45. end
  46. end
  47. end
  48.  
  49. ## The Error
  50.  
  51. NoMethodError in InvoicesController#print
  52.  
  53. undefined method `text_width' for #<Invoice:0x4720f5c>
  54.  
  55. RAILS_ROOT: C:/INSTAN~1/rails_apps/drawbridge
  56. Application Trace | Framework Trace | Full Trace
  57.  
  58. C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/attribute_methods.rb:205:in `method_missing'
  59. C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/pdf-writer-1.1.8/lib/pdf/simpletable.rb:780:in `__find_table_max_width__'
  60. C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/pdf-writer-1.1.8/lib/pdf/writer/ohash.rb:32:in `each'
  61. C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/pdf-writer-1.1.8/lib/pdf/writer/ohash.rb:32:in `each'
  62. C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/pdf-writer-1.1.8/lib/pdf/simpletable.rb:779:in `__find_table_max_width__'
  63. C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/pdf-writer-1.1.8/lib/pdf/simpletable.rb:778:in `each'
  64. C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/pdf-writer-1.1.8/lib/pdf/simpletable.rb:778:in `__find_table_max_width__'
  65. C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/pdf-writer-1.1.8/lib/pdf/simpletable.rb:260:in `render_on'
  66. lib/invoice_drawer.rb:46:in `draw'
  67. lib/invoice_drawer.rb:12:in `new'
  68. lib/invoice_drawer.rb:12:in `draw'
  69. app/controllers/invoices_controller.rb:80:in `print'
Add Comment
Please, Sign In to add comment