0

I am using Ruby2.6.0, Rails5.2.3 and Kibana6.6.1.

I need some help to realise pdf download automatically. Is there any way to download pdf/png automatically using the following url in controller? 'automatically' means users won't feel the file download and don't need to click download icon.

http://localhost:5601/api/reporting/jobs/download/jvhs9dga06559d006260ms70

Edit: Add the code I tried. It helps me to download a file. But I cannot open it with Chrome or sublime. It seems like the file is related to kibana Chromium sandbox (https://www.elastic.co/guide/en/kibana/current/reporting-chromium-sandbox.html). Is anybody know what type of this download file is and how to open it? How to save it as pdf/png file in my local folder?

 File.open('./public/testfile.png', "wb") do |file|
  file.write open("http://localhost:5601/api/reporting/jobs/download/jvhs9dga06559d006260ms70")
 end
landy
  • 29
  • 1
  • 7
  • do you mean when a user hit's a certain controller action you want to download the pdf? – uday May 10 '19 at 09:23
  • Exactly! I want to download the file with the url when user hits a controller action. I added code I tried and problem I met now. – landy May 10 '19 at 09:26
  • i think [this](https://stackoverflow.com/questions/13164063/file-download-link-in-rails) question which you mentioned earlier should actually solve you're problem. Did you tried that solution? If so, did you got any errors? – uday May 10 '19 at 09:32
  • thanks for your reply! I didn't try it. I am not sure if the codes work for me because the codes look like 'generate pdf file with data in db and download that '.pdf' file. But I want to use the above url(without .pdf suffix) to download pdf/png. – landy May 10 '19 at 09:41
  • That answer also explains how to download existing file from `public` folder with this heading `controller (already have pdf):`. I think it works! – uday May 10 '19 at 09:44
  • Sorry for my unclear description. I don't have file in public folder yet. I would like to use the url to download pdf file into public folder. Will the answer work for it? – landy May 10 '19 at 10:48
  • Where is the pdf then? – uday May 10 '19 at 10:54
  • It is in the above url 'http://localhost:5601/.....'. It is not with '.pdf' suffix. I am not sure if I can call it a 'pdf'. The content is 'pdf' and the browser is headless 'Chromium'. I want to save the pdf content in my local public folder. (https://www.elastic.co/guide/en/kibana/current/reporting-chromium-sandbox.html) – landy May 10 '19 at 11:02
  • you can try something like [this](https://stackoverflow.com/questions/2515931/how-can-i-download-a-file-from-a-url-and-save-it-in-rails) answer. Open the file from URL and write it to file in you're desired path. And also you forgot to `.read` the opened file in the above code mentioned. – uday May 14 '19 at 06:12
  • Thanks a lot for your help! 'open-uri' is nice to download the file. And I used 'wget' to download the file with the url successfully. It seems like my previous code didn't write the file correctly. – landy May 14 '19 at 08:22

0 Answers0