Create Word Documents using Ruby on Rails

886 views
Skip to first unread message

Jivan

unread,
Oct 25, 2011, 4:35:24 PM10/25/11
to Ruby Ireland
Hi,

As I mentioned before, I am new to Ruby on Rails (I am studying it in
National College of Ireland in IFSC).

I have just submitted a project proposal but have no idea how easy/
difficult it is to do what I want. What I'd like to be able to do is
take some data entered on the screen (such as name etc) and fill in
the corresponding fields on a Word template document (along the lines
of Mail Merge but just for the logged in user).

Any ideas on how to do this in the simplest way possible?

Regards and thanks in advance,

Jivan

Justin Corry

unread,
Nov 2, 2011, 10:24:30 AM11/2/11
to Ruby Ireland
Jivan,

A MS-Word document is a zipped file. For example, try creating a Word
document and renaming is from test.docx to test.zip. Then opening the
zipped file. There is a sub-folder called word containing document.xml
which contains your document.

I have not done this but it should be possible to use an erb template
to create a document.xml with the content you require. You could then
use rubyzip to create a docx containing the generated document.xml.

Justin.

Simon Hamilton

unread,
Nov 2, 2011, 12:04:00 PM11/2/11
to ruby_i...@googlegroups.com
Hey Jivan

I recently had a similar dilemma and chose to go RTF.  I'm using existing RTF docs as templates with placeholders and replacing with Mustache (standard mustache delimiters didn't work btw).

I've also written a somewhat incomplete RTF Renderer for Redcarpet, to generate RTF from Markdown, which I then inject into the Template as body content to create a doc.

I'll be open sourcing the Redcarpet renderer soon but fire me an email directly and I'll share sooner if it is of help.

Cheers

Simon

--
Simon Hamilton
RUMBLE LABS
rumblelabs.com
+442895680011
@hamstarr

--
You received this message because you are subscribed to the Google Groups "Ruby Ireland" group.
To post to this group, send email to ruby_i...@googlegroups.com.
To unsubscribe from this group, send email to ruby_ireland...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/ruby_ireland?hl=en.




Alan Kennedy

unread,
Nov 2, 2011, 6:48:15 PM11/2/11
to ruby_i...@googlegroups.com
[Simon]

> I recently had a similar dilemma and chose to go RTF.  I'm using existing
> RTF docs as templates with placeholders and replacing with Mustache
> (standard mustache delimiters didn't work btw).

If you want to go the RTF route, you could use also JRuby to script RTFTemplate

http://rtftemplate.sourceforge.net/

I had good success a few years back with RTFTemplate+Java+Velocity.

Alan.

Jivan

unread,
Nov 3, 2011, 5:26:10 AM11/3/11
to Ruby Ireland
Thanks to all for so much input!

As I hadn't received any replies till today I thought - wrongly,
obviously! - that the topic wasn't of interest to anyone. But I guess
the message just got caught in the moderatorship backlog.

All the terminology is still new to me so I will run all of the above
by my lecturer :$...

I am using Ubuntu environment so win32ole didn't work. In the
meantime, I have been investigating Prawn and producing a pdf. And
the latest thought is to use htmldoc
http://blog.adsdevshop.com/2007/11/20/easy-pdf-generation-with-ruby-rails-and-htmldoc/.
Perhaps there are gems other than Prawn to produce pdf? Anybody have
experience producing pdfs, using Prawn, htmldoc? (I will look at what
you all have shared too!)

Go raibh míle maith agaibh go léir!

Jivan

Jamie Lawrence

unread,
Nov 3, 2011, 5:40:03 AM11/3/11
to ruby_i...@googlegroups.com
PDFkit looks like a great, possibly perfect, way to produce PDFs (I
haven't used it yet, just investigating):
https://github.com/jdpace/PDFKit

The other approach is to create ODF (Open Document Format, used my
OpenOffice) as there are several Java libraries available for this
format (like .docx it's a zipped XML file). Again, I haven't used it
but googling for "ODF java" brings up a few options. It should be easy
enough to use these libraries with JRuby

Jamie

Simon Rand

unread,
Nov 3, 2011, 5:42:55 AM11/3/11
to ruby_i...@googlegroups.com
Jivan,

In addition to the suggestions provided alright, I would suggest you have a look at threads on this topic on Stackoverflow - here's one which seems to have some more information: http://stackoverflow.com/questions/697505/creating-microsoft-word-docx-documents-in-ruby

As one of the few (I can only assume) non-fulltime devs here I can only wholly recommend Stackoverflow as a invaluable resource to learning more about Ruby/Rails and most topics for that matter, it's saved me many times.

Simon.

Jivan

unread,
Nov 3, 2011, 6:01:20 AM11/3/11
to Ruby Ireland
Thanks once again to all.

Jivan

Steven Wilkin

unread,
Nov 4, 2011, 6:32:46 AM11/4/11
to Ruby Ireland
On a previous project I successfully used wkhtmltopdf[1] to render a
PDF from an existing view and serve it up to the user.

The view was essentially a report containing a number of charts
created with the HighCharts JavaScript library and so would have been
very time consuming to recreate with images which could then be
embedded within a PDF.

The process is a bit convoluted but the result was a faithful
reproduction of an arbitrary view of a web app within a PDF.

I could cobble together a blog post and some sample code if there's
interest.

Steve

[1] http://code.google.com/p/wkhtmltopdf/


On Nov 3, 9:40 am, Jamie Lawrence <hopel...@gmail.com> wrote:
> PDFkit looks like a great, possibly perfect, way to produce PDFs (I
> haven't used it yet, just investigating):https://github.com/jdpace/PDFKit
>
> The other approach is to create ODF (Open Document Format, used my
> OpenOffice) as there are several Java libraries available for this
> format (like .docx it's a zipped XML file). Again, I haven't used it
> but googling for "ODF java" brings up a few options. It should be easy
> enough to use these libraries with JRuby
>
> Jamie
>
> On 3 Nov 2011, at 09:26, Jivan <yo.ji...@gmail.com> wrote:
>
>
>
>
>
>
>
> > Thanks to all for so much input!
>
> > As I hadn't received any replies till today I thought - wrongly,
> > obviously! - that the topic wasn't of interest to anyone.  But I guess
> > the message just got caught in the moderatorship backlog.
>
> > All the terminology is still new to me so I will run all of the above
> > by my lecturer :$...
>
> > I am using Ubuntu environment so win32ole didn't work.  In the
> > meantime, I have been investigating Prawn and producing a pdf.  And
> > the latest thought is to use htmldoc
> >http://blog.adsdevshop.com/2007/11/20/easy-pdf-generation-with-ruby-r....

Jivan

unread,
Nov 4, 2011, 8:34:17 AM11/4/11
to ruby_i...@googlegroups.com
That would be fantastic, Steven.

Thanks,

Jivan
Reply all
Reply to author
Forward
0 new messages