5

I have a project named "Project", and a PDF file caled "file.pdf" located in Project/app/assets/file.pdf. I want to render the PDF on a page called "file.html", located in Project/app/views/static/file.html.

I wrote the following code, but all that displays is an empty white box.

<embed src="../../assets/file.pdf" width="500" height="375" type='application/pdf'>

I'm pretty sure my browser can't find the file. What could I be doing wrong?

UPDATE: I also tried it with an image, placed in app/assets/images/Image.png, like so:

<embed src="../../assets/images/Image.png" width="500" height="375" type='image/png'>

but that doesn't render either.

UPDATE 2: Output from console:

Started GET "/file" for 127.0.0.1 at 2016-02-21 04:48:27 -0600 Processing by StaticController#file as HTML

Rendered static/file.html.erb within layouts/application (63.8ms) Completed 200 OK in 757ms (Views: 749.4ms | ActiveRecord: 0.0ms)

Started GET "/assets/application.self-e570ac372b74f676a029186250aeeba9bf7016bcd014314d103b5d18b3eb858e.css?body=1" for 127.0.0.1 at 2016-02-21 04:48:28 -0600

Started GET "/assets/pdf-8f705790d319666d8f804cf8809f4d74f036483049ef8c520646d6c98ebedc5f.png" for 127.0.0.1 at 2016-02-21 04:48:28 -0600

Started GET "/assets/Advanced_Reading_Sample.pdf" for 127.0.0.1 at 2016-02-21 04:48:28 -0600

Joe Morano
  • 1,715
  • 10
  • 50
  • 114

5 Answers5

4

In your controller write,

def file
  pdf_filename = File.join(Rails.root, "/assets/file.pdf")
  send_file(pdf_filename, :filename => pdf_filename, :disposition => 'inline', :type => "application/pdf")
end

Rename your file.html => file.html.erb and place it under views/controller_name/file.html.erb, also add route to this controller action.

In above setup when you access controller/file/ pdf will shown in the browser(inline).

Albert Paul
  • 1,168
  • 13
  • 23
2

Try this and see if it works

<iframe src="yourfile.pdf" style="width:100%; height:700px;" frameborder="0"></iframe>
Omari Victor Omosa
  • 2,814
  • 2
  • 24
  • 46
  • I'm getting a screen that tells me there are no route matches for that path. – Joe Morano Feb 21 '16 at 20:10
  • do you see this part `src="yourfile.pdf"` have you tried to link that part to where your pdf is? e.g. `src="../../assets/file.pdf"` .Try linking that to your file . And if your file is a pdf it has to display. – Omari Victor Omosa Feb 21 '16 at 21:39
  • I tried that as well, but it's still telling me it can't find the route. – Joe Morano Feb 21 '16 at 21:44
  • @JoeMorano check your file it must be pdf or you path might be incorrect, i.e. `src="../../assets/file.pdf"` that path might not be pointing to that file or file might be corrupt leading to white screen. If it doesnt work we can try another way – Omari Victor Omosa Feb 21 '16 at 21:47
  • @JoeMorano What if you copy the pdf to the same folder where the `file.html` file is the try this direct path `src="file.pdf" ` just try. here where i am it is working . – Omari Victor Omosa Feb 21 '16 at 21:50
  • They're in the exact same folder and and I wrote src="file.pdf", but I'm getting the error 'No route matches [GET] "/file.pdf"' – Joe Morano Feb 21 '16 at 22:05
  • @JoeMorano try the same with another file e.g. `src="anotherfile.pdf"` to test if the file is corrupt. and if you have php codes in that page then save the page as `file.php` insted as `file.html` – Omari Victor Omosa Feb 21 '16 at 22:20
1

Create folder called pdf (for example) in app/assets and put there your file.pdf then in app/views/static/file.html change src of embed tag to /assets/file.pdf. This should do the trick.

FixerRB
  • 326
  • 2
  • 11
1

Try, This will help

<a href="../../assets/file.pdf" target="_blank">Click To View</a>

thanks

Mark SykOv

0

Well I have no idea why my app can't find the file. I just ended up using iframes and the absolute url of the file:

<iframe src="http://www.project.com/file.pdf" style="width:100%;height:100%;"><p>Your browser does not support iframes.</p></iframe>

And finally, the file renders.

Joe Morano
  • 1,715
  • 10
  • 50
  • 114