ChrisDekker
ChrisDekker

Reputation: 1773

Templated document generation using Ruby on Rails

I am currently developing an administrative suite using Ruby on Rails 3.2.

One of the tasks it needs to perform is the generation of contracts and invoices.

Because the application needs to accomodate multiple clients, I want each client to be able to upload some kind of template which dictates their personal style and layout. (i.e.: logo, company name, addr, etc).

I am thinking of letting clients create their own .docx contract templates using variables and placeholders which my application should replace whener a specific contract / invoice is generated. Note that the people creating the templates will not be tech savvy.

How should I be able to accomplish this? Note that there will placeholders which can be replaced directly as well as repeating elements which should be contained in tables. The number of these elements can vary from contract to contract.

After having considered many solutions, gems and examples available I am unable to find one which works for my situation. This is what I have tried so far:

I get the feeling I am missing something obvious. I cannot imagine that such a mundane task as document generation is so complicated when using Ruby on Rails.

Upvotes: 5

Views: 2398

Answers (4)

Théo Capdet
Théo Capdet

Reputation: 1062

Maybe this gem is interesting for you.

https://github.com/trade-informatics/caracal/

It like prawn but with docx.

Upvotes: 1

ChrisDekker
ChrisDekker

Reputation: 1773

After considering the alternatives I decided to go in a different route and I stumbled upon this: github.com/sandrods/odf-report

It is a ODF (open office) gem for Rails that lets you do the same thing as ruby-docx-templater, but it feels much more robust and doesn't break as easily as the docx variants...Even spellcheck can break up parts of a [First name] block and make it stop working

Upvotes: 2

mck
mck

Reputation: 1404

Have a look at ruby-docx-templater

I use it for exactly the same purpose

Upvotes: 2

edi9999
edi9999

Reputation: 20564

I have created a library exactly for this purpose:

It's called Docxgen and can be found on github: https://github.com/edi9999/docxgenjs

Here's a usage example of how to use it:

Used content: Hello {first_name} {last_name}

var doc= new DocxGen(docData); //Create a new DocxGen document

doc.setTemplateVars(
    {"first_name":"Hipp",
    "last_name":"Edgar",
    }
) //set the templateVariables
doc.applyTemplateVars() //apply them (replace all occurences of {first_name} by Hipp, ...)
doc.output() //Output the document using Data-URI

Here's the example live on my site: http://javascript-ninja.fr/docxgenjs/examples/demo.html

Okay this is javascript, but:

it runs on node when you install it globally, as a command line:

docxgen <inputFileDocx> <inputFileJson>

More about the installation on the github repo: https://github.com/edi9999/docxgenjs#node-installation-and-usage

Hope this helps

Upvotes: 2

Related Questions