I need to render my #manufacturers array to pdf, but do it only via click on some link in view...
Now i have such code
def index
#manufacturers = Manufacturer.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #manufacturers }
format.pdf { render :layout => false }
end
end
I see a lot of examples in web, but i didn't found clear and actual example... Just how simple do in a4 pdf table with my array #manufacturers ?
In addition to prawn, use the prawnto rails plugin to help with rendering the PDF as a template.
See https://github.com/prior/prawnto for the plugin and http://railscasts.com/episodes/153-pdfs-with-prawn for how to use it.
[Note: the Report gem currently only generates on letter-size paper, patch for A4 would be welcome!]
You can use the Report gem, which generates PDF using Prawn but also XLSX and CSV.
# a fake Manufacturer class - you probably have an ActiveRecord model
Manufacturer = Struct.new(:name, :gsa)
require 'report'
class ManufacturerReport < Report
table 'Manufacturers' do
head do
row 'Manufacturer report'
end
body do
rows :manufacturers
column 'Name', :name
column 'GSA?', :gsa
end
end
# you would want this so that you can pass in an array
# attr_reader :manufacturers
# def initialize(manufacturers)
# #manufacturers = manufacturers
# end
def manufacturers
[
Manufacturer.new('Ford', true),
Manufacturer.new('Fischer', false),
Manufacturer.new('Tesla', nil),
]
end
end
When you call report.pdf.path, a PDF is generating in the tmp directory:
report = ManufacturerReport.new
puts report.pdf.path #=> /tmp/185051406_Report__Pdf.pdf
puts report.xlsx.path #=> /tmp/185050541_Report__Xlsx.xlsx
You can do it in your controller like:
#manufacturers = Manufacturer.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #manufacturers }
format.pdf do
report = ManufacturerReport.new(#manufacturers) # using the commented-out code
send_file report.pdf.path, :type => 'application/pdf', :disposition => 'attachment', :filename => 'ManufacturersReport.pdf'
# tmp files are periodically cleaned up by the operating system, but if you want to be extra clean you can call
# report.cleanup
# but this may remove the tmp files before apache/nginx/etc. finishes delivering the file
end
end
End result:
PDF
XLSX
Note that the XLSX has an autofilter added for you automatically.
Related
I have problem collection of pdfs file and save it to file. I create this for show action so when I clink the link it generate pdf and store them in public folder:
def show
add_breadcrumb "Inovice details"
respond_to do |format|
format.html
format.pdf do
render :pdf => "file_name", :save_to_file => Rails.root.join('public', "Invoice no. #{#invoice.format_id}.pdf")
end
format.csv {send_data Invoice.where(id: #invoice.id).to_csv,
filename: "Invoice no. #{#invoice.format_id}.csv"}
end
end
Now I want to create the same functionality but for collection of objects. For examples I have 10 invoices and I want for all of them generate pdf and save it to public folder. I was trying something like that:
def index
#invoices = #company.invoices
respond_to do |format|
format.html
format.js
format.csv { send_data #invoices.to_csv }
format.pdf do
#invoices.each do |invoice|
render :pdf => "file_name", :save_to_file => Rails.root.join('public', "Invoice no. #{invoice.format_id}.pdf")
end
end
end
authorize #invoices
end
But it didnt work. I have no ideas how to solve this problem. I will be grateful for every help.
You can not send multiple PDF's in the same request.
I think a best solution is generate the PDF's in a background job ( https://github.com/mileszs/wicked_pdf/wiki/Background-PDF-creation-via-delayed_job-gem ) and present an HTML page with links to all you PDF's.
If that doesn't work for you, you can merge all the content in a big PDF file.
In my Rails 4.1.1 app (which has the jbuilder gem included), json views always output all columns in the table, ignoring the app/views/[model]/*.json.jbuilder files.
In routes.rb, I have:
resources :workshops do
resources :memberships
resources :events
end
In events_controller.rb, I have:
# GET /workshop/:workshop_id/events
# GET /workshop/:workshop_id/events.json
def index
#events = #workshop.events
respond_to do |format|
format.html
format.json { render json: #events }
end
end
I set the #workshop variable in a "before_action" in the controller.
When I visit /workshops/f00/events, it displays in HTML format as expected.
If I make the file, app/views/events/index.json.jbuilder:
json.events do
end
...when I visit /workshops/f00/events.json, I expect that the output would be empty. However, I get the contents of the entire #events in JSON format.
What I would like to see is only particular fields being output, given a app/views/events/index.json.jbuilder that contains:
json.array!(#events) do |event|
json.extract! event, :id, :title, :description
json.start event.starts_at
json.end event.ends_at
json.url workshop_event_url([#workshop, event], format: :json)
end
... but no matter the contents of the .jbuilder file, the output is always the same. Could anyone tell me why my .jbuilder file is being ignored, and how to get it working?
The line format.json { render json: #events } will always render the #events array since the url /workshops/f00/events accepts both html and json formats and you're rendering #events when hitting the url with the json format.
If you want to render the data in app/views/events/index.json.jbuilder change:
respond_to do |format|
format.html
format.json { render json: #events }
end
to:
respond_to do |format|
format.html
format.json
end
By not rendering the #events array you rely on Rails to output whatever is in app/views/events/index.json.jbuilder.
How to convert ruby file in word file i.e (docx file). For pdf, we prawn gem. But is there any gem for word file. I am trying to convert my html file in word file so that it can be editable for user too. What should do in that case ? I was planning to convert that file in word file. Will it be possible or not.
If you are using Rails:
in initializers/mime_types.rb:
Mime::Type.register 'application/vnd.ms-word', :msword
in your controller:
say you want to export show action:
def show
#item = Item.find params[:id]
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #item }
format.msword { set_header('msword', "#{#item.title}.doc") }
format.pdf do
render :pdf => 'Coming soon...', :layout => false
end
end
end
define set_header in application_controller.rb:
def set_header(p_type, filename)
case p_type
when 'xls'
headers['Content-Type'] = "application/vnd.ms-excel; charset=UTF-8'"
headers['Content-Disposition'] = "attachment; filename=\"#{filename}\""
headers['Cache-Control'] = ''
when 'msword'
headers['Content-Type'] = "application/vnd.ms-word; charset=UTF-8"
headers['Content-Disposition'] = "attachment; filename=\"#{filename}\""
headers['Cache-Control'] = ''
end
end
now define a show.msword.erb #you can use any template handler like haml etc.
YOUR HTML HERE TO EXPORT TO DOC
AS LIKE NORMAL ERB TEMPLATE
Use htmltoword gem.
https://github.com/nickfrandsen/htmltoword
it hasn't been updated since November 2015, but works well.
I want to export results generated from my rails app to excel format. distribution_sheet, results and specimens are all models. This is my script in my controller:
def first
#distribution_sheet = DistributionSheet.find(:all, :conditions => ["lifecycle_state = ?","closed"]).last
#results = #distribution_sheet.results
#specimens = #distribution_sheet.specimens
end
include DisplayResultHelper
def show
respond_to do |format|
format.html
format.csv {
#specimens.each do |sp|
send_data(generate_csv([["Lab No","Assay","Batch","Cuttoff"],[sp.id]]),
:filename => "my_data-#{Time.now.to_date.to_s}.csv",
:type => 'text/csv')
end
}
format.xls{
send_data(generate_xls([["Lab No","Assay","Batch","Cuttoff"],[]]),
:filename => 'my_date.xls',
:type => 'application/vnd.ms-excel')
}
end
end
end
This works if I add any words inside the arrays, but once I add sp.id it fails. I want to add the data in sp.id. Any clues?
Mark
I'm not sure which library you're using - but perhaps it requires real strings in those arrays, so try passing in sp.id.to_s instead of just sp.id
i have table like the following
now, i want to export this to excel, so i can open it in ms excel
You can use FasterCSV gem.
You can either use to_csv method.
def index
#records = ....
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #records }
format.csv { #records.to_csv }
end
end
or customize the output and use send_data method in the controller.
format.csv do
csv_string = FasterCSV.generate do |csv|
# header row
csv << ["id", "Column1", "Column1"]
# data rows
#records.each do |r|
csv << [r.id, r.column1, r.column2]
end
# send it to the browser
send_data csv_string,
:type => 'text/csv; charset=iso-8859-1; header=present',
:disposition => "attachment; filename=records.csv"
end
I would advice to use Spreadsheet which is mature. I'm using it with Rails 3 with no problems.
The overall process would be:
book = Spreadsheet::Workbook.new
sheet = book.create_worksheet :name => 'Customers'
sheet.row(0).concat %w{Name Country Acknowlegement}
book.write '/path/to/output/excel-file.xls'
Ruby 1.9 has a built in CSV library with very similar API as the FasterCSV gem (actually the gem got integrated into Ruby!).