How to convert existing html file to pdfs in rails 4? - ruby-on-rails

I have an html file generate_invoice.html.erb. I have to provide a button on the same file to convert generate_invoice.html.erb into a downloadable pdf.I have tried using wicked pdf gem and gem 'wkhtmltopdf-binary'
then I also created the config/initializers/wicked_pdf.rb and set the mime types to pdf
my routes is as follows:
match 'orders/:id/invoice' => 'orders#order_invoice', as:
'order_invoice', via: [:put,:post,:get]
My order_controller is as follows
def order_invoice
#base_url = ENV['base_url']
#order = Order.includes(:status, :user, payment:
[:status]).where(id: params[:id]).first
if (['Notary', 'Attestation','Franking'].include?
#order.service.name)
#no_of_copies = ((#order.answers.where(question_id:
[37,15]).length
> 0) ? #order.answers.where(question_id: [37,15]).first.body :
0).to_i
else
#no_of_copies = ((#order.answers.where(question_id:
[37,15]).length
> 0) ? #order.answers.where(question_id: [37,15]).first.body :
0).to_i + 1
end
render 'order_mailer/generate_invoice',layout: false
respond_to do |format|
format.pdf do
render pdf: "order_invoice"
end
end
end
it however throws this error
ActionController::UnknownFormat at /admin/orders/131/invoice
ActionController::UnknownFormat
basically it is pointing at respond_to do |format| line
What am I doing wrong.Please help me rectify this.Also please let me know the next course of action to achieve the pdf format

I assume your order_invoice file has a format of order_invoice.html.erb instead of order_invoice.pdf.erb which should be the correct format.

respond_to do |format|
format.html
format.pdf do
render pdf: "order_invoice"
end
end
and I hope you already have the view file "order_invoice.html.erb"

Related

Rails Prawn UnknownFormat (ActionController::UnknownFormat):

I am new to rails and I am trying to use Prawm to generate pdf's. I am getting this error:
ActionController::UnknownFormat
Here is the code:
def show_deposited_checks
deposit_id = params[:format]
if deposit_id.present?
#payments = Payment.where(:deposit_id => deposit_id)
respond_to do |format|
format.html
format.pdf do
pdf = Prawn::Document.new
pdf.text "Hello World"
send_data pdf.render
end
end
else
#payments = Payment.all.limit(10)
end
end
I an redirecting from here:
redirect_to show_deposited_checks_payments_path(deposit)
I have another method that works just fine so I suspect it has something to do with the redirect and the way the controller is receiving the data. Any help with be greatly appreciated.
I think the problem is that where doesn't execute immediately. There's probably a better way(I'm fairly new to Rails too), but I think this should work:
#payments = Payment.where(:deposit_id => deposit_id).take(10) #or how ever many you want included

Creating collection of pdfs with wicked_pdf

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.

Export MS Word file Rails [duplicate]

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.

Load XML into variable with Rails

This is how I generate XML for purchase model:
# GET /purchases/1
def show
#purchase = Purchase.find(params[:id])
#purchases = Purchase.all
respond_to do |format|
format.html # show.html.erb
format.xml { render :action => "something.xml.builder", :layout => false }
end
end
Now I'd like to get this rendered XML as string into variable so I could post it to WebService.
How can I get XML through sales_invoice.xml.builder without rendering it?
I don't want use dirty hacks and loading XML from http://appurl/purchases/1.xml
Thanks!
What I was looking for was render_to_string method.

Using Prawn & Prawnto for Rails PDF generation

This is probably more a design or usage question but the main issue is using the Prawn plugin with Rails 2.3.4 and accessing the resulting PDF object to render it to a file.
The scenario is basically;
a controller with the respond_to block setup
a view with code for rendering the text, graphics etc to PDF
It looks like:
From Customer Controller
def show
#customer = Customer.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #customer }
format.pdf { render :layout => false }
end
From Customer View
pdf.text "Hello World"
pdf.text "Customer is #{#customer.name}"
This works fine, producing a PDF file in response to /customers/1.pdf, as expected with a PDF file being generated.
One of the other requirements is to render the pdf to a file and store a copy on the server. Prawn provides the method:
pdf.render_file {path_tofile}
So now if I include this in the View code I can of course save the file on the server. But I'd like to manage this in the controller, as it's related to logic, not view per se.
The first attempt was :
def show
#customer = Customer.find(params[:id])
#pdf = Prawn::Document.new()
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #customer }
format.pdf { render :layout => false }
pdf.render_file {path_to_file}
end
From Customer View
pdf.text "Hello World"
pdf.text "Customer is #{#customer.name}"
PROBLEM
The problem with this attempt is that the PDF is not rendered. I suspected the Controller instance variable is clashing with the Prawnto Plugin pdf variable. But even changing the PDF variable didn't work.
Any suggestions ?
This is how I use prawn in my rails app: http://yob.id.au/2009/05/30/prawn-and-x-accel-redirect.html - it doesn't use prawnto at all.
You can ignore the X-Accel-Redirect stuff unless you use nginx.
Alternatively, another one of the prawn core devs has put together this guide: http://wiki.github.com/sandal/prawn/using-prawn-in-rails

Resources