Mohammad Sadiq Shaikh
Mohammad Sadiq Shaikh

Reputation: 3200

How to render format in pdf in rails

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

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

Upvotes: 7

Views: 35040

Answers (5)

Samiron
Samiron

Reputation: 5317

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.

Upvotes: 7

silverdr
silverdr

Reputation: 2172

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"

Upvotes: 15

Samiron
Samiron

Reputation: 5317

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.

Upvotes: 7

Myst
Myst

Reputation: 19221

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.

Upvotes: 0

Samiron
Samiron

Reputation: 5317

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.

Upvotes: 5

Related Questions