Ok, it ‘s turn for PDF file in Rails.
The wicked_pdf is the best gem I have seen for generating pdf file. It is powerful, versatile and flexible. It also provides the capability of generate PDF from HTML file. This is just what I want.
The
There are also many options which provides huge flexibility to users to customize the output PDF file.
Because wicked_pdf is a wrapper for wkhtmltopdf, you’ll need to install that in Gemfile, too.
3. Remove all the
It’s done!
Further reading: How To Create PDFs in Rails
The wicked_pdf is the best gem I have seen for generating pdf file. It is powerful, versatile and flexible. It also provides the capability of generate PDF from HTML file. This is just what I want.
The
wicked_pdf
utilizing the shell utility wkhtmltopdf to serve a PDF file to a user from HTML. There are also many options which provides huge flexibility to users to customize the output PDF file.
Installation
put it in Gemfilegem 'wicked_pdf'
Likewise, bundle install
. Then, execute the initializer.$rails generate wicked_pdf
It will create app/config/initializers/wicked_pdf.rb
. Normally, nothing has to do with it if higher version of Rails was used. (My Rails version is 5)Because wicked_pdf is a wrapper for wkhtmltopdf, you’ll need to install that in Gemfile, too.
gem 'wkhtmltopdf-binary'
Of course, then bundle install
.Usage
1.Put the code in theresponse_to
section of the controller. Like:def show
@orderdetails = @order.orderdetails.includes(:model, :size, :color)
respond_to do |format|
fn = "order_#{@order.po_number}_#{Time.now.strftime("%Y-%m-%d %H:%M:%S")}"
format.pdf { render pdf: fn, disposition: 'attachment'}
end
end
2.Copy the show.html.erb
to a new file and rename it as show.pdf.erb
. 3. Remove all the
link_to
tags in the file to prevent them from showing in the pdf file.It’s done!
Further reading: How To Create PDFs in Rails
No comments:
Post a Comment