Rails custom browser's tab title on send_data - ruby-on-rails

I send inline pdf file with send_data (in a new tab)
send_data row_pdf_data, disposition: 'inline', filename: 'report.pdf', type: 'application/pdf'
and I've got tab with PDF preview with a title with my action name (action which sends this data/file).
Sent Pdf document doesn't have title in the properties.
How can I send and use custom title on the new tab?
Maybe it's possible to send some headers in response or something to change the preview page's title
To be clear. I don't use WickedPDF, Prawn etc... This data/file comes from service and I can't change it on the fly
Thanks in advance

Related

send_file in rails does not download to browser - instead loads in browser window directly [duplicate]

This question already has an answer here:
Force browser to download file instead of opening it
(1 answer)
Closed 5 years ago.
I have the following in my .erb:
<%= link_to 'Download PDF', planners_download_pdf_url %>
I have the following method to respond:
def download_pdf
send_file(
"#{Rails.root}/Thing/ex/example.xls",
filename: "mything.xls",
type: "application/xls"
)
end
My routes has:
get '/planners/download_pdf'
When I click my link, the method does get invoked. My problem is that is doesn't download to the browser. Instead, it takes the tab I have open in the browser and dumps the file as HTML. Like... have you ever seen an Excel file opened in notepad? It does that. See below:
How do I get this to instead download the file?
You need to add disposition: 'attachment' to your send_file option hash (or at least ensure that you do not have disposition: 'inline' set, since 'attachment' should be the default).
If this is not the issue, if you are using Turbolinks, as mentioned in other answers you need to disable Turbolinks on the link by setting data-turbolinks="false" in your link element (e.g.: data: {turbolinks: false} in your link_to tag helper).
Finally, here are some other things to try if this doesn't work:
Set type to 'application/vnd.ms-excel', the valid MIME type for XLS files.
Set the download="mything.xls" html5 attribute on the link tag directly (e.g.: download: 'mything.xls' in your link_to tag helper.

Show image uploaded in rails

I followed this tutorial http://ryan.endacott.me/2014/06/10/rails-file-upload.html
to make an upload images without using gems .
But it does not teach how to display the image.I 've tried several ways , but without success.
Make sure to add disposition: "inline" to the send_data method as well as the type of file in order to display an image instead of downloading it, as documented here. http://api.rubyonrails.org/classes/ActionController/DataStreaming.html#method-i-send_data
def show
#document = Document.find(params[:id])
send_data(#document.file_contents,
type: #document.content_type,
filename: #document.filename,
type: 'image/png',
disposition: "inline")
end
Then in whatever/your/path/is/show.html.erb
<%= image_tag url_for(controller: "documents",
action: "show",
id: #document.id) %>
This article may be helpful (https://www.safaribooksonline.com/library/view/rails-cookbook/0596527314/ch15s04.html):
For a browser to display binary image data, it needs to be instructed that the data is an image. Specifically, it needs to be told that the content type of the data is something like image/gif. Providing a filename gives the browser something to name the data, should it be downloaded and saved by the user. Finally, the disposition specifies whether the file will be displayed inline or downloaded as an attachment. If its disposition is not specified, it’s assumed to be an attachment.
I hope that helps!

Change browser tab title to file name when send_file pdf

In my project I'm using Carrierwave to upload files like doc, and pdf.
In case I want to download a pdf my controller do
send_file #document, :disposition =>
'inline', :type => 'application/pdf'
And this works fine, displays the pdf file in browser and the url I get is /documents/file_name and it's fine too. But I want to set the "headers title" or "url title" so in the browser's tab shows the file_name instead myapp.com/documents/file_name
Is there a simple way to do that?
Unfortunately it is not possible as you describe.
The tab title is taken from the PDF documents metadata, specifically the Title attribute.
If you usually generate your document with Prawn you can do:
Prawn::Document.generate(my_filename, info: {Title: my_title})

How do I export an HTML file with a XLS extension?

I'm trying to generate reports. I already created the HTML table in a view and wanted to follow the approach talked about in "Exporting data to CSV and Excel in your Rails app".
I read:
"On my project I save the html table into a file, but give it the xls extension instead of html. e.g table.xls. I then send it to the browser with the Excel mime type, and Excel will open this file and will also format the table like the html table is, so you'll get boldened headings and cell background colors etc.. "
"The easiest way I know is to generate an HTML file with a table and give it an XLS extension. The HTML charset is respected, you can have linebreaks as much as you want, little or big endian is not an issue, th-tags define the column-headers and there are already plenty of solutions to build the file."
How can I save the HTML table into a file, and give it an .XLS extension to open it in Excel?
Don't know if that's good advice or not. Personally I always stick to csv. But here's how you would do it.
class ExportController < ApplicationController
def export
html = build_html_table()
respond_to do |format|
format.xls { send_data html, :type => 'application/vnd.ms-excel; charset=utf-8; header=present', :filename => 'export.xls' }
end
end
end
Then in config/initializers/mime_types.rb you'll need to append:
Mime::Type.register 'application/vnd.ms-excel', :xls

Forcing the inline rendering of a PDF document in Rails

I'm writing a service that generates PDF files from a set of XML files. The PDF is being correctly generated. However, everytime I click on the "view PDF" link, the browser asks the user to download the PDF file.
I need the PDF to display inline, just like any regular HTML page. I though I wrote the code right, but something must be missing - the browser keeps asking the user to download.
Here's the current code:
class PdfController < Controller
def generate
# stuff
send_data pdf_bytes, :disposition => 'inline', :type => 'application/pdf'
end
end
Any ideas?
Try removing the Content-Disposition header altogether. It's been my experience that Content-Disposition: attachment works pretty well, but many browsers have inconsistent behavior for any other value. If you want to display inline, it might just be better to remove the header and hope for the best. IE seems to have the most problems with this header. (Surprise, surprise.) Just make sure you're still setting Content-Type: application/pdf.
The other option would be to use an iframe and set the src of the iframe to your PDF file. Almost all browsers that support inline PDF viewing will handle this correctly. The downside is that you might end up displaying a blank iframe whereas non-supported browsers would have otherwise done a graceful fallback to simply downloading the PDF.

Resources