Fork me on GitHub
buset mendungnya tebel banget nih pagi2

Rails Tips : Prawnto PDF Generator Dynamic File Name

Posted: December 29th, 2009 | Author: gozali | Filed under: Programming, Rails, Ruby, Work | Tags: , , , , | 1 Comment »

I was getting stuck how to generate dynamic file name for prawn generated pdf file, because there’s lack of example and document here http://cracklabs.com/prawnto/demos

What is Prawnto? Prawnto is is a rails plugin leveraging the new prawn library to produce compiled pdf views, as for me i’m using it for custom reporting tools.

OK, let’s getting back to the problem, what should you do is place prawnto definition in your method rather than in controller’s callbacks :

# /app/controllers/transactions_controller.rb

1
2
3
4
5
6
7
8
9
def show
    prawnto :prawn => { :page_size => 'A4',
                                     :left_margin => 40,
                                     :right_margin => 30,
                                     :top_margin => 40,
                                     :bottom_margin => 30},
                                     :filename => "invoice_#{@transaction.id}_customer_#{@transaction.customer_id}.pdf"
    render :layout => false
end

# /app/views/transactions/show.pdf.prawn

1
<%= link_to "Generate PDF", transaction_url(transaction.id, :format => 'pdf') %>

Hope this is useful, cheers :)

Share and Enjoy:
  • Print this article!
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • E-mail this story to a friend!
  • Turn this article into a PDF!
  • Twitter

One Comment on “Rails Tips : Prawnto PDF Generator Dynamic File Name”

  1. 1 tuteh said at 9:03 PM on December 30th, 2009:

    Happy early NEW YEAR 2010!
    welcome the new day full of smiles and hope,
    Wish the world of blogger still victorious,
    More friendship and brotherhood,
    Wish all the best for us,
    Happy, happy, happy new year 2010!
    SELAMAT TAHUN BARU 2010!

    -tuteh-


Leave a Reply