Reputation: 1677
I have trying to create pdf from html file using FOP. My requirement is, I want to pass variable value at run time. How can I pass variable value at run time?
Upvotes: 4
Views: 1184
Reputation: 8877
It is not clear at what point you can inject "variables" nor how you expect to do them. Here's a sample that may provide some inspiration. It only uses a simple identity-translate and omits all the FO stuff for brevuty.
General principle -- put in a hidden div
with some codes that are variables. For instance and simplicity, your input HTML now has this:
<html>
<div class="variables" style="display:none">
<div class="var_1" data-value="variable 1 value"/>
<div class="var_2" data-value="variable 2 value"/>
<div class="var_3" data-value="variable 3 value"/>
</div>
<div>
<div>Var 1 Value: <span class="variable" data-ref="var_1"/></div>
<div>Var 2 Value: <span class="variable" data-ref="var_2"/></div>
<div>Var 3 Value: <span class="variable" data-ref="var_3"/></div>
</div>
</html>
And you modify your XSL for a template that matches on a span
where you want to insert the variable:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="span[@class='variable']">
<xsl:variable name="lookup">
<xsl:value-of select="@data-ref"/>
</xsl:variable>
<span>
<xsl:value-of select="//div[@class=$lookup]/@data-value"/>
</span>
</xsl:template>
<xsl:template match="node()">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
The output of this would be:
<html>
<div class="variables" style="display:none">
<div class="var_1" data-value="variable 1 value"></div>
<div class="var_2" data-value="variable 2 value"></div>
<div class="var_3" data-value="variable 3 value"></div>
</div>
<div>
<div>Var 1 Value: <span>variable 1 value</span></div>
<div>Var 2 Value: <span>variable 2 value</span></div>
<div>Var 3 Value: <span>variable 3 value</span></div>
</div>
</html
Of course, you could expand that to include a template to strip the div
whose class is variables
for instance to not have it in the output or processed by your templates.
Upvotes: 1
Reputation: 8068
You can pass parameters to the XSLT stylesheet that generates the XSL-FO that is what FOP formats.
If you are using FOP to do the XSLT transformation, the format is -param name value
(see https://xmlgraphics.apache.org/fop/2.3/running.html). If you are using an external XSLT processor to generate the XSL-FO that you pass to FOP, then you use the format that the XSLT processor requires (which will be specified in its documentation).
The closest things to variable text in the formatting stage are fo:marker
and fo:table-marker
, but even the markers are set before the formatting starts, and the variability comes from not knowing where page breaks occur until the document is formatted.
Upvotes: 0