txt2booklet: Generate directories with Perl and XHTML

Back in September I saw the light and began using Perl and XHTML/CSS to produce documents destined for print. This has served me well when creating calendars, for example — so when it came time for me to generate a directory for our youth group, I naturally fired up the text editor and started cranking out Perl.

My goal was simple: given a plain text file that anyone could update, output a directory in booklet form with double-sided pages like you’d find in any other real publication. The directory ends up as XHTML files, with only a little CSS work needed to make a print-ready document.

Input

The Perl script accepts the path to a text file as its only argument… the text file is formatted like so:

name:Matt Swann
photo:photos/mswann.jpg
birthday:Oct 18
phone:(123) 456-7890
cell:(123) 456-9087
email:swannman@mac.com
address:742 Evergreen Terrace<br/>Springfield, USA 47012

name:Amanda Swann
photo:photos/aswann.jpg
birthday:April 11
phone:(123) 456-7890
cell:(123) 456-4679
email:amalou83@mac.com
address:742 Evergreen Terrace<br/>Springfield, USA 47012

facebook:, myspace:, and website: attributes are supported as well — and it’s easy to add more. Each record is separated by a blank line. The photo: attribute contains the local path to an image from the text file directory. A record that contains only a title: attribute can be used to separate the directory into sections… although the script doesn’t output the title, it acts as a page break.

Processing

The Perl script looks for two groups of records: one that begins with title:Leaders, and the other with title:Students. Each line in a person record is read in, split on the colon character, and added to a hash table. At the end of each record, the hash table is added to the appropriate array.

Each array is then split into half-sheet pages containing five people per page, arranged in the same order as the contents of the text file. These sheets are then arranged appropriately on pages (two per side) to appear in the correct order when printed on paper and assembled.

Output

txt2booklet outputs valid XHTML documents in the same directory as the text file. An eight-page booklet is represented by two sheets, each with two sides:

directorybook_1 back.html
directorybook_1 front.html
directorybook_2 back.html
directorybook_2 front.html

Each page references a stylesheet file named dirbookstyle.css in the same directory. Example output is included in the txt2booklet distribution along with CSS that you can use to produce print-ready output.

Usage and future directions

Download txt2booklet here. Run it from Terminal.app by navigating to the txt2booklet directory and typing txt2booklet.pl path/to/people.txt.

Some work is still needed to make the script more general… it shouldn’t have hardcoded Students and Leaders arrays, for example. I believe there may also be an edge case when there are not enough students to fill both the front and back side of the highest-numbered output sheet.

This script has only been tested on Mac OS X — some modifications may be necessary in order for it to run on Windows.

One Trackback

  1. [...] miscellany Skip to content AboutProjectspdf2gerb: Convert PDF to Gerber and NC Drill formatstxt2booklet: Generate directories with Perl and XHTMLQuotes « Anyone want “Oblivion With Bells”? Not dead, just resting. [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*