# Save as document that can open in msword or openoffice

**URL:** <https://www.ruby-forum.com/t/save-as-document-that-can-open-in-msword-or-openoffice/108261>\
**Category:** Rails\
**Created:** [July 1, 2007, 7:03am UTC](https://www.ruby-forum.com/t/save-as-document-that-can-open-in-msword-or-openoffice/108261 "2007-07-01T07:03:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alias](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a88e4f/32.png) [@Alias](https://www.ruby-forum.com/u/Alias)\
**Post date:** [July 1, 2007, 7:03am UTC](https://www.ruby-forum.com/t/save-as-document-that-can-open-in-msword-or-openoffice/108261/1 "2007-07-01T07:03:13Z")

</div>

If anyone can help me, i would like a button that converts html  
view(mostly reports with tables) into a document that can open in msword  
or openoffice where i can edit it later.

---

<div class="post-metadata">

**Author:** ![Alias](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a88e4f/32.png) [@Alias](https://www.ruby-forum.com/u/Alias)\
**Post date:** [July 1, 2007, 10:28am UTC](https://www.ruby-forum.com/t/save-as-document-that-can-open-in-msword-or-openoffice/108261/2 "2007-07-01T10:28:57Z")

</div>

There is an RTF writer for Ruby. YOu would have to program it  
yourself, but it looks fairly easy.

Regards

Mikel

---

<div class="post-metadata">

**Author:** ![Alias](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a88e4f/32.png) [@Alias](https://www.ruby-forum.com/u/Alias)\
**Post date:** [July 2, 2007, 5:04pm UTC](https://www.ruby-forum.com/t/save-as-document-that-can-open-in-msword-or-openoffice/108261/3 "2007-07-02T17:04:02Z")

</div>

You can give a URL to the MS-Word open prompt (never tried it with  
OO, but might work, too).

You can also change the content-type of the page (on the server) and  
the browser will hand it off to another application.

Content-type:application/vnd.ms-excel

(since you said tables, I’m thinking it’s spreadsheet-like data)

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![Alias](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a88e4f/32.png) [@Alias](https://www.ruby-forum.com/u/Alias)\
**Post date:** [July 3, 2007, 6:37pm UTC](https://www.ruby-forum.com/t/save-as-document-that-can-open-in-msword-or-openoffice/108261/4 "2007-07-03T18:37:07Z")

</div>

Alias wrote:

> If anyone can help me, i would like a button that converts html  
> view(mostly reports with tables) into a document that can open in msword  
> or openoffice where i can edit it later.

Yes, generating RTF is not difficult. I’ve not used the Ruby RTF  
library, but I have used PyRTF which is for Python, and have rolled my  
Java own code for RTF generation in a J2EE app. I looked at the RTF spec  
and worked out what to write.  
Basically, RTF is a rich text format that can be opened in MS Word or  
[OpenOffice.org](http://OpenOffice.org). It uses hierarchical tags ( braces: “{” and “}” ) - with  
markup, like HTML does, to say how the text should be displayed, e.g.  
headings, fonts, tables, etc.

Here’s what you can try:

In the view I assume you’ll have some form(s) to accept some parameters  
used to generate the report, e.g. order\_no between 100 and 200, etc.  
Apart from generating the HTML report views based on this input, write  
other methods in your controllers (or write another controller, as  
appropriate), which in turn call methods of a model class. This class  
can call the Ruby RTF library to generate the report as an RTF file, and  
pass the filename back to the controller (or the controller can specify  
the filename as an argument to the model method); some path on the  
filesystem will have to be decided, where the file(s) generated will be  
stored - you can use RAILS\_ROOT as the prefix for this. The controller  
can then either send the file to the browser using send\_file or  
send\_data (including setting the Content-type, Content-length and  
Disposition HTTP headers appropriately).

Vasudev Ram  
Dancing Bison Enterprises

> **[dancingbison.com](http://ww38.dancingbison.com/)**
>
> Learn Python Programming

> **[jugad's Journal](https://jugad.livejournal.com/)**
>
> Vasudev Ram's blog on software innovation

> **[Conversion of other file formats to PDF](https://sourceforge.net/projects/xtopdf/)**
>
> Download Conversion of other file formats to PDF for free. xtopdf: Tools to convert other formats (x) to PDF; x as in math.

---

<div class="post-metadata">

**Author:** ![Alias](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a88e4f/32.png) [@Alias](https://www.ruby-forum.com/u/Alias)\
**Post date:** [July 3, 2007, 6:45pm UTC](https://www.ruby-forum.com/t/save-as-document-that-can-open-in-msword-or-openoffice/108261/5 "2007-07-03T18:45:38Z")

</div>

Vasudev Ram wrote:

> Alias wrote:
> 
> > If anyone can help me, i would like a button that converts html  
> > view(mostly reports with tables) into a document that can open in msword  
> > or openoffice where i can edit it later.
> 
> Yes, generating RTF is not difficult. I’ve not used the Ruby RTF  
> library, but I have used PyRTF which is for Python, and have rolled my  
> Java own code for RTF generation in a J2EE app. I looked at the RTF spec  
> and worked out what to write.  
> Basically, RTF is a rich text format that can be opened in MS Word or  
> [OpenOffice.org](http://OpenOffice.org). It uses hierarchical tags ( braces: “{” and “}” ) - with  
> markup, like HTML does, to say how the text should be displayed, e.g.  
> headings, fonts, tables, etc.
> 
> Here’s what you can try:
> 
> In the view I assume you’ll have some form(s) to accept some parameters  
> used to generate the report, e.g. order\_no between 100 and 200, etc.  
> Apart from generating the HTML report views based on this input, write  
> other methods in your controllers (or write another controller, as  
> appropriate), which in turn call methods of a model class that acts as a ‘manager’ or ‘helper’ class. This class  
> can call the Ruby RTF library to generate the report as an RTF file and  
> pass the filename back to the controller (or the controller can specify  
> the filename as an argument to the model method); some path on the  
> filesystem will have to be decided, where the file(s) generated will be  
> stored - you can use RAILS\_ROOT as the prefix for this. The controller  
> can then either send the file to the browser using send\_file or  
> send\_data (including setting the Content-type, Content-length and  
> Disposition HTTP headers appropriately).
> 
> Vasudev Ram  
> Dancing Bison Enterprises  
> [http://www.dancingbison.com](http://www.dancingbison.com)  
> [http://jugad.livejournal.com](http://jugad.livejournal.com)  
> [Conversion of other file formats to PDF download | SourceForge.net](http://sourceforge.net/projects/xtopdf)

P.S.: In the previous post, insert the words " (after calling the  
appropriate other model classes to get the needed data from the DB), "  
after “generate the report as an RTF file”.

Note: Its not a requirement to have an underlying database table for  
each model class in Rails. I’ve seen that some people have this  
misunderstanding, so mentioning it. A model class is just a Ruby class,  
and can represent any concept or business logic; typical uses are for  
‘manager’ or ‘helper’ classes that do something and also talk to the  
other table-backed models to do their work, like getting and setting  
table data. That’s the pattern I’ve used in my post above.

- Vasudev
