You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
Gem for generating .odt files by making strings, images, tables and sections replacements in a previously created .odt file.
INSTALL
In your Gemfile
gem'odf-report'
USAGE
Step 1 -- the template
First of all, you need a.odt file to serve as a template.Templates are normal .odt files with[PLACEHOLDERS] forsubstitutions.There arefour kinds of substitutions available:
fields
tables
images
sections
Fields
It's just an upcase sentence, surrounded by brackets. It will be replaced by the value you supply.
In the folowing example:
report=ODFReport::Report.new("Users/john/my_template.odt")do |r|r.add_field:user_name,@user.namer.add_field:address,"My new address"end
All occurences of[USER_NAME] found in the file will be replaced by the value of@user.name whereas all[ADDRESS] 'es will containsMy new address
Tables
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, chooseTable Properties... and type a name in the Name field.
If you informheader: true, the first row will be treated as aheader 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 withField placeholders, just insert a[FIELD_NAME] in each cell and let the magic takes place.
and considering you have a table like this in your template
#ID
Description
[ITEM_ID]
[DESCRIPTION]
and a collection@list_of_items, 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.
Sections
Sometimes, you have to repeat a whole chunk of a document, in a structure a lot more complex than a table. You can make a Section in your template and use it in this situations. Creating a Section in OpenOffice is as easy as select menuInsert and thenSection..., and then choose a name for it.
Sections 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 Tablesand nested Sections, as long as you provide the appropriate data structure.
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 will 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 ainvoice.items. Easy, right?
Images
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.
rubyzip: manipulating the contents of the odt file, since it's actually a zip file.nokogiri: parsing and manipulating the document xml files.mime-types: identify images mime types
TROUBLESHOOTING
Placeholder not replaced
If your placeholder is not being replaced, the problem might come from OpenOffice/LibreOffice which, when a placeholder is edited, add some markup that prevents odf-report from identifying the placeholder.
The golden rule is: NEVER edit the placeholders. If you want to change one, delete it an write again, including the []Example: if you have, say, [USER] in your template and you want to change to [USERNAME], you should not edit and type NAME.Delete the PLACEHOLDER [USER] and type [USERNAME].
Word found unreadable content
Symptom: You prepare your template file in eg. LibreOffice, and when you open the template in Word it says "Word found unreadable content"
Solution: Open your template in LibreOffice, save as.docx, quit LibreOffice. Open the.docx in LibreOffice, save as.odt.
Hypothesis: Word does not support all ODT features. Saving as.docx removes those features of the document.
About
Generates ODF files, given a template (.odt) and data, replacing tags