I seem to remember a few years back looking into this, i found that it could be done with XML and xslt. I remember there was an export format to XML from InDesign also, and there was a spec on xml page layouts to generate pdfs... you might be able to look into one of these directions...