I've been looking for a simple way to create printable documents with
Hobo. The target is to create PDF and ODT formats from templates.
After some search, I found that serenity-odt gem was perfect to
generate documents from ODT templates. You just write the template
with OpenOffice writer with some Ruby keywords and serenity-odt parses
it nicely. And once you have the document in ODT, converting it to PDF
should be fairly simple with a headless OpenOffice (I'll post about
that once I get it working smoothly).

So this is a mini example to use ODT templates with Hobo:

- Step one: Create an example application. Two models: members
(name:string) and items (name:string usage:string). Each member has
many items. For example: "Michael" has a spoon and a knife; and "John"
has a tin-opener and a bottle.

- Step two: Install serenity-odt. (in Ubuntu sudo gem install serenity-
odt)

- Step three: Create a template with OpenOffice. To make things
simple, we'll start with the same one from the serenity-odt manual:
showcase.odt. (http://github.com/kremso/serenity/blob/master/showcase/
showcase.odt). Just a small change is needed: "title" and "crew"
variables need to be "@title" and "@crew"

- Step four: Create a folder to store the templates and the output
documents. I used "documents" in the root of the application and put
"showcase.odt" there.

- Step five: Write the code into the controllers. We are going to use
a filter to recreate a ODT document every time something changes in
the members or items table. This way it will always be updated. Put
this in members_controllers.odt:

  #ODT Template. Recreate it on changes
  after_filter :generate_odt, :only => [:create, :update, :destroy]
  require 'serenity'
  include Serenity::Generator
  def generate_odt
    @title = "Using ODT templates is fun!"
    @crew = Member.find(:all)
    render_odt 'documents/showcase.odt', 'documents/
example_output.odt'
  end

And this in items_controller.rb
  #ODT Template automatic generation on changes
  after_filter :generate_odt, :only =>
[:create_for_member, :update, :destroy]
  include Serenity::Generator
  def generate_odt
    @title = "Using ODT templates is fun!"
    @crew = Member.find(:all)
    render_odt 'documents/showcase.odt', 'documents/
example_output.odt'
  end



And that's it! A document called "example_output.odt" should be
recreated every time something changes on the members or items
database :).

I know the code is quite poor, maybe someone can help me improve it :P
- How can I make "generate_odt" DRY? Where do I put it?
- Next step is to allow this documents to be downloaded from the
application with a button. Any idea how to implement this?

-- 
You received this message because you are subscribed to the Google Groups "Hobo 
Users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/hobousers?hl=en.

Reply via email to