Wicked_PDF Download Only - Rails 3.1 - ruby-on-rails

I currently run Rails 3.1 and am using the latest version of Wicked_pdf for PDF generation.
I have set everything up correctly, and PDFs are generated just fine.
However, I want the user to be able to click a button to DOWNLOAD the pdf. At the present time, when clicked, the browser renders the pdf and displays it on the page.
<%= link_to "Download PDF", { :action => "show", :format => :pdf }, class: 'button nice red large radius', target: '_blank'%>
My Controller.
def show
#curric = Curric.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #curric }
format.pdf do
render :pdf => #curric.name + " CV",
:margin => {:top => 20,
:bottom => 20 }
end
end
end
I have been pointed towards send_file, but have absolutely no idea how to use it in this scenario.
Any help is appreciated.

Decomposition the config you need to set as 'attachment', example:
respond_to do |format|
format.pdf do
render pdf: #curric.name + " CV",
disposition: 'attachment'
end
end

I am doing it that way:
def show
#candidate = Candidate.find params[:id]
respond_to do |format|
format.html
format.pdf do
#pdf = render_to_string :pdf => #candidate.cv_filename,
:encoding => "UTF-8"
send_data(#pdf, :filename => #candidate.cv_filename, :type=>"application/pdf")
end
end
end
and it works for me ;-)

Let's try:
pdf = render_to_string :pdf => #curric.name + " CV",
:margin => {:top => 20,
:bottom => 20 }
send_file pdf

//Download pdf generated from html template using wicket_pdf gem
pdf = render_to_string :template => "simple/show" // here show is a show.pdf.erb inside view
send_data pdf

Related

Rails generating PDF File from current Page

What I want to do is, generate a PDF file from my current page. (HTML)
I call a controller function which generates my page, it fetches data from database and so on.
Now I want a button which saves the current rendered page as a PDF file on the visitors machine. How can I do that?
If I use your code like this:
respond_to do |format|
format.html { render :template => "user/settings" }
format.pdf {
kit = PDFKit.new('http://google.com')
kit.stylesheets << "#{Rails.root}/public/stylesheets/pdf.css"
send_data(kit.to_pdf, :filename=>"sdasd.pdf",
:type => 'application/pdf', :disposition => 'inline')
}
end
and reload the page ... nothing gonna be downloaded as a pdf ... why?
You can create a similar template for pdf of your current page. Then you can use pdfkit gem.
Add this gems to your gemfile:
gem "pdfkit"
gem "wkhtmltopdf-binary"
Add something like this at your controller:
def show
#user= User.find(params[:id])
respond_to do |format|
format.html { render :template => "users/show" }
format.pdf {
html = render_to_string(:layout => false , :action => "show.pdf.erb") # your view erb files goes to :action
kit = PDFKit.new(html)
kit.stylesheets << "#{Rails.root}/public/stylesheets/pdf.css"
send_data(kit.to_pdf, :filename=>"#{#user.id}.pdf",
:type => 'application/pdf', :disposition => 'inline')
}
end
end
Now you can download pdf by adding .pdf to your controller route. eg: controller/routes/path/1.pdf
Or if you don't like it you can do
kit = PDFKit.new('http://google.com')

How to render XML template and then use SEND_DATA in Ruby on Rails 3.2.8?

Can anybodyes help me with XML template rendering and send_data?
I have a controller:
def show
#calculation = Calculation.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #calculation }
format.xml {send_data( :partial=>show.xml.erb, :filename => "my_file.xml" ) }
format.pdf { render :format=>false}
end
end
But I have many errors with "stack level too deep"
If I use
{send_data( #calculation, :filename => "my_file.xml" ) }
I get XML file, but not from my template...
EDIT: I've got a way!
format.xml do
stream = render_to_string(:template=>"calculations/show" )
send_data(stream, :type=>"text/xml",:filename => "test.xml")
end
And all works properly!
Copying the answer from the edited question body in order to remove this question from the "Unanswered" filter:
format.xml do
stream = render_to_string(:template=>"calculations/show" )
send_data(stream, :type=>"text/xml",:filename => "test.xml")
end
~ answer per Dmitry
You can try this with Rails 5
respond_to do |format|
format.html do
stream = render_to_string(:template => "calculations/test.xml.builder")
send_data stream,
:type => 'text/xml; charset=UTF-8;',
:disposition => "attachment; filename=test.xml"
end
end

Prawn, prawnto and templates

sorry for my english.
I am trying to user prawn and prawnto on my application. I have a pdf file to use as template, the pdf file has only one page and that page just has a header and a footer, then, I have this on my controller:
def index
#search = User.search(params[:search])
#users = #search.paginate(:page => params[:page])
respond_to do |format|
format.html # index.html.erb
format.json { render json: #users }
format.xml { render xml: #users }
format.xlsx { export2xlsx(#search.relation.to_xlsx :columns => [:cod_cia, :cod_emp, :login, :email]) }
format.pdf { render :layout => false }
prawnto :prawn => { :template => "#{Rails.root}/app/assets/pdfs/template1.pdf" }
end
end
All good, except that the template is rendered only at the first generated page, the other pages has not the template.
Somebody know how I can get the template repeat in my all generated pages?
Thk in advance.
Regards.
Not sure about prawnto, but with prawn you can tell it to not auto create the first page. Then add each page manually with a template.
filename = "/path/to/template.pdf"
Prawn::Document.generate("output.pdf", :skip_page_creation => true) do
start_new_page(:template => filename)
text "First page"
start_new_page(:template => filename)
text "Second page"
end

How to set prawn pdf filename in Ruby on Rails?

What i have is the controller action responding to html and pdf file format like this:
def detail
#record = Model.find(params[:id])
respond_to do |format|
format.html # detail.html.erb
format.pdf { render :layout => false } #detail.pdf.prawn
end
end
but when i get the file it comes with the name: 1.pdf 2.pdf depending on the params[:id] how do i set the filename to myfile.pdf
--UPDATE--
Example of my detail.pdf.prawn file
pdf.font "Helvetica"
pdf.image open("http://localhost:3000/images/myImage.png"),:position => :left,:width=>100
pdf.text "some text"
pdf.table(someData,:cell_style => { :border_width => 0.1,:border_color=> 'C1C1C1' }) do |table|
table.row(0).style :background_color => 'D3D3D3'
table.column(0..1).style(:align => :left)
table.column(2..4).style(:align => :center)
table.column(0).width = 100
table.column(1).width = 250
table.column(3..4).width = 68
table.row(2).column(0..2).borders = []
table.row(2).column(3).style(:font_style => :bold, :align => :right)
end
and the format.pdf { render :layout => false } in the controller renders de pdf file with the instructions on detail.pdf.prawn
To elaborate on fl00r's answer, If your using prawnto, the pdf setup params can go in your controller, including the filename.
def detail
#record = Model.find(params[:id])
prawnto :prawn => { :page_size => 'A4',
:left_margin => 50,
:right_margin => 50,
:top_margin => 80,
:bottom_margin => 50},
:filename => #record.name, :inline => true #or false
respond_to do |format|
format.html # detail.html.erb
format.pdf { render :layout => false } #detail.pdf.prawn
end
end
If you are creating lot's of different pdf's with prawnto, you would probably move the config out into it's own method. but if your only doing the one, in the controller is fine.
NOTE: the PDF url will still display e.g. 1.pdf But when they save the PDF the filename param will show up in the save box dialog.
prawnto :filename => "myfile", :prawn => {
...
}
Really thanks, this post really help my old pdf way. There is another way to using prawn of Rails. You can check oh this link. Not mine, but a good tutorial for making it. Just saying probably for anyone that still confuse how to make it.
I was using this method before, then I moved on method from that link. Really fun for doing some research about it.
If prawn-rails or prawn_plus you may do following in controller.
headers["Content-Disposition"] = "attachment; filename=\"myfile.pdf\""

Rails Ajax single line RJS files

Everything I am reading about rails 3 and AJAX says that we should have
respond_to do |format|
#wines = Wine.all(:conditions => {:category => "Sparkling"})
format.js
end
and then a seperate js.erb file that is
$("wines").update("<%= escape_javascript(render :partial => "sparkling")%>")
but that one line js file seems a little extreme, can I do something like this:
respond_to do |format|
#wines = Wine.all(:conditions => {:category => "Sparkling"})
format.js {render :js => '$("wines").update("<%= escape_javascript(render :partial => "sparkling")%>"')}
end
and then leave out the extra .js.erb file
The problem I see here is a double render (am noob so I'm not sure)? What is the best way to do this?
Inline RJS is a bad practice, but you can use it like this:
def your_action
#wines = Wine.all(:conditions => {:category => "Sparkling"})
respond_to do |format|
format.js {render :update do |page|
page << '$("wines").update("<%= escape_javascript(render :partial => "sparkling")%>"')
end}
end
end
UPD
No, it's not silly to store one more file. It makes your controllers cleaner. Look with your_action.js.erb
# your controller
def your_action
#wines = Wine.all(:conditions => {:category => "Sparkling"})
end
# your you_action.js.erb
$("wines").update("<%= escape_javascript(render :partial => "sparkling")%>"
That is two lines instead of 6 :)

Resources