in reply to automatic thank you!

The following is my patent-pending method for generating documents automatically using Perl:

  1. 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.)
  2. 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.
  3. Unzip it into a working directory.
  4. 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.)
  5. 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.
  6. open XML, ">$workdir/content.xml"; print XML $interpolatedcontent; close XML;
  7. 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.
  8. 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$/

Replies are listed 'Best First'.
Re: Re: automatic thank you!
by DapperDan (Pilgrim) on Aug 30, 2003 at 15:20 UTC
    Wow, having generated items like this using mail merge (with embedded tabs in CSV data which makes Word move to the next table cell; ugh!) in Microsoft Word, you don't know what a relevation it is to see how easy that is. Once you're dealing with a text/XML format you can just interpolate your data.

    I've been turned off by OpenOffice's clunky UI but I'll have to look past that now and develop a workflow for Writer.

    Thanks a bunch for that recipe. I'll be submitting this node to google.

Re: Re: automatic thank you!
by PodMaster (Abbot) on Aug 31, 2003 at 06:49 UTC
    Use an editor macro to backslash all the double quotes in it so that it's one big string.
    That's yucky
    my $bob = 1; print <<"__HELLO_HERE_DOCUMENTS__"; print 1; die 2; exit 3; HELLO $bob!!!!!!!!1 __HELLO_HERE_DOCUMENTS__
    And you can always use an odd delimiter like perl -le" $bob = 1; print qqº i am quoted $bob º, qq¿ me too $bob ¿"

    For more info on here-documents, `perldoc perldata' and `perldoc -q "here document"'

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Re: automatic thank you!
by csuhockey3 (Curate) on Aug 31, 2003 at 06:24 UTC

    Wow. I can use this method for all sorts of home/school/work related prolems. Awesome recipe.

    csuhockey3