How to render format in pdf in rails
Asked Answered
C

5

7

I want the the table to be displayed in pdf format .

we are using

format.html
format.json{render json: @user}

etc for generating html ,json format

Similarly

  • I want to render the page as pdf .

so what steps are there if it is possible in rails???

Chlordane answered 25/8, 2012 at 7:54 Comment(1)
Is it possible to use format.pdf {render :pdf } etcChlordane
B
7

Another thing is Prawn.

It seems more robust and support complex situations. So you may choose this to work with something sophisticated. Get the https://prawnpdf.org/manual.pdf here. The API docs: https://prawnpdf.org/api-docs/2.3.0/

Thanks Glenn for pointing it out.

Bertram answered 25/8, 2012 at 8:30 Comment(3)
You want to use Prawn? or Wicked_pdfBertram
Prawn but first I want working example I am not understanding the manuals and internet stuff...Chlordane
The link is broken. This prawn? prawnpdf.org/api-docs/2.3.0Ronaldronalda
D
15

Step one:

register PDF type for use in respond_to blocks

# config/mime_types.rb
Mime::Type.register "application/pdf", :pdf

Step two:

in your controller respond to PDF format request

respond_to do |format|
    format.html
    format.json{render json: @user}
    format.pdf do
        # ...
        # here your PDF generating code
        # ...
        send_data(your_generated_pdf, filename: 'your_filename.pdf', type: 'application/pdf')
    end
end

For generating PDF you can use pdfkit with wkthmltopdf, Prawn and some other. Refer to the respective documentations on their usage.

Starting from Rails 5:

Step three:

add gem 'responders' as Rails 5 changelog says: "Remove respond_to/respond_with placeholder methods, this functionality has been extracted to the responders gem"

Deviltry answered 25/12, 2016 at 13:47 Comment(2)
NoMethodError - undefined method `respond_to'Balzer
#38601128Deviltry
B
7

I think this might help [ https://github.com/mileszs/wicked_pdf ]

You will always be able to use like format.pdf as long as your route supports .:format part. So just use that to capture the request for pdf. Then render the pdf using wicked_pdf.

The basic usage is showing there as below

def controller_action
    ... ... ...
    ... ... ...
    format.pdf do
        render :pdf => "file_name"
    end
end

Here is another article might help to understand the usage.

Bertram answered 25/8, 2012 at 8:15 Comment(1)
Thanks for the article on wicked, although it was moved - snikt.net/blog/2012/04/26/wicked-pdfBlakeney
B
7

Another thing is Prawn.

It seems more robust and support complex situations. So you may choose this to work with something sophisticated. Get the https://prawnpdf.org/manual.pdf here. The API docs: https://prawnpdf.org/api-docs/2.3.0/

Thanks Glenn for pointing it out.

Bertram answered 25/8, 2012 at 8:30 Comment(3)
You want to use Prawn? or Wicked_pdfBertram
Prawn but first I want working example I am not understanding the manuals and internet stuff...Chlordane
The link is broken. This prawn? prawnpdf.org/api-docs/2.3.0Ronaldronalda
B
5

Finally its done with prawn and prawn-rails. Here are the details.

To make my system ready

# Add this to your Gemfile
gem 'prawn'
gem 'prawn_rails'

# run
bundle install

I hit the url http://localhost:3000/regions i.e. to hit the index of RegionsController and it shows the page as pdf format.

I have a Region Model with a single attribute name. Has two entries in regions table.

[
 #<Region id: 1, name: "Region 1", ... >,
 #<Region id: 2, name: "Region 2", ... >
]

My Controller Method:

def index
    @regions = Region.all # Should return two region objects :)

    respond_to do |format|
        format.html # index.html.erb
        format.json { render json: @regions }
        format.pdf    # <---------- This will handle the pdf response
    end
end

I have created a view file in views/regions/index.pdf.prawn. Anyway the view file name format is :action.pdf.prawn and the view file is containing

prawn_document() do |pdf|
    @regions.each {|r| pdf.text r.name}
end

This will just output the name of Regions.

Thats it. You have your page in pdf format. Now just play with all other options provided by Prawn and Prawn-Rails. You can find some here - http://prawn-rails-demo.heroku.com/

Just thinking to write a series of blogs guiding how to use different pdf generation tool with rails 3.

Let me know if it solves your problem.

Bertram answered 25/8, 2012 at 14:56 Comment(2)
Why didn't you merge your second answer into the first one?Bryant
hmm... its around two years back but probably because second answer is just like saying "Hi" to Prawn and third answer is doing business with him along with a new partner prawn_rails. Didnt want to mix that up.Bertram
L
0

If you need to use templates, you can always use the combine_pdf gem by itself (it has minor editing capabilities, such as tables and text boxes) or together with any of the aforementioned solutions, such as Prawn.

Len answered 7/5, 2015 at 21:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.