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\""
Related
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')
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
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
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 :)
Ruby 1.8.7, Rails 3.0.4, PDFKit 0.5.0
I'm trying to create a PDF with PDFKit without using the middleware so I can disable javascript (there's an accordion action in there that hides a lot of info that should be on the PDF). However, whenever I try, it fails because it says the partials in my view (show.html.erb) are missing:
Missing partial programs/details with {:locale=>[:en, :en], :formats=>[:pdf], :handlers=>[:erb, :rjs, :builder, :rhtml, :rxml]}
If I remove the references to the partials, it works fine. I've also tried putting the partials in the same directory with show.html.erb to no avail. Here is the code in my controller's show action:
respond_to do |format|
format.html # show.html.erb
format.pdf {
html = render_to_string(:template => "show.html.erb")
kit = PDFKit.new(html, :disable_javascript => true )
send_data(kit.to_pdf, :filename => "test_pdf", :type => "application/pdf", :disposition => 'attachment')
}
end
Is there any way to do this and keep the partials?
EDIT: for now I've done this:
# config/initializers/pdfkit.rb
PDFKit.configure do |config|
config.default_options = {
:page_size => 'Legal',
:print_media_type => true,
:disable_javascript => true
}
end
This has the disadvantage of turning off javascript for every PDF I generate, but it'll do for now. Any answers on the original question of getting the partials to work still with render_to_string still appreciated.
I was running into this issue this morning and came across your question while looking for a solution.
Controller extract:
respond_to do |format|
format.html
format.pdf {
html = render_to_string(:layout => false , :action => "constitution.pdf.haml")
kit = PDFKit.new(html)
kit.stylesheets << "#{Rails.root}/public/stylesheets/pdf.css"
send_data(kit.to_pdf, :filename => "#{#organisation_name} Constitution.pdf",
:type => 'application/pdf', :disposition => 'inline')
return
}
end
constitution.pdf.haml extract:
=render :partial => 'shared/constitution'
Error:
Missing partial shared/constitution with {:locale=>[:en, :e ...
After a while banging my head against a wall, I had a guess and changed constitution.pdf.haml to:
=render :partial => 'shared/constitution.html.haml'
I only know a tiny amount about Rails. Could it really be that (unlike my normal Haml views), PDFKit requires the file extension? That's fixed it for me!
You can also set :formats for render_to_string to prevent having to change your partial names.
html = render_to_string(:layout => false , :action => "show", :formats => :html)
This forces html, instead of pdf, format for the remained of the view rendering. Allowing you to use the same views/partials without change for HTML and PDF responses.
You should specify full path to your template I think:
html = render_to_string(:template => "my_view_folder_name/show.html.erb")