i know how to render it in webapp but i'm supposed to generate pdf through an api call , so i need to send url of the pdf generated instead of the pdf itself ( thats what the mobile developer is asking for ) , is there any way to do it ?
like:
respond_to do |format|
format.pdf do
pdf = CustomCertificatePdf.new(current_user, tutorials)
url = pdf.link
# send_data pdf.render, filename: "custom_certificate_#{current_user.first_name.downcase}_#{current_user.last_name.downcase}.pdf",
# type: "application/pdf",
# disposition: "inline"
render :json => {url: url}
end
end
even your slight hint will be appreciated , thanks :)
save pdf to public folder or upload to another server( S3, google drive, dropbox, ...)
then use that link in api controller
if you save pdf file in public folder, the link should be your host name + relative path with public folder
ex:
file_path = "/webapp/public/pdf/custom_certificate.pdf"
the link should be
http://yourhostname.com/pdf/custom_certificate.pdf
If you need protect your file, you should write another controller to serve it with authentication
This is what i ended up doing
pdf = CustomCertificatePdf.new(current_user, tutorials)
#filename = File.join("custom_certificate_#{current_user.first_name.downcase}_#{current_user.last_name.downcase}.pdf")
pdf.render_file #filename
current_user.cust_cert = File.open("custom_certificate_#{current_user.first_name.downcase}_#{current_user.last_name.downcase}.pdf")
current_user.save!
render :json => {url: "#{current_user.cust_cert}"}
in user model
has_mongoid_attached_file :cust_cert,
:default_url => "",
:path => ':attachment/:id/:cust_cert',
:storage => :s3,
:url => ':s3_domain_url',
:s3_credentials => File.join(Rails.root, 'config', 's3.yml')
validates_attachment_content_type :cust_cert, :content_type => [ 'application/pdf' ], :if => :cust_cert_attached?
def cust_cert_attached?
self.cust_cert.file?
end
Related
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
I want to generate pdf file with an image as header in each page
def download_contract
#campaign = Campaign.find_by_id(params['campaign_id'])
html = render_to_string partial: 'campaign_shoppers/contract', locals: {contract: #campaign.contract}
kit = PDFKit.new(html, :header_right => **Here i want to display an image**)
send_data(kit.to_pdf, :filename => "contract_campaign_#{#campaign.title}.pdf", :type => 'application/pdf', :disposition => 'attachment')
end
But I can't generate pdf file with option 'header_right' is image( text is fine).
Any suggestion? Thanks a lot
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've created a file in the tmp directory with the following controller code:
def download
file_path = "#{RAILS_ROOT}/tmp/downloads/xxx.html"
data = render_to_string( :action => :show, :layout => nil )
File.open(file_path, "w"){|f| f << data }
flash[:notice] = "saved to #{file_path}"
end
This creates the file I wanted in the tmp directory, what I want to do is force the user to download that file.
On my local machine, the file is saved to path like:
/Users/xxxx/Documents/Sites/xxxx/Website/htdocs/tmp/downloads/xxxx.html
And on the live server this url will be somthing totally different.
What I was wondering is how do I force the user to download this xxxx.html ?
P.S.
If I put a...
redirect_to file_path
...on the controller it just give's me a route not found.
Cheers.
Take a look at the send_file method. It'd look something like this:
send_file Rails.root.join('tmp', 'downloads', 'xxxxx.html'), :type => 'text/html', :disposition => 'attachment'
:disposition => 'attachment' will force the browser to download the file instead of rendering it. Set it to 'inline' if you want it to load in the browser. If nginx is in front of your Rails app then you will have to modify your environment config (ie. environments/production.rb):
# For nginx:
config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect'
It's easy to confuse file paths with URLs, but it is an important distinction. What has a URL path of /a/b.txt is actually located in the system path #{Rails.root}/public/a/b.txt so you may need to address this by generating both in tandem.
Here's how you might address that:
def download
base_path = "downloads/xxx.html"
system_path = File.expand_path("public/#{base_path}", Rails.root)
url_path = "/#{base_path}"
File.open(file_path, "w") do |f|
f.puts render_to_string(:action => :show, :layout => nil)
end
flash[:notice] = "saved to #{base_path}"
redirect_to(url_path)
end
You cannot redirect to a resource that is not exposed through your web server, and generally only things in public/ are set this way. You can include additional paths if you configure your server accordingly.
You can also side-step this whole process by simply rendering the response as a downloadable inline attachment, if you prefer:
render(:action => :show, :layout => nil, :content_type=> 'application/octet-stream')
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.