Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## /app/controllers/invoices_controller.rb
- def print
- @invoice = Invoice.find(params[:id])
- send_data InvoiceDrawer.draw(@invoice), :filename => 'invoice.pdf', :type => 'application/pdf', :disposition => 'inline'
- end
- ## lib/invoice_drawer.rb
- class InvoiceDrawer
- def self.draw(invoice)
- pdf = PDF::Writer.new
- pdf.select_font("Helvetica")
- PDF::SimpleTable.new do |tab|
- tab.title = "PDF User Unit Conversions"
- tab.column_order.push(*%w(from1 to1 from2 to2))
- tab.columns["from1"] = PDF::SimpleTable::Column.new("from1") { |col|
- col.heading = "From"
- }
- tab.columns["to1"] = PDF::SimpleTable::Column.new("to1") { |col|
- col.heading = "To"
- }
- tab.columns["from2"] = PDF::SimpleTable::Column.new("from2") { |col|
- col.heading = "From"
- }
- tab.columns["to2"] = PDF::SimpleTable::Column.new("to2") { |col|
- col.heading = "To"
- }
- tab.show_lines = :all
- tab.show_headings = true
- tab.orientation = :center
- tab.position = :center
- data = [
- { "from1" => "1 point", "to1" => "0.3528 mm",
- "from2" => "1 point", "to2" => "1/72”" },
- { "from1" => "10 mm", "to1" => "28.35 pts",
- "from2" => "", "to2" => "" },
- { "from1" => "A4", "to1" => "210 mm × 297 mm",
- "from2" => "A4", "to2" => "595.28 pts × 841.89 pts" },
- { "from1" => "LETTER", "to1" => "81/2” × 11”",
- "from2" => "LETTER", "to2" => "612 pts × 792 pts" },
- ]
- tab.data.replace data
- tab.render_on(invoice)
- end
- end
- end
- ## The Error
- NoMethodError in InvoicesController#print
- undefined method `text_width' for #<Invoice:0x4720f5c>
- RAILS_ROOT: C:/INSTAN~1/rails_apps/drawbridge
- Application Trace | Framework Trace | Full Trace
- C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/attribute_methods.rb:205:in `method_missing'
- 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__'
- C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/pdf-writer-1.1.8/lib/pdf/writer/ohash.rb:32:in `each'
- C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/pdf-writer-1.1.8/lib/pdf/writer/ohash.rb:32:in `each'
- 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__'
- C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/pdf-writer-1.1.8/lib/pdf/simpletable.rb:778:in `each'
- 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__'
- C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/pdf-writer-1.1.8/lib/pdf/simpletable.rb:260:in `render_on'
- lib/invoice_drawer.rb:46:in `draw'
- lib/invoice_drawer.rb:12:in `new'
- lib/invoice_drawer.rb:12:in `draw'
- app/controllers/invoices_controller.rb:80:in `print'
Add Comment
Please, Sign In to add comment