Reputation: 1213
I am trying to figure out how to download a pdf document from the standard html template. I am using wicked pdf gem to generate the PDF.
I followed the instructions in bundle install
and set up the config according to
https://github.com/mileszs/wicked_pdf
following this, I am trying to make it work for a custom route. It keeps rendering HTML
instead of delivering PDF
as specified in the form
What am i doing wrong? Is there a better way to route it to the pdf download?
config/routes.rb
match "/profile" => "vacancies#profile", via: :get
view
<%= form_for(@vacancy, :url => profile_path, :html => {:method => :get, :format => 'pdf'}) do |f| %>
<%= submit_tag "Export Profile"%>
<%end%>
vacancies_controller
def profile
respond_to do |format|
format.html{
render :layout => false
}
format.pdf do
render :pdf => "temp"
render :layout => false
end
end
end
Upvotes: 0
Views: 6350
Reputation: 828
Here is the example that you get the idea for download pdf. Follow these steps.
Gemfile
gem 'wicked_pdf'
Run this command : $ bundle view
= link_to('Download Notes', export_notes_path(id: current_participant.id, format: :pdf), target: '_blank')
config/routes.rb
get '/export_notes/:id' => 'participant/notes#export_notes', as: :export_notes
controller.rb
def export_notes
# Export PDF
respond_to do |format|
format.html
format.pdf do
render pdf: 'notes',
template: 'participant/notes/notes_pdf.html.haml',
dpi: '96',
:show_as_html => params[:debug].present?,
disable_internal_links: true, disable_external_links: true,
:print_media_type => false, :no_background => false
return
end
end
end
participant/notes/
!!!
%html
%head
%meta{"http-equiv" => "Content-Type", :content => "text/html; charset=utf-8"}
%body
#wrap
%header
%main{:role => "main"}
.main-container
.main-container-inner
.main-content
.page-content
.page-header
%h1.text-center
%b Notes
Upvotes: 0
Reputation: 1213
I wrote a second action following some research, now the download link(in view) works
def download
html = render_to_string(:action => :resume, :layout => false)
pdf = WickedPdf.new.pdf_from_string(html)
send_data(pdf,
:filename => "temp.pdf",
:disposition => 'attachment')
end
config/routes.rb
match "/download_profile" => "vacancies#download", via: :get
view
<%= link_to "Export Profile", download_profile_path"%>
Upvotes: 3