0

My Rails app is generating a PDF file in a file system. When the user clicks "Preview" button in the front-end which is in Angular, it generates a PDF file and returns path.

def renderPDF()
 # PDF render codes
 STORAGE_PATH = 'public/rendered'
 return "#{STORAGE_PATH}/example.pdf"
end

In my front-end, I would like to open this PDF when the Preview button is clicked. How would I show the PDF file on the browser?

Kahsn
  • 1,045
  • 3
  • 15
  • 25

1 Answers1

0
def renderPDF
  storage_path = 'public/rendered'
  send_file("#{storage_path}/example.pdf", :filename => 'your_document.pdf', :disposition => 'inline', :type => 'application/pdf')
end
agustaf
  • 683
  • 1
  • 6
  • 19
  • it seems like it's not opening pdf file. Method runs fine tho. – Kahsn Apr 24 '16 at 01:04
  • Ok, so I just tried it and the problem, for me, is that all caps variable are interpreted as constants. See the edit, STORAGE_PATH is changed to storage_path. – agustaf Apr 24 '16 at 01:14
  • I'm wondering why `example.pdf` and `your_document.pdf` are different. Am I not supposed to put `example.pdf` for `filename`? And i tried it but it doesn't do anything. Not sure why. – Kahsn Apr 24 '16 at 01:19
  • If you wanted, but it doesn't matter. If the user downloads the file it will show the filename you choose here which you may or may not want to be different. If you wanted them to be the same you could just exclude filename. http://api.rubyonrails.org/classes/ActionController/DataStreaming.html#method-i-send_file – agustaf Apr 24 '16 at 01:23
  • Gotcha. Any guess why it's not opening the pdf tho? I confirm that the function send_file runs in the console but it doesn't do anything in the browser. – Kahsn Apr 24 '16 at 01:26
  • Did you already change your variable to not all caps? http://ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/constants.html – agustaf Apr 24 '16 at 01:30
  • Yes, I did change everything. – Kahsn Apr 24 '16 at 01:35
  • Hmmm, that's weird, can you add what your server says to your question? – agustaf Apr 24 '16 at 01:40