Forum: Ruby on Rails Generating Word Doc

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
41cc2c53544016a86472343ab3fdc0ff?d=identicon&s=25 Simon Macneall (Guest)
on 2009-04-21 03:54
(Received via mailing list)
Hi all,

Does anyone have any suggestions for generating a word doc from a linux
rails application? We were using html and just naming it .doc, which
works well until you need to embed images into the document. MHT looks
promising but the only libraries aren't free (not a deal breaker, but
free is better).

Thanks
Simon
Dd2d775dea75b381edb1bbf0600a0907?d=identicon&s=25 Marnen Laibow-Koser (marnen)
on 2009-04-21 05:38
Simon Macneall wrote:
> Hi all,
>
> Does anyone have any suggestions for generating a word doc from a linux
> rails application?

Yes: don't do it.  Word documents have no place on the Web -- they don't
reliably preserve formatting and they don't play nice with Web browsers.
Generate a PDF file instead; prawn works well for this.

[...]
> Thanks
> Simon

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen@marnen.org
41cc2c53544016a86472343ab3fdc0ff?d=identicon&s=25 Simon Macneall (Guest)
on 2009-04-21 05:59
(Received via mailing list)
That's a fabulous answer.
I wouldn't be asking if I could convince my customers that a PDF would
do. They need to be able to edit the document after it is generated.

-1 for helpfulness
5772c599ccab3081e0fffb1d54f3b6de?d=identicon&s=25 Andrew Timberlake (andrewtimberlake)
on 2009-04-21 06:08
(Received via mailing list)
On Tue, Apr 21, 2009 at 5:58 AM, Simon Macneall <macneall@gmail.com>
wrote:
>
> That's a fabulous answer.
> I wouldn't be asking if I could convince my customers that a PDF would do. They need to 
be able to edit the document after it is generated.
>
> -1 for helpfulness
>

Totally off the wall here but .docx files are XML so why not generate
one in Word and then manipulate that from your app?

Another crazy idea is to use OpenOffice, I think they have a Java
interface that you might be able to hook into somehow.

I have not done either of these, just throwing out some ideas.

Andrew Timberlake
http://ramblingsonrails.com
http://www.linkedin.com/in/andrewtimberlake

"I have never let my schooling interfere with my education" - Mark Twain
2505b282d57c29be797dc35b245adb4c?d=identicon&s=25 Philip Hallstrom (Guest)
on 2009-04-21 06:08
(Received via mailing list)
Well, to be fair, you didn't specify that the file needed to be
editable upon download so Simon's solution is pretty reasonable.

You might look into RTF.  Some google searches seem to say that you
can embed images into that format.
41cc2c53544016a86472343ab3fdc0ff?d=identicon&s=25 Simon Macneall (Guest)
on 2009-04-21 06:29
(Received via mailing list)
Hi Andrew,

Yeah, docx would work, however the customer only has word 2003, which
doesn't support that format.

the options I have found so far are:
mhtml - the only ruby lib is not free, and the format looks a bit
painful to build yourself
rtf - can't see a ruby lib, but could build a servlet with java/itext
html - can't embed the image, but can make it a link back to the server
(which is what we do with our old .net system), but is not ideal

I use OpenOffice here, but the customer has word, and have you ever
tried to convince a govt dept to change to open office? Not a quick/easy
job :)
Thanks
Simon
41cc2c53544016a86472343ab3fdc0ff?d=identicon&s=25 Simon Macneall (Guest)
on 2009-04-21 06:33
(Received via mailing list)
No I didn't, and I probably snapped a bit more than I should have, but
you continually see people answering questions with negatives like that,
and it irks me.
Dd2d775dea75b381edb1bbf0600a0907?d=identicon&s=25 Marnen Laibow-Koser (marnen)
on 2009-04-21 07:24
Simon Macneall wrote:
> No I didn't, and I probably snapped a bit more than I should have, but
> you continually see people answering questions with negatives like that,
> and it irks me.

You also continually see people asking how to solve a problem in a way
they do not realize is suboptimal.  Granted, that may not be the case
here, but that wasn't obvious from your original question.

I would actually say that giving the client an HTML file might be the
way to go here.  It's not well known, but MS Word does a beautiful job
of importing HTML files (or did the last time I tried), including
parsing <img> tags correctly.  So your client should have no trouble
editing the file with Word, or with just about anything else, and you
won't have to support a Web-unfriendly file format.

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen@marnen.org
41cc2c53544016a86472343ab3fdc0ff?d=identicon&s=25 Simon Macneall (Guest)
on 2009-04-21 07:36
(Received via mailing list)
The only problem with HTML is that I can't find a way to embed the image
(that works in Word), as opposed to having it link back to our server
(which isn't the desired outcome).
4a84e2fd070859702276b195f1ba10ff?d=identicon&s=25 Steven E (Guest)
on 2009-04-21 09:10
(Received via mailing list)
I'm working on something similar to this now. I'm writing my own
libraries I will post back when I'm done. Will put it on rubyforge

Sent from my iPhone

On Apr 20, 2009, at 9:53 PM, "Simon Macneall" <macneall@gmail.com>
41cc2c53544016a86472343ab3fdc0ff?d=identicon&s=25 Simon Macneall (Guest)
on 2009-04-21 09:50
(Received via mailing list)
Steven,

Cool, look forward to seeing the finished libraries.
In the mean time, I managed to get the embedded images working in an
html file.
Using the example here:
http://www.htmlcodetutorial.com/help/ftopic5309.html
But for anyone following this, not the white space (ie empty lines) are
important.

Cheers
Simon
47e0224e35459fc27bd3c01952425c69?d=identicon&s=25 Hasham (Guest)
on 2009-04-21 11:29
(Received via mailing list)
Have you looked at http://poi.apache.org/. Its a java library but has
wrappers for ruby.

Hasham
http://www.zenofruby.com
41cc2c53544016a86472343ab3fdc0ff?d=identicon&s=25 Simon Macneall (Guest)
on 2009-04-21 12:36
(Received via mailing list)
yeah, had a quick look, but it is an orphan project at the moment (at
least the word module is).
52226fe985e6e64828e4448f89582759?d=identicon&s=25 wjl_xyz (Guest)
on 2009-04-21 16:58
(Received via mailing list)
You can talk directly to the OpenOffice back-end, and have it generate
an MSWord .doc file.
4e9b34b410463c486f8d1ca11f5ce892?d=identicon&s=25 paron (Guest)
on 2009-04-21 16:59
(Received via mailing list)
There is an antique technique written up by Sam Neff at
http://coldfusion.sys-con.com/node/41604 for ColdFusion. It works, and
you don't need to run Word on your server. You don't have to use
ColdFusion. In fact, you don't need to run Windows. The source code is
still available at http://gemsres.com/story/41604/zip.html

Coldfusion is pretty easy to read, but if you get stuck, I'll be glad
to interpret.

I won't say it's not a PITA, but it does work; I've used it. I think I
did a version of it in VBA out of MS Access. I even embedded the
graphics.

Ron
41cc2c53544016a86472343ab3fdc0ff?d=identicon&s=25 Simon Macneall (Guest)
on 2009-04-22 07:33
(Received via mailing list)
I didn't think about that. I have html working fine now, but will keep
this in mind for when the customer (as is almost guaranteed) asks for a
more complex document.

Cheers
Simon
41cc2c53544016a86472343ab3fdc0ff?d=identicon&s=25 Simon Macneall (Guest)
on 2009-04-22 07:34
(Received via mailing list)
Thanks,

I ended up with something similar, using html and multipart mime

Simon
4b1b339ee3f96d37aba2adfa3e03276e?d=identicon&s=25 Brandon Keepers (Guest)
on 2009-04-23 21:16
(Received via mailing list)
On Tue, Apr 21, 2009 at 1:35 AM, Simon Macneall <macneall@gmail.com>
wrote:
>
> The only problem with HTML is that I can't find a way to embed the image (that works in 
Word), as opposed to having it link back to our server (which isn't the desired outcome).

I am using this approach on a project with success.  The only gotcha
is that you have to use the full URL of the image, instead of just the
path that Rails generates.

Here's a helper method I used:

  def image_url(img)
    request.protocol + request.host_with_port + image_path(img)
  end

Hope that helps

-- Brandon
--------------------------------------------------------------------------------
Idea Foundry: training takes a Holiday
http://ideafoundry.info – Refer a friend and get $100
This topic is locked and can not be replied to.