# Viewing text docs

**URL:** <https://www.ruby-forum.com/t/viewing-text-docs/87027>\
**Category:** Rails\
**Created:** [February 27, 2007, 6:12pm UTC](https://www.ruby-forum.com/t/viewing-text-docs/87027 "2007-02-27T18:12:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jasongarber](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/977dab/32.png) [@jasongarber](https://www.ruby-forum.com/u/jasongarber)\
**Post date:** [February 27, 2007, 6:12pm UTC](https://www.ruby-forum.com/t/viewing-text-docs/87027/1 "2007-02-27T18:12:24Z")

</div>

I’m building an app that lets users post documents that other users can  
view. The format to the type of document doesn’t hold up with a cut and  
paste so they have to upload. My question is how to display the content  
of a document uploaded to the site without the user having to download  
the file. Any help would be great.

Thanks

J

---

<div class="post-metadata">

**Author:** ![jasongarber](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/977dab/32.png) [@jasongarber](https://www.ruby-forum.com/u/jasongarber)\
**Post date:** [February 27, 2007, 6:27pm UTC](https://www.ruby-forum.com/t/viewing-text-docs/87027/2 "2007-02-27T18:27:43Z")

</div>

it’s going to depend on the type of document.

---

<div class="post-metadata">

**Author:** ![jasongarber](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/977dab/32.png) [@jasongarber](https://www.ruby-forum.com/u/jasongarber)\
**Post date:** [February 27, 2007, 6:37pm UTC](https://www.ruby-forum.com/t/viewing-text-docs/87027/3 "2007-02-27T18:37:44Z")

</div>

I agree with Chris… it can vary very widely depending on the type of  
document… basically, what you need is a way to parse the document on  
the  
server and display it in the page as HTML… HTML being one of the very  
few  
things you can push across the internet and expect someone to receive…  
there are converters for a lot of different document types, you could  
possibly conceivably generate an image of the document somehow, etc…  
but  
it will all hinge on what kind of document it is.

---

<div class="post-metadata">

**Author:** ![jasongarber](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/977dab/32.png) [@jasongarber](https://www.ruby-forum.com/u/jasongarber)\
**Post date:** [February 27, 2007, 7:32pm UTC](https://www.ruby-forum.com/t/viewing-text-docs/87027/4 "2007-02-27T19:32:05Z")

</div>

my experience with handling document assets on the web, be they word  
docs, pdfs, spreadsheets, whatever, is that you should rely on the  
browser and OS to handle the display. No matter how you look at it,  
the file data has to be ‘downloaded’, whether it’s displayed in the  
page as html or via a plugin or opened in Word. all you should worry  
about is setting the correct headers then sending the data.

ex: i click a link to a Word document, Open Office opens to display  
the document. I click on a pdf document and the Acrobat plugin  
displays the document within the browser.

Chris

---

<div class="post-metadata">

**Author:** ![jasongarber](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/977dab/32.png) [@jasongarber](https://www.ruby-forum.com/u/jasongarber)\
**Post date:** [February 27, 2007, 7:21pm UTC](https://www.ruby-forum.com/t/viewing-text-docs/87027/5 "2007-02-27T19:21:20Z")

</div>

To add to Luke and Chris -

Also be aware that it will be your hosting server doing the reading  
before any document is served. e.g. If you upload a powerpoint file from  
a windows machine, the server would need converters/readers to read that  
file type.

Might be easy for a text document. If you are storing the file on the  
server, you could simple use

```
# Return the contents of a file as a string
def file_to_str(filename)
   rv = ""

   if File.exist?(filename) == false
      rv = nil
    else
      File.open(filename, "rb") do |infile|
         rv = infile.read
      end
  end
  rv
end

```

to retrieve the file contents as string and serve it any way you want.

---

<div class="post-metadata">

**Author:** ![jasongarber](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/977dab/32.png) [@jasongarber](https://www.ruby-forum.com/u/jasongarber)\
**Post date:** [February 27, 2007, 7:46pm UTC](https://www.ruby-forum.com/t/viewing-text-docs/87027/6 "2007-02-27T19:46:33Z")

</div>

The docs will be limited to pdf files. So, if the OS handles the  
display, the view should just link\_to what, the file itself or is there  
a method that needs to be called on it to tell the os have the Acrobat  
plugin display it? Sorry for the rookie questions but, I’m a rookie.

Thanks for all the help

J  
Chris H. wrote:

> my experience with handling document assets on the web, be they word  
> docs, pdfs, spreadsheets, whatever, is that you should rely on the  
> browser and OS to handle the display. No matter how you look at it,  
> the file data has to be ‘downloaded’, whether it’s displayed in the  
> page as html or via a plugin or opened in Word. all you should worry  
> about is setting the correct headers then sending the data.
> 
> ex: i click a link to a Word document, Open Office opens to display  
> the document. I click on a pdf document and the Acrobat plugin  
> displays the document within the browser.
> 
> Chris

---

<div class="post-metadata">

**Author:** ![jasongarber](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/977dab/32.png) [@jasongarber](https://www.ruby-forum.com/u/jasongarber)\
**Post date:** [February 27, 2007, 8:46pm UTC](https://www.ruby-forum.com/t/viewing-text-docs/87027/7 "2007-02-27T20:46:24Z")

</div>

Your browser should handle a link to a pdf if you have the plugin or  
reader installed. Worse case is your browser will open a dialog and  
ask if you want to save or open with whatever your default viewer is  
set to.

I believe that with the Acrobat plugin, it will open the document  
directly in the browser.

one thing i’ve done in the past is have an iframe setup for displaying  
documents using the acrobat plugin. you can then target that iframe  
from the link and the acrobat plugin should open within the targeted  
iframe and display your document.

Chris
