XSL-FO Introduction

XSLT transforms XML into HTML for screen display. But screens and printed pages are very different. A printed page has a fixed size, margins, headers, footers, and page numbers. XSL-FO — XSL Formatting Objects — is the part of the XSL family designed specifically for paginated, print-ready output.

Think of XSL-FO like a professional typesetter. You hand over the raw text (XML data), define the page layout rules (XSL-FO), and get back a precisely formatted document ready for printing or saving as a PDF.

What XSL-FO Does

  • Defines page size — A4, Letter, custom dimensions.
  • Sets margins — top, bottom, left, right.
  • Creates headers and footers with page numbers.
  • Controls fonts, font sizes, and text formatting.
  • Manages multi-column layouts.
  • Handles tables, images, and lists in print.
  • Produces PDF output through an FO processor.

How XSL-FO Fits in the Workflow

Source XML Data
      |
      v
XSLT Transformation
(converts XML data into XSL-FO format)
      |
      v
XSL-FO Document
      |
      v
FO Processor (Apache FOP, RenderX XEP, etc.)
      |
      v
Output: PDF or Printed Page

XSL-FO Document Structure

An XSL-FO file uses the namespace xmlns:fo="http://www.w3.org/1999/XSL/Format". All formatting objects use the fo: prefix.

<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">

  <!-- 1. Page layout definitions -->
  <fo:layout-master-set>
    <fo:simple-page-master master-name="A4"
      page-height="29.7cm" page-width="21cm"
      margin-top="2cm" margin-bottom="2cm"
      margin-left="2.5cm" margin-right="2.5cm">

      <fo:region-body/>
      <fo:region-before extent="2cm"/>
      <fo:region-after extent="1.5cm"/>

    </fo:simple-page-master>
  </fo:layout-master-set>

  <!-- 2. Page sequence — the actual content -->
  <fo:page-sequence master-reference="A4">

    <!-- Header -->
    <fo:static-content flow-name="xsl-region-before">
      <fo:block text-align="center" font-size="10pt">
        Company Report 2024
      </fo:block>
    </fo:static-content>

    <!-- Footer with page numbers -->
    <fo:static-content flow-name="xsl-region-after">
      <fo:block text-align="center" font-size="9pt">
        Page <fo:page-number/> of <fo:page-number-citation ref-id="end"/>
      </fo:block>
    </fo:static-content>

    <!-- Main body content -->
    <fo:flow flow-name="xsl-region-body">
      <fo:block font-size="18pt" font-weight="bold" space-after="12pt">
        Annual Sales Report
      </fo:block>
      <fo:block font-size="11pt" line-height="1.5">
        This report summarizes the company sales for financial year 2024.
      </fo:block>
      <fo:block id="end"/>
    </fo:flow>

  </fo:page-sequence>

</fo:root>

Key XSL-FO Elements

ElementPurpose
fo:rootRoot element of every XSL-FO document
fo:layout-master-setContainer for all page layout definitions
fo:simple-page-masterDefines one page layout (size, margins, regions)
fo:region-bodyThe main content area of the page
fo:region-beforeThe header area of the page
fo:region-afterThe footer area of the page
fo:page-sequenceA sequence of pages using one layout
fo:flowThe content that flows into region-body
fo:static-contentContent that repeats on every page (header/footer)
fo:blockA paragraph-like block of content
fo:inlineInline formatting within a block
fo:tableA table structure in the output
fo:page-numberInserts the current page number

Page Regions Diagram

+------------------------------------------+
|           fo:region-before               |  ← Header (static content)
+------------------------------------------+
|                                          |
|             fo:region-body               |  ← Main content (fo:flow)
|                                          |
|                                          |
+------------------------------------------+
|           fo:region-after                |  ← Footer (static content)
+------------------------------------------+

Formatting Text in XSL-FO

Every piece of text in XSL-FO sits inside an fo:block. Formatting properties attach directly to the block as attributes.

<fo:block font-family="Arial" font-size="14pt"
          font-weight="bold" color="#003366"
          space-before="10pt" space-after="6pt"
          text-align="left">
  Section Title
</fo:block>

<fo:block font-size="11pt" line-height="1.6"
          text-indent="1cm">
  This paragraph has an indented first line and 1.6 line spacing.
</fo:block>

XSL-FO Processors

ProcessorLanguageLicense
Apache FOPJavaOpen source (Apache)
RenderX XEPJavaCommercial
Antenna HouseJava / .NETCommercial
xmlroffCOpen source

XSL-FO vs CSS for Print

FeatureXSL-FOCSS for Print
Pagination supportFull — page breaks, page numbersLimited
Multi-column layoutYesBasic
Headers and footersFull control every pageLimited
PDF generationNative via FO processorsRequires additional tools
Learning curveSteepGentler

Key Points to Remember

  • XSL-FO is the XSL language for print and PDF output with full pagination control.
  • An XSLT stylesheet converts XML data into XSL-FO; then an FO processor creates the PDF.
  • fo:simple-page-master defines page size, margins, and regions.
  • fo:region-body holds the main content; fo:region-before and fo:region-after hold headers and footers.
  • fo:block is the fundamental text container — equivalent to a paragraph.
  • Apache FOP is the most popular open-source XSL-FO processor.

Leave a Comment

Your email address will not be published. Required fields are marked *