Raills app is sending a blank pdf - ruby-on-rails

In my rails app I have a STATIC pdf here "../public/camp.pdf" visitors can download it, but when they do the file size is extremely small and the pdf wont open. I think my rails app is sending a blank pdf. Any ideas?
def download_1
send_data '/camp.pdf', :type => 'application/pdf', :filename => "camp.pdf"
end
View:
<%= link_to 'PDF', home_download_1_path %>
EDIT: contents of the pdf is only this "/REGLAS_DE_CARBONO_MEXICO2.pdf"

Edit:
The answer is to use an HTML5 download attribute:

Related

open a url link in a new tab from a Rails controller

I've been trying to open multiple pdf's in new tabs automatically from my rails controller, but nothing has worked so far so I'm back to square one. If anyone could help, it would be much appreciated! Below is an example of what I'm trying to do.
id_array = [1,2,3]
id_array.each do |id|
// I want to open each of these three links in a new browser tab
http://localhost:3000/pdf/id.pdf
end
Cheers!
Can open files in new browser window using send_file method. Use :disposition => 'inline'
Eg: send_file "#{Rails.root}/pdf/id.pdf",:filename => 'id.pdf', :type => 'application/pdf', :disposition => 'inline'
you should respond with a javascript file.
<% id_array.each do |id| %>
window.open('<%= "http://localhost:3000/pdf/#{id}.pdf" %>', '_blank');
<% end %>

Opening pdf data as image in rails views

I have used Rails 4,
views
<div>
<%=image_tag(some_method_path), class: 'image-view' %>
</div>
controller method
def some_method
#label_image = Base64_incoded image hex
send_data #label_image, :type => 'application/pdf', :disposition => 'inline'
end
The image is not opening in view but it is opening as pdf if we run the url in window.
How to display the pdf as image in the div ?
Any help appreciated
As far as I understood, what you are trying to do is to render a preview into an image tag of a PDF document. However, this is not an automatic task, and your browser won't magically perform the conversion for you.
You need a library to render the PDF and capture a preview. This is not a Rails problem, it's a generic PDF conversion problem.
Note. This question is very similar to How to have pdf viewer in ruby

When using redirect_to files on s3. How to either display an image or download a file

In Rails 3 AttachmentsController, I have the following:
def show
attachment = Attachment.find_by_id(params[:id])
redirect_to(attachment.authenticated_url())
end
Where authenticated_url is simply a URL to S3 to access the file.
The problem is that the file is always downloaded by the browser. What I would like to have happen is if the file is an image/pdf, something the browser can render, show the file in the browser and only download non-browser friendly files.
Have you seen this before? Any ideas on where to start?
Thanks
send_file can be used for remote url as well
file = open("http://cdn2.example.com/somefile.pdf")
send_file(file, :filename => "example.pdf", :type => "application/pdf" , :disposition => "attachment")
Here example.pdf will be downloaded. If you want open pdf in browser itself use this
file = open("http://cdn2.example.com/somefile.pdf")
send_file(file, :filename => "example.pdf", :type => "application/pdf" , :disposition => "inline")
redirect_to #attachment.url
I'm using Paperclip as well, and this display pictures inside the browser. Do you have to do something different than this?
I think you'll want to look into the rails send_file method.
1) http://apidock.com/rails/ActionController/DataStreaming/send_file
The :disposition option lets you decide whether a file will be downloaded or displayed inline. I used this in my rails app to let users download mp3 files.
Hope this helps.

Download pdf instead of showing it in the browser

I am using pdfkit on my rails application (I did this according to Ryan Bates railscast)
Currently it's showing the pdf inside the browser.
How can the pdf be downloaded instead ?
Thanks
EDIT:
Thanks for the response!
But how can I create the data of the pdf in the controller?
The pdf shows up automatically when the url has .pdf. for example when going to this link:
http://localhost:3000/home/index.pdf
It opens a pdf version of the the page automatically on the browser (it's a rails middleware setup).
Try using the send_data method that all rails controllers have.
Something like this:
# Generate a PDF document with information on the client and return it.
# The user will get the PDF as a file download.
def download_pdf
send_data(generate_pdf, :filename => "my_file.pdf", :type => "application/pdf")
end
Assuming it a simple rack end point:
class PdfEndpoint
def call(env)
[200, {"Content-Type" => "application/pdf", "Content-Disposition" => "attachment", "filename" => "filename.pdf"}, pdf_data]
end
end
Also the following Railscast will probably help:
http://railscasts.com/episodes/151-rack-middleware

Download Image attached using Paperclip

Is there a way to make the users download the images attached using paperclip?
I just made a link like this:
link_to 'imagename', image.attachment.url
But this makes the browser open the image. I want the browser to ask the user to save the image. Is there a way to do that?
When it comes to sending a file you can find all information in here http://api.rubyonrails.org/classes/ActionController/Streaming.html There are most important cuts:
Simple download:
send_file '/path/to.zip'
Show a JPEG in the browser:
send_file '/path/to.jpeg', :type => 'image/jpeg', :disposition => 'inline'
Show a 404 page in the browser:
send_file '/path/to/404.html', :type => 'text/html; charset=utf-8', :status => 404
to use one of this option you have to create a new action in controller like this:
class SomeController < ApplicationController
def download_file
send_file image.attachment.path
end
end
Here's a simple solution using the HTML5 download attribute
<%= link_to image.name, image.attachment.url, download: image.attachment.original_filename %>

Resources