Generate MS Word files in Rails

Hey all,

Can you suggest a good gem for generating word files from Rails. The main requirement is to replace some sections inside an existing template in rails.

I used docx_replace, but found that it’s not always replacing the variable names with the template values.

Thanks,

Ganesh

Can you suggest a good gem for generating word files from Rails. The main requirement is to replace some sections inside an existing template in rails.

1) You might want to use RTF templates instead. It's a format you can more easily search for your markers and modify, and Word will open it directly.

2) If RTF is insufficient, you might consider Word + VBA, and use Word's actual scripting to fill in the templates. (Perhaps also OpenOffice or a derivative.)

3) Perhaps there's something in the OpenOffice libraries which could be wrapped up into a gem to do what you want.

I used docx_replace, but found that it's not always replacing the variable names with the template values.

Note the second sentence in the description of docx_replace: "This is purposefully meant to be simple and feature-light."

The specification of the docx format is over 7,000 pages--"simple" and "complete" are incompatible goals when dealing with docx.

​Thanks for your answer Scott.

Does RTF pose any formatting challenges as compared to docx? Except bold, italics, bulleting my requirement doesn't need any advanced formatting.

I just need users to be able to download the file, open it in MS Word and print.

Thanks, Ganesh

If you just want to download, open and print then I recommend using pdf. Several possibilities are available for this:

Colin

​I looked at PDF too but the users might want to make minor changes to the layout. The site is for generating legal agreements so minor editing and formatting changes might be required ​before taking print

Thanks, Ganesh

OK. It does of course mean that the printed document may not be the same as the one you generated, that is presumably not an issue in your case.

Colin

Hi Ganesh,

I would also recommend you to take a look at RTF, but if like me the .docx was a requirement I would suggest you to take a look at the gem https://github.com/nickfrandsen/htmltoword.

It’s still in its early stages and doesn’t support bulleted lists, but it’s pretty simple to use and have nice features like template definition and page breaks.

Best regards,

In that case, RTF should be quite easy to deal with. There are two ways to approach it, and I'd actually suggest doing both just to learn about RTF. You can search for "RTF specification" and download it from Microsoft. You can also create a document in Word, and save to RTF. The first approach will tell you more about the minimal RTF you could use to get that simple formatting. The second approach will show you everything Word specifies, which is a lot. I'd bet that the RTF that specifies what you really want to lies in between. (Of course, you might just use what Word outputs and be done--who cares if it overspecifies document attributes.)