Got fonts working in FOP
Posted by mholmes on 06 Nov 2008 in Activity log
Here's what I had to do:
- Create the config file with a slightly different name, thus:
[cocoon]/teiJournal/fop-config-src.xml. - In that file, replace paths to font metrics and font files with simple folder+file names, thus:
<font metrics-file="fop-fonts/DejaVuSans.xml" kerning="yes" embed-file="fop-fonts/DejaVuSans.ttf"> - In the fo2pdf serializer section of the root sitemap, set the user-config setting to use a cocoon path, which will invoke a pipeline we create:
<map:serializer logger="sitemap.serializer.fo2pdf" mime-type="application/pdf" name="fo2pdf" src="org.apache.cocoon.serialization.FOPSerializer"> <user-config>cocoon://teiJournal/fop-config.xml</user-config> </map:serializer> - Create an XSLT file to do the transformation. This invokes Cocoon's RealPath module to find the real path of Cocoon on the filesystem, so we can use it to reconstruct the path to the fonts:
<?xml version="1.0"?> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="#all"> <xsl:param name="fontPath" /> <!-- XSLT Template to copy anything, priority="-1" --> <xsl:template match="@*|node()|text()|comment()|processing-instruction()" priority="-1"> <xsl:copy> <xsl:apply-templates select="@*|node()|text()|comment()|processing-instruction()"/> </xsl:copy> </xsl:template> <!-- Massage the path attributes. --> <xsl:template match="@metrics-file"> <xsl:attribute name="metrics-file"><xsl:value-of select="$fontPath"/><xsl:value-of select="."/></xsl:attribute> </xsl:template> <xsl:template match="@embed-file"> <xsl:attribute name="embed-file"><xsl:value-of select="$fontPath"/><xsl:value-of select="."/></xsl:attribute> </xsl:template> </xsl:stylesheet> - In the main pipeline of the root sitemap, add a pipeline to handle generating, transforming and serializing the config file, like this:
<map:match pattern="teiJournal/fop-config.xml"> <map:generate src="teiJournal/fop-config-src.xml" /> <map:transform type="saxon" src="teiJournal/xsl/fop-config.xsl"> <map:parameter name="fontPath" value="{realpath:/}WEB-INF/" /> </map:transform> <map:serialize type="xml"/> </map:match> - Check that this worked OK, by accessing the font-config.xml file in your browser, at the location pointed to by the pipeline.
- Get working fonts when you run a PDF generation.