I'm trying to create a downloadable PDF using wicked and Rails API. At the moment I can only get a PDF to download but the contents are empty and the file name is response.pdf.pdf.
This is the method I'm using to generate the PDF when a GET request is made to a specific score.
def download_pdf(score)
html = render_to_string(:action => :show, :layout => "pdf.html.erb", :template => "scores/show.pdf.erb", locals:{:score => score})
pdf = WickedPdf.new.pdf_from_string(html)
send_data(pdf,
:filename => 'test.pdf',
:disposition => 'attachment')
end
I came across this issue today. Quite late but will be useful for people looking for a solution to this.
You will have to inherit from ActionController::Base and not ActionController::API. Or you can override render_to_string with the following code.
def render_to_string(*args)
controller = ActionController::Base.new
controller.locale = I18n.locale
controller.render_to_string(*args)
end
reference: https://github.com/mileszs/wicked_pdf/issues/652
Related
Currently I am generating a pdf using prawn gem of rails. pdf is generated when user hit on this action
def print_pdf
#user = User.find(params[:id])
#details = #user.details_data
respond_to do |format|
format.pdf do
pdf = PrintDetailsPdf.new(#user, view_context, #details)
send_data pdf.render, filename: "#{#user.id}.pdf",
type: 'application/pdf',
disposition: 'inline'
end
end
end
In above action, I generate the pdf and show it in browser and it works perfectly. But I want to show the pdf in browser and also save the pdf at server in public/user_details directory. How can I do that?
There are some good gems that help in file uploading and saving. Here is a list of these gems. Paperclip and Carrierwave are the most popular options.
You could also implement it from scratch. Rails has built-in helpers which make it easy to roll your own solution.
pdf_content = *Content you want to save in the file*
File.open(Rails.root.join('public', 'user_details', filename), 'wb') do |file|
file.write(pdf_content.read)
end
It depends on the complexity of what you want to achieve, but this is totally sufficient for easy file saving tasks. Go here to find more information.
You could do this
pdf_string = render_to_string :template => 'template', :layout => false
File.open("public/userfiles/example.pdf", 'wb') { |
f| f.write(pdf_string) }
end
Render the template to string and then create a file and write that pdf to it..
This should help
I need a quick tip on something which seems really simple. I have some pictures inside private folder and would like to display them inside my View.
The only solution I found was this:
def show
send_file 'some/image/url', :disposition => 'inline', :type => 'image/jpg', :x_sendfile => true
end
I've read that :disposition => 'inline' should not trigger image download and allow me to display it inside my View. The problem is that each time I trigger show action, image download is automatically activated and it is automatically downloaded. View for show action is not displayed.
How can I display that image inside my View? Thank you.
The way I do it, and I'm not saying it's perfectly by the book, is I make a root for images and an action in the controller to render it.
So, for instance, in routes.rb
match '/images/:image', to: "your_controller#showpic", via: "get", as: :renderpic
In your controller:
def showpic
send_file "some/path/#{params[:image]}.jpg", :disposition => 'inline',
:type => 'image/jpg', :x_sendfile => true # .jpg will pass as format
end
def show
end
And in your view
<img src="<%= renderpic_path(your image) %>">
Here is a working example, with fewer parameters on "send_file"
def showpic
photopath = "images/users/#{params[:image]}.jpg"
send_file "#{photopath}", :disposition => 'inline'
end
I think the problem is type. From documentation:
:type - specifies an HTTP content type
So the proper HTTP content type should be image/jpeg instead of image/jpg, as you can see here. Try with:
:type => 'image/jpeg'
You also can list all available types coding Mime::EXTENSION_LOOKUP into a rails console.
Example:
Controller
class ImagesController < ApplicationController
def show_image
image_path = File.join(Rails.root, params[:path]) # or similar
send_file image_path, disposition: 'inline', type: 'image/jpeg', x_sendfile: true
end
end
Routes
get '/image/:path', to: 'images#show_image', as: :image
Views
image_tag image_path('path_to_image')
You would need to have the view use an image_tag to display on the view.
Similar question was raised here: Showing images with carrierwave in rails 3.1 in a private store folder
Right now im using rail 3.0.0 version.now im generate the pdf file and save that file in public folder using wicked_pdf.now i want open that pdf file using controller action.Im using this code in controller.but it is not working.please help me how to do.
def download_prescription_pdf
pdf_pres = UploadedDocument.find(params[:pdf])
send_file "#{RAILS_ROOT}/public/prescription/#{pdf_pres.file_path}", :type => "application/pdf"
end
This works for me:
render :template => 'admin/idreport',
:formats => [:pdf],
:handlers => [:erb],
:pdf => "ID List",
:save_to_file => Rails.root.join('../Documents', "ID List")
Good Luck
Bob
I have an application that needs to support a small set of trusted users uploading new templates. I'll store them in the database or in S3. My question is: how do I tell the controller to render a given template? Of course, I could do it with a manual ERB call:
class MyController < ApplicationController
def foo
template_source = find_template(params[:name])
template = Erubis::Eruby.new(template_source)
render :text => template.result({ :some => #data })
end
end
But then I lose things like helpers and the automatic copying of instance variables.
You could do it using render :inline
render :inline => find_template(params[:name])
I'm trying to display images using my web application written in Rails. I've come across solutions like PaperClip, Attachment Fu etc; but they modify my data model and require to save the image through UI. The problem is that, the images content is not stored using Rails, but a Java Servlet Application. Is there a way to just display the blob content of image to my View.
-Snehal
class ImagesController < ApplicationController
caches_page :show
def show
if #image = Image.find_by_file_name(params[:file_name])
send_data(
#image.file_data,
:type => #image.content_type,
:filename => #image.file_name,
:disposition => 'inline'
)
else
render :file => "#{RAILS_ROOT}/public/404.html", :status => 404
end
end
end
map.connect "/images/*file_name", :controller => "images", :action => "show"
Or something like that.