Search code examples
pythonxmlxsltsubprocessapache-fop

Is it possible to call xsl Apache FOP without providing an input file but instead passing a string


I am trying to generate a PDF using FOP. To do this I am taking in a template file, initialling its values with Jinja2 and then passing it through to fop with a system call.

Is it possible to do a subprocess call to FOP without passing through an input file but instead a string containing the XML directly? And if so how would I go about doing so?

I was hoping for something like this

fop -fo "XML here" -pdf output.pdf

Solution

  • Yes actually it was possible.

    Using python I was able to import the xml from the file into lxml.etree:

    tree = etree.parse('FOP_PARENT.fo.xml')
    

    And then by using the etree to parse the include tags:

    tree.xinclude()
    

    Then it was a simple case of converting the xml back into unicode:

    xml = etree.tounicode(tree)
    

    This is how I got the templates to work. Hopefully this helps someone who has the same issue!