Generating Word Doc

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

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

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,

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

1 Like

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

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.

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 :slight_smile: Thanks Simon

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.

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,

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

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

Have you looked at http://poi.apache.org/. Its a java library but has wrappers for ruby.

Hasham http://www.zenofruby.com

yeah, had a quick look, but it is an orphan project at the moment (at least the word module is).

You can talk directly to the OpenOffice back-end, and have it generate an MSWord .doc file.

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

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

Thanks,

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

Simon

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