How to Display a PDF in Ror (Ruby)

How do I display a PDF in ROR (Ruby)?

In your controller:

def pdf
pdf_filename = File.join(Rails.root, "tmp/my_document.pdf")
send_file(pdf_filename, :filename => "your_document.pdf", :type => "application/pdf")
end

In config/environment/production.rb:

config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache

or

config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx

The config modification is required because it enables the web server to send the file directly from the disk, which gives a nice performance boost.

Update

If you want to display it instead of downloading it, use the :disposition option of send_file:

send_file(pdf_filename, :filename => "your_document.pdf", :disposition => 'inline', :type => "application/pdf")

If you want to display it inline, this question will be much more complete that I could ever be.

Display a converted PDF to a rails page

To display the image, the browser only need a URL to the image. If you don't want to store the image on your HDD, you can encode the image into a data URL.

...
scaled_page = first_page.scale(300, 450)

# Set the image format to png so that we can quantize it to reduce its size
scaled_page.format('png')

# Quantize the image
scaled_page = scaled_page.quantize

# A blob is just a binary string
blob = scaled_page.to_blob

# Base64 encode the blob and remove all line feeds
base64 = Base64.encode64(blob).tr("\n", "")

data_url = "data:image/png;base64,#{base64}"

# You need to find a way to send the data URL to the browser,
# e.g. `<%= image_tag data_url %>`

But I highly recommend that you persist the thumbnails on your HDD or better on a CDN because such images are hard to generate but are frequently accessed by the browsers. If you decided to do so, you need a strategy to generate unique URLs to those thumbnails and a way to associate the URLs to your PDF files.

Display pdf file inline in Rails app

My guess would be that it's not displaying because you're trying to display a pdf in an image tag. It's not an image it's a pdf.

Either display it in an iframe or put it in a link that displays just the pdf using send_file and a disposition of inline.

I create pdfs with the princely plugin and they display inline using this method.

Show PDF in Ruby on Rails assets

Change your link

<%= link_to "document", asset_path(document.file.url) %>

To

<%= link_to "document", document_path(document) %>

Then in documents_controller.rb

def show
send_file(document.file.url, :filename => "your_document.pdf", :disposition => 'inline', :type => "application/pdf")
end

Check this question in case it asks to download the PDF file

Forcing the inline rendering of a PDF document in Rails

Ruby on rails : Display a pdf file

You do not have to define a route to it, just link to it properly just like you would an image

Your url linking to the asset is incorrect. Remove the /pulbic so it looks like below.

'/uploads/6/test.pdf'

Display attached pdf in browser within iframe - rails

yes, there is a gem is named Prawn in github. Check it out.

But there is more flexible way to implement this feature with Google Docs. You can embed your pdf with google docs viewer.

Simply:

<a href="https://docs.google.com/gview?url={YOUR_PDF_URL}&embedded=true">Your Pdf Viewer</a> 

Thanks.

trying to display pdf in view in ruby on rails app

After compiling the assets, your pdf is stored here

{Rails.root}/public/assets/Portfolio.pdf

and it's web path is

/assets/Portfolio.pdf

Therefore you may need to use

Rails.root.join("public", "assets", "Portfolio.pdf")

How to render pdf into web browser in RoR

You can't call render that way. It's expecting very specific options. In this case it probably looks like:

pdf_content = HTTParty.get(params[:url])
send_data(pdf_content, disposition: 'inline', type: 'application/pdf')

As a note, you probably want to limit what sorts of things that tool fetches or someone will eventually abuse it.



Related Topics



Leave a reply



Submit