Thursday, June 24, 2010

Prawnto to generate pdf documents.

You need to install the following
gem install prawn
ruby script/plugin install git://github.com/thorny-sun/prawnto.git

Now add a link in your view

<%= link_to "Printable Invoice (PDF)", order_path(@order, :format => 'pdf') %>

Your controller

prawnto :prawn => { :top_margin => 75 }

def show
  @order = Order.find(params[:id])
end
 
create show.pdf.prawn in your views and add
 
pdf.text "Order ##{@order.id}", :size => 30, :style => :bold

pdf.move_down(30)

items = @order.cart.line_items.map do |item|
  [
    item.product.name,
    item.quantity,
    number_to_currency(item.unit_price),
    number_to_currency(item.full_price)
  ]
end

pdf.table items, :border_style => :grid,
  :row_colors => ["FFFFFF","DDDDDD"],
  :headers => ["Product", "Qty", "Unit Price", "Full Price"],
  :align => { 0 => :left, 1 => :right, 2 => :right, 3 => :right }

pdf.move_down(10)

pdf.text "Total Price: #{number_to_currency(@order.cart.total_price)}", :size => 16, :style => :bold
 
For more information refer: http://railscasts.com/episodes/153-pdfs-with-prawn
For more information on prawnto refer: http://www.cracklabs.com/prawnto 

No comments:

Post a Comment