Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • Docx manipulation by extracting document.xml and replacing placeholders. This is not very robust and reliable as spell check or line breaks split up the XML nodes containing the placeholders.
  • wicked_pdf and other html > pdf generators: Not viable as I cannot expect my clients to supply custom css to style their documents.
  • Prawn: More tailored towards creating documents from scratch. Template functionality simply does not do what I need and does not support replacing placeholder data and repeating elements
  • Google Drive: Hand off template to Google and use the Drive API to modify document and pull PDF when done: Add extra reliability on external service. More of a last resort. Really hope to figure out a solution I can run locally.

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.

like image 911
ChrisDekker Avatar asked Aug 17 '13 01:08

ChrisDekker


2 Answers

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

like image 191
edi9999 Avatar answered Sep 19 '22 06:09

edi9999


Have a look at ruby-docx-templater

I use it for exactly the same purpose

like image 22
mck Avatar answered Sep 21 '22 06:09

mck