Easy PDF Templates

July 02, 2014

We have a standard contractor agreement we use at work. It’s pretty cookie cutter but for every contractor certain things needed to be edited. These edits included name of the contractor, address, start date of the contract, etc. After giving up on forms in LibreOffice, I found an elegant solution. It turns out that LibreOffice od* files are just zipped XML. So I created an Open Document Template(odt) file and replaced all the relevant data with placeholders like __CONTRACTOR__ for contractor name. Then I use a simple make file to:

  1. unzip the template
  2. fill in the placeholders using sed
  3. zip it back up
  4. convert it to a PDF

Here’s the Makefile:

all: Independent-Contractor-Agmt.pdf

CONTRACTOR=Foobar Industries
BEGINDATE=$(shell date +"%B %d, %Y")
ENDDATE=$(shell date -d "+$(CONTRACTPERIOD)" +"%B %d, %Y")

%.odt: %.ott
  mkdir $*
  cd $*; unzip ../$<
  sed -i 's/__CONTRACTOR__/$(CONTRACTOR)/g' $*/content.xml
  sed -i 's/__CONTRACTORLOCATION__/$(CONTRACTORLOCATION)/g' $*/content.xml
  sed -i 's/__BEGINDATE__/$(BEGINDATE)/g' $*/content.xml
  sed -i 's/__ENDDATE__/$(ENDDATE)/g' $*/content.xml
  sed -i 's/__CONTRACTPERIOD__/$(CONTRACTPERIOD)/g' $*/content.xml
  cd $*; zip -r ../$@ *
  rm -r $*

%.pdf: %.odt
  unoconv -f pdf $<

  rm -f *.odt
  rm -f *.pdf

And here’s how I run it from the command line:

make all CONTRACTOR="Contracts R Us" \
CONTRACTORLOCATION="1234 Fantasy Ln, Austin, TX"

There are defaults for all the placeholders in the Makefile and you can override them on the command line. For example, BEGINDATE is the current date by default. I originally tried using m4 instead of sed but it wasn’t able to parse the XML file. This is just another example of doing things the UNIX WayTM.