XSL-FO with XSLT — XML to PDF
XSLT transformations can separate content structure from formatting instructions in XSL-FO documents. Rather than embedding formatting directly in XML, XSLT templates transform clean semantic markup into styled XSL-FO output that PdfBroker.io can render to PDF.
Direct Formatting Approach
In the previous chapters, we embedded formatting attributes directly in XSL-FO blocks:
<fo:block font-size="14pt" font-family="verdana" color="red"
space-before="5mm" space-after="5mm">
PdfBroker.io
</fo:block>
This works but mixes content with formatting, making documents verbose and hard to maintain.
With a Little Help from XSLT
Using XSLT, you can define templates that apply formatting to semantic XML elements:
<xsl:template match="header">
<fo:block font-size="14pt" font-family="verdana" color="red"
space-before="5mm" space-after="5mm">
<xsl:apply-templates />
</fo:block>
</xsl:template>
Your source XML stays clean and semantic:
<header>PdfBroker.io</header>
<paragraph>Welcome to our documentation.</paragraph>
Benefits of Using XSLT
- Reusable stylesheets — apply the same formatting to different data sets
- Maintainable documents — change formatting without touching data
- Separation of concerns — data stays clean, formatting stays in templates
Try It
Use the XSL-FO Transform API to submit your XSLT template and XML data separately. The API handles the transformation and PDF generation in one step. See the invoice demo for a real-world example.