Search code examples
ruby-on-railspdfdocxdocumentation-generation

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.


Solution

  • 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