How to render format in pdf in rails

后端 未结 5 914
长情又很酷
长情又很酷 2020-12-10 17:50

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

we are using

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

et

5条回答
  •  盖世英雄少女心
    2020-12-10 18:22

    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.

提交回复
热议问题