Adds usefull extensions to your ruby application:
-
Excel (xls and xlsx)
-
Pdf
you can now create Excel and Pdf using a simple view template
Add this line to your application’s Gemfile:
gem 'usefull_extension'
And then execute:
$ bundle
Or install it yourself as:
$ gem install usefull_extension
The gem require acts_as_xls (github.com/skylord73/acts_as_xls) for xls and xlsx support and add pdf
in your controller you can write a render like this:
#app/controllers/users_controller.rb def index @users = User.all respond_do |format format.pdf { render :pdf => "index"} end end
or if you need a quick render, without any template you can write:
... format.xls { render :xls => @users}
the renderer will call to_xls method to the @users and return the file
Note: if you use render :xlsx or :pdf => @users you have to implement your own to_xlsx or to_pdf methods.
you can use xls or xlsx templates as described in acts_as_xls github.com/skylord73/acts_as_xls, or a pdf template
the pdf the template expose the pdf variable you can use as descibed in Prawn site http://prawn.majesticseacreature.com/ #app/views/users/index.pdf.maker pdf.text "Users inspected in pdf..." @users.each { |user| pdf.text user.inspect}
then in your view simply link the page using the right format:
#app/views/users/index.html.erb <%= link_to "pdf", users_path(:format => :pdf) %>
if you need a pdf instance with special options, like templates, you can override the default object :
#app/views/users/index.pdf.maker @pdf = Prawn::Document.new(:page_size => "A4", :margin => [10.28,10,10,10], :template => 'my_template.pdf') pdf.text "Users inspected in pdf..." @users.each { |user| pdf.text user.inspect}
N.B. You have to redefine @pdf non pdf!!
You can pass all renderers common options, plus :filename => “my_file_name.xls” | “my_file_name.xlsx” to change the default filename (file.xls or file.xlsx)
You can watch the test/dummy application to find some working examples
-
Fork it
-
Create your feature branch (‘git checkout -b my-new-feature`)
-
Commit your changes (‘git commit -am ’Added some feature’‘)
-
Push to the branch (‘git push origin my-new-feature`)
-
Create new Pull Request
I want to thanks:
-
Daniel J. Berger, Hannes Wyss, GmbH, creators of the Spreadsheet gem that is the engine of the xls extension
-
randym, creator of the Axlsx gem that powers the xlsx extension ( github.com/randym/axlsx )
-
Gregory Brown, James Healy, Brad Ediger, Daniel Nelson, Jonathan Greenberg, creators of Prown ( prawn.majesticseacreature.com/ )
Extends acts_as_xls, adding pdf support in the same way
This project rocks and uses MIT-LICENSE.