#153 PDFs with Prawn
Mar 16, 2009 | 10 minutes | Plugins
Prawn is an excellent Ruby library for generating PDF documents. Learn how to use it along with the Prawnto plugin in this episode.
- Download:
- source codeProject Files in Zip (121 KB)
- mp4Full Size H.264 Video (14.6 MB)
- m4vSmaller H.264 Video (10.2 MB)
- webmFull Size VP8 Video (27.7 MB)
- ogvFull Size Theora Video (20.4 MB)
There is a newer version of this episode, see the revised episode.
Resources
bash
sudo gem install prawn
script/plugin install git://github.com/thorny-sun/prawnto.git
sudo gem install prawn script/plugin install git://github.com/thorny-sun/prawnto.git
views/orders/show.pdf.prawn
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
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
orders_controller.rb
prawnto :prawn => { :top_margin => 75 }
def show
@order = Order.find(params[:id])
end
prawnto :prawn => { :top_margin => 75 } def show @order = Order.find(params[:id]) end
rhtml
<p><%= link_to "Printable Invoice (PDF)", order_path(@order, :format => 'pdf') %></p>
<p><%= link_to "Printable Invoice (PDF)", order_path(@order, :format => 'pdf') %></p>

