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 :)