How to display HTML in your BI Publisher document and change the font using HTML2FO and subtemplates
When you implement the HTML2FO function in your document you'll soon notice that the font being used for your HTML will differ from the font used in your document. In order to keep the same layout across our BI Publisher document we also need a way to change the font for the displayed HTML to reflect the same standard font-type and font-size. Let's start with an example.
We have the below XML data available to us:
<?xml version="1.0" encoding="UTF-8"?>
<TEST_CYRIEL>
<TEXT>
<NORMAL_TEXT>This is normal text</NORMAL_TEXT>
<HTML_TEXT><![CDATA[<DIV>This is a piece of text in <strong>HTML</strong>, the word <strong>HTML</strong> should be bold.<p>This is a new paragraph.</p>It contains also a number of standard HTML tags like italic as the next part <i>italic text</i><p>We can also have a bullet list like this: <ul><li>Item 1</li><li>Item 2</li></ul></p><p>Or a numbered one like this: <ol><li>Item 1</li><li>Item 2</li></ol></p></DIV>]]>
</HTML_TEXT>
</TEXT>
</TEST_CYRIEL>
As can be seen above the HTML text within the HTML_TEXT element is enclosed by CDATA tags to preserve the HTML tags within the XML structure. The HTML contains tags to display bold text (<strong>...</strong>) and italic text (<i>....</i>), paragraphs (<p>...</p>), an unnumbered or bullet list (<ul><li>...</li></ul>) and finally a numbered list (<ol><li>...</li></ol>). We want to have this same structure in our BI Publisher document.
Let's first display this HTML_TEXT element in our BI Publisher template by inserting a Microsoft Word field with the below code:
<?HTML_TEXT?>
The result will be the below.
Now, let's convert these HTML tags to the BI Publisher FO language by invoking the function HTML2FO on our XML data element. We'll add a Microsoft Word field with the below code, so use html2fo with a colon and after that your xml element name:
<?html2fo: HTML_TEXT?>
The result will be the below.
As can be seen BI Publisher now displays our HTML correctly in the BI Publisher generated document. The strong tag is converted to bold, the i tag to italic, paragraphs are added, and the bullet and numbered list is displayed.
There is only one annoying thing. The html2fo function will use a default font for the converted result. Our main BI Publisher RTF template has Calibri 12 as the standard font, however the HTML font used in this case is Arial 12. Also note that the html2fo function will not use the standard RTF default font setting as defined in your BI Publisher properties, so we need a way to change the font used in our converted html2fo html text.
What we will do is to use an XSL subtemplate definition within our main BI Publisher RTF template. In this subtemplate definition we need to define a fo:inline block with our call to HTML2FO. On the actual fo:inline definition we'll set the used font, in my example Calibri and our font-size 12.
Insert a Microsoft Word field with the below code:
<xsl:template name="HTMLTEXT">
<fo:inline padding-bottom="0.0pt" text-align="start" padding-top="0.0pt" end-indent="0.0pt" linefeed-treatment="preserve" start-indent="0.0pt" height="0pt" font-family="Calibri" font-size="12px">
<xsl:value-of select="xdoxslt:HTML2FO($_XDOXSLTCTX, (.//HTML_TEXT)[1], '')" disable-output-escaping="yes"/>
</fo:inline>
</xsl:template>
The above code defines an XSL template with the name HTMLTEXT. In there is a fo:inline with a number of layout properties including our font-family and font-size attributes. The xsl:value part will select the html2fo function using the xdoxslt library with the HTML_TEXT data element as an input.
When you define an internal subtemplate in your main BI Publisher template there is of course no need to import that subtemplate. So, we can just call our subtemplate where we want the template to appear.
In your main document insert a Microsoft Word field with the below code:
<?call-template@inlines:HTMLTEXT?><?end call-template?>
Use @inlines to add the XSL template result on the same line, and do not forget to add <?end call-template> to close the call.
The result will be the below:
Now, we have correctly displayed HTML in our main document with the correct font and font-size.
Enjoy!