Generates ODF files, given a template (.odt) and data, replacing tags
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
Gemfile use bundler Jul 24, 2014
Manifest Added suport for sections May 19, 2010



Gem for generating .odt files by making strings, images, tables and sections replacements in a previously created .odt file.


  • as per popular request, now uses rubyzip ~> 1.1.0
  • uses Zip::OutputStream.write_buffer to generate the file. This should avoid corruption issues.
  • the .generate method now returns the actual report binary, so you can send_data it directly
  • you can also use .generate('file.odt'), which saves the report to the specified file


In your Gemfile

gem ‘odf-report’ 


Step 1 — the template

First of all, you need to create a .odt file to serve as a template.
Templates are normal .odt files with placeholders for substitutions.
There are now four kinds of substitutions available: fields, tables, images and sections.

Fields placeholders

It’s just an upcase sentence, surrounded by brackets. It will be replaced for wathever value you supply.

In the folowing example:

report ="Users/john/my_template.odt") do |r|

  r.add_field :user_name,
  r.add_field :address, "My new address"


All occurences of [USER_NAME] found in the file will be replaced by the value of whereas all [ADDRESS] ’es will contains My new address

It’s as simple as that.

Table placeholders

To use table placeholders, you should create a Table in your document and give it a name. In OpenOffice, it’s just a matter of right-clicking the table you just created, choose Table Properties… and type a name in the Name field.

If you inform :header=>true, the first row will be treated as a header and left untouched. The remaining rows will be used as the template for the table. If you have more than one template row, they will be cycled. This is usefull for making zebra tables.

As with Field placeholders, just insert a [FIELD_NAME] in each cell and let the magic takes place.

Taking the folowing example:

report ="Users/john/my_template.odt") do |r|

  r.add_field "USER_NAME", @user.nome
  r.add_field "ADDRESS", @user.address

  r.add_table("TABLE_1", @list_of_itens, :header=>true) do |t|
    t.add_column(:item_id, :id)
    t.add_column(:description) { |item| "==> #{item.description}" }


and considering you have a table like this in your template


* this is my lame attempt to draw a table.
  you are not supposed to type this.
  you have to use an actual table.
  i don't know... just thought I should mention it ;-)

and a collection @list_of_itens, it will create one row for each item in the collection, and the replacement will take place accordingly.

Any format applied to the fields in the template will be preserved.


You must put a mock image in your odt template and give it a name. That name will be used to replace the mock image for the actual image.
You can also assign any properties you want to the mock image and they will be kept once the image is replaced.

An image replace would look like this:

report ="Users/john/my_template.odt") do |r|

  r.add_image :graphics1, "/path/to/the/image.jpg"



Sometimes, you have to repeat a whole chunk of a document, in a structure a lot more complex than a table. Now you can make a Section in your template and use it in this situations. Creating a Section in OpenOffice is as easy as select menu Insert and then Section…, and then choose a name for it.

Section ’s are lot like Tables, in the sense that you can pass a collection and have that section repeated for each member of the collection. But, Sections can have anything inside it, even Tables and nested Sections, as long as you pass the appropriate data structure.

Let’s see an example:

  @invoices = Invoice.find(:all)

  report ="reports/invoice.odt") do |r|

    r.add_field(:title, "INVOICES REPORT")

    r.add_section("SC_INVOICE", @invoices) do |s|

      s.add_field(:number) { |invoice| invoice.number.to_s.rjust(5, '0') }
      s.add_field(:name,    :customer_name)
      s.add_field(:address, :customer_address)

      s.add_table("TB_ITEMS", :items, :header => true) do |t|
        t.add_column(:product) {|item| }
        t.add_column(:value, :product_value)

      s.add_field(:total) do |invoice|
        if invoice.status == 'CLOSED'


      s.add_section("SUB_NOTES", :notes) do |s1|

        s1.add_field(:note_title) { |n| n.title }

        s1.add_table ...




Note that when you add a Table to a Section, you don’t pass the collection itself, but the attribute of the item of that section that’s gonna return the collection for that particular Table. Sounds complicated, huh? But once you get it, it’s quite straightforward.

In the above example, s.add_table("TB_ITEMS", :items, :header => true) do |t|, the :items thing refers to a invoice.items. Easy, right?

Step 2 — generating the document

It’s fairly simple to generate the document. You can use this inside a Rails application or in a standalone script.

Generating a document in a Rails application

In a controller, you can have a code like this:

def print

  @ticket = Ticket.find(params[:id])

  # For Rails 3 or latest replace #{RAILS_ROOT} to #{Rails.root}
  report ="#{RAILS_ROOT}/app/reports/ticket.odt") do |r|

    r.add_field(:created_by, @ticket.created_by)
    r.add_field(:created_at, @ticket.created_at.strftime("%d/%m/%Y - %H:%M"))
    r.add_field(:status,     @ticket.status_text)
    r.add_field(:date,"%d/%m/%Y - %H:%M"))
    r.add_field(:solution,   (@ticket.solution || ''))

    r.add_table("OPERATORS", @ticket.operators) do |t|
      t.add_column(:operator_name) { |op| "#{} (#{op.department.short_name})" }

    r.add_table("FIELDS", @ticket.fields) do |t|
      t.add_column(:field_name, :name)
      t.add_column(:field_value) { |field| field.text_value || "Empty" }


  send_data report.generate, type: 'application/vnd.oasis.opendocument.text',
                              disposition: 'attachment',
                              filename: 'report.odt'


That’s all I have to say about that.

Generating a document in a standalone script

It’s just the same as in a Rails app, but you can inform the path where the file will be saved.

report ="ticket.odt") do |r|

... populates the report ...




  • rubyzip*: for manipulating the contents of the odt file, since it’s actually a zip file.
  • nokogiri*: for parsing and manipulating the document xml files.