Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDF renders as empty white box

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

like image 832
Joe Morano Avatar asked Feb 17 '16 19:02

Joe Morano


2 Answers

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

like image 174
Albert Paul Avatar answered Nov 09 '22 04:11

Albert Paul


Try this and see if it works

<iframe src="yourfile.pdf" style="width:100%; height:700px;" frameborder="0"></iframe>
like image 2
Omari Victor Omosa Avatar answered Nov 09 '22 04:11

Omari Victor Omosa