The following is my patent-pending method for generating
documents automatically using Perl:
- Start OpenOffice.org, go into the options (under the
Tools menu) and under Load/Save, click on General.
Turn off the option "Size optimization for XML format
(no pretty printing)". (This will make a later
step much easier.)
- Use OpenOffice.org Writer to create a sample document
with all of the formatting and setup exactly the way
you want (tables, columns, image placement, fonts,
and so forth), but sample/dummy content (e.g., a
thank-you to Jane Doe for the wonderful Widget Set).
Save this.
- Unzip it into a working directory.
- Copy and paste the contents of content.xml into a
big string in your Perl script. Use an editor macro
to backslash all the double quotes in it so that
it's one big string. (Update: Or you can use a
heredoc as PodMaster suggests.)
- Interpolate your content. You can do this in a
loop or whatever. (I often do tables out of a
database, so I do the old
".(join"\n",map{tablerow($_)}@rows)."
trick, but for what you're doing simple interpolation
will probably be fine.) Note that certain characters
(notably ampersand) have to be written as entities,
so any that you're going to have to deal with be
sure to include in your sample document so you can
see the encoded form.
- open XML, ">$workdir/content.xml";
print XML $interpolatedcontent;
close XML;
- Zip up the contents of your working directory into
an sxw. You can either use Archive::Zip or
just backtick out to info-zip or whatever zip app
you use.
- Doubleclick the sxw and print it. I haven't found
a way to automate this step yet. However, while I
haven't done this, it would easily be possible to
generate multiple items (in a loop) in one document
separated by hard page breaks and print them all at
one go.
If you want to take this approach, be sure to put
a hard page break in your sample doc so you have
one to copy-and-paste into your string.
$;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}}
split//,".rekcah lreP rehtona tsuJ";$\=$ ;->();print$/