Change browser tab title to file name when send_file pdf - ruby-on-rails

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})

Related

Rails custom browser's tab title on send_data

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

Why does adding ":remote => true" cause my file download not to launch?

I’m using Rails 5. I have set up this link for downloading a file
<%= link_to image_tag("cc_icon.png"), scenario_download_cc_path(subscription.scenario), :remote => true %>
which links to this controller method
def download_cc
scenario = Scenario.find(params[:scenario_id])
send_data scenario.cc_data, filename: "#{scenario.title}.imscc", type: 'application/zip', :disposition => 'attachment'
end
But when I click on the link above, even though I see a remote call being made, no file download appears in my browser (I’ve tried this on both Chrome and Firefox). How do I make this work? Note, removing “remote => true” is not an option because then the URL in my address bar changes, which I don’t want.
I've not seen the remote option used with file downloads. It's mostly designed for AJAX form posting and URL hits like deleting a record or action calls that make sense to do over AJAX.
If you want the link to force a "Save As..." dialog, use the download attribute in the link. A 'bare' download attribute will force the "Save As..." dialog. Its value will be the download filename. So <a href="/my/download/action" download="file_100.zip"> opens a dialog prompting you to save file_100.zip.

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!

Multiline graph in PDF file using ruby on rails

I want to give export option in my application that will export PDF file and it should consist multi-line graph. How can i do this in ruby on rails.
It depends on your situation, and what content you already have (if any) which you want to render your PDF based on. Some existing options are:
PDFKit, which can create PDFs based on your existing HTML documents
Prawn, which is a lightweight Ruby API for creating PDFs
Some options (like PDFKit above, and also Wicked PDF) will rely on an external tool like wkhtmltopdf to render existing HTML pages as PDFs, whereas other options (like Prawn) are more Ruby native, so it really depends on your situation which path your should take in your application.
As for the graph you mentioned, if you already have it being rendered as an image file, it might be easiest to use one of the HTML-to-PDF generation options, but if you're wanting to render the graph custom in the PDF context and don't already have it as a standalone image, something like Prawn may be best.
In my application, i am using Highcharts.
I have found great success in rendering charts to pdf using a combination of 2 gems:
wicked_pdf - https://github.com/mileszs/wicked_pdf
and
wkhtmltopdf-binary - https://github.com/zakird/wkhtmltopdf_binary_gem
The second is the binary dependency for wicked_pdf.
It can render your rails view in pdf format.
Edit:
I have used PDFkit as well, code is given below from my controller method:
Onclick of "Download PDF" button:
if params[:commit]=="Download PDF"
html = render_to_string(:layout => false , :action => "controller_name/pdf_chart_erb_file.html.erb")
kit = PDFKit.new(html)
kit.stylesheets << "#{Rails.root}/app/assets/stylesheets/application.css"
send_data(kit.to_pdf, :filename => "my_chart.pdf", :type => 'application/pdf', :disposition => 'inline')
file = kit.to_file("#{Rails.root}/app/exe.pdf")
end
You can use or render your graphs in pdf_chart_erb_file.html.erb, it simply converts all your html file to PDF file as per code given above.

Question about Paperclip for Rails

I'm using Papeclip(2.3.1.1) with Rails(2.3.5).
In my view I write so:
<%= link_to image_tag(p.attachment.url(:small)), p.attachment.url(:original) %>,
and it becomes into
href="/system/attachments/1/original/1.JPG?1270134617 (for a tag)
src="/system/attachments/1/small/1.JPG?1270134617" (for img tag).
And when I click on the picture, my browser (Firefox) offers me to save or open picture and I want to just open picture in browser without any dialogs.
I think it's because link contains ?1270134617 after file's name. How can I fix it?
This could be related with the mime types.
Go to /config/initializers/mime_types.rb, and add (or uncomment, if it's there) this line:
Mime::Type.register "image/jpg", :jpg, ["image/jpeg", "image/pjpeg"]
Then restart your web server.
Hopefully this will make the picture "show on the browser" instead of "trying to download it".

Resources