What is XSL-FO

Top Previous Topic Next Topic  Print this topic

XSL-FO is an XML language designed for describing all visual aspects of paginated documents. The well known HTML is another language for specifying formatting semantics, but is more usable for documents that are presented on screen, and less for materials created for printing, because it does not support pagination elements like headers and footers, page size specifications, footnotes, etc.

 

XSL-FO is part of XSL language family:

XSLT - (XSL Transformations) a language for transforming XML.

XSL-FO - (XSL Formatting Objects) a language that can be used in XSLT for the purpose of "presenting" the XML.

 

XSL Formatting Objects is a W3C standard used by Ecrion XF Rendering Server 2010 to produce print-ready documents in PDF,  AFP,  Postscript, TIFF and other formats.

XF Designer can edit XSL-FO documents in the same way like a HTML editor can edit HTML pages.

 

The following image depicts the steps required to produce a PDF document (or any other supported output format) using XSL:

As you can see the XML data is transformed together with the XSL stylesheet to produce an XSL-FO document and the document is then converted to PDF.

 

How hard it is to learn?

 

The XSL-FO language uses CSS (Cascading Style Sheets) to describe formatting attributes like fonts, colors and borders, so from this point of view, it should be easy to learn by HTML developers. This manual will help you understand the language and accomplish more complicated tasks.

 

Here is the traditional Hello World, XSL-FO style:

 

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

       <fo:layout-master-set> (2)                                                                                                        

               <fo:simple-page-master master-name="LetterPage" page-width="6.5in"

                                                               page-height="1in"> (3)                                                                                                        

                       <fo:region-body region-name="PageBody" margin="0.2in"/>        

               </fo:simple-page-master>                        

       </fo:layout-master-set>                                                                                                        

       <fo:page-sequence master-reference="LetterPage">                                                                                                                        

               <fo:flow flow-name="PageBody" font-family="Times New Roman"

                                                               font-size="24pt">                                                                                                        

                       <fo:block> Hello World </fo:block> (4)                                                                                                                

               </fo:flow>                                                                                                        

       </fo:page-sequence>                                                                                                        

</fo:root>                                                                                                                                

 

There are several things to notice:

 

(1) Any XML document must have only one root, and XSL- FO makes no exception. The root element for an XSL-FO document is fo:root. The word "fo" before column character ":" is called a namespace prefix. An XML namespace is collection of names identified by an unique URL. It's main role is to avoid collisions when a single XML document contains elements and attributes defined by multiple software modules. The "fo" namespace prefix is linked with an unique URL, in this case "http://www.w3.org/1999/XSL/Format" using xmlns attribute. This syntax is based on W3C XML Namespace Spec.

 

(2)(3) The pages structure is defined using fo:layout-master-set; more about this in the chapter Pagination. For now, is enough to say that it declares one type of page, 11.5 x 8 inches (US Letter).

 

(4) The "Hello World" paragraph is added into the page.

 

The result of rendering should be identical with the following figure:

hello_world

 Figure 1.

 

To convert this document into PDF, you can use XF Designer. Open the document and generate the PDF from the tools menu. Or you can use render.exe, a console program located in "C:\Program Files\Ecrion Software\XF Rendering Server 2010\bin".

 

The command line is:

 

               

       render.exe -fo HelloWorld.fo -pdf C:\Temp\HelloWorld.pdf                                                                                                                                                                                                        

 

 

The command line flag -pdf is optional; if not present render will generate a PDF file with a name identical with the input file's name and .pdf extension.