How to render format in pdf in rails

2019-01-19 07:17发布

问题:

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???

回答1:

Another thing is Prawn.

It seems more robust and support complex situations. So you may choose this to work with something sophisticated. Get the http://prawn.majesticseacreature.com/manual.pdf here.



回答2:

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.



回答3:

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.



回答4:

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.



回答5:

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.