Backend Development
XML/RSS Tutorial
From XML to PDF: A Guide to Generating Documents with XSL-FO
From XML to PDF: A Guide to Generating Documents with XSL-FO
XSL-FO is a powerful way to convert XML to PDF, suitable for printed documents that require precise layout. 1. Prepare structured XML data; 2. Use XSLT to convert XML into XSL-FO that describes the layout; 3. Render XSL-FO to PDF through a FO processor (such as Apache FOP). This method is suitable for scenarios such as requiring high-precision formats, compliance requirements, or generating invoices and reports from XML. Although the learning curve is steep and the flexibility is lower than that of CSS schemes, it still has advantages in structured document automation, ultimately achieving reliable and consistent output from XML to PDF.

Converting XML to PDF using XSL-FO is a powerful way to generate professional, print-ready documents from structured data. Unlike HTML-to-PDF methods, XSL-FO (Extensible Stylesheet Language Formatting Objects) gives you fine-grained control over layout, pagination, fonts, and styling—ideal for invoices, reports, and technical documentation.

Here's how it works in practice.
What Is XSL-FO and Why Use It?
XSL-FO is part of the XSL (Extensible Styleship Language) family, designed specifically for formatting XML data for output to PDF, print, or other paginated media. It sits between your raw XML and the final PDF:

XML → XSL-FO (via XSLT) → FO Processor → PDF
- XML holds your structured content (eg, product data, article text).
- XSLT transforms XML into XSL-FO, which describes layout (margins, blocks, tables, etc.).
- An FO processor (like Apache FOP, RenderX XEP, or Antenna House) renders the FO into PDF.
This pipeline is especially useful when:
- You need consistency, repeatable document formatting.
- Output must meet strict print or regulatory standards.
- Content comes from databases or CMS systems in XML format.
Step-by-Step: XML to PDF with Apache FOP
Apache FOP (Formatting Objects Processor) is a popular open-source tool for generating PDFs from XSL-FO. Here's a practical workflow.
1. Prepare Your XML
Start with clean, well-structured XML. For example:
<report>
<title>Monthly Sales Report</title>
<date>2025-04-05</date>
<section>
<heading>Revenue Summary</heading>
<paragraph>Total sales reached $120,000 this month.</paragraph>
</section>
</report>2. Write an XSLT to Generate XSL-FO
Transform the XML into formatting objects. Key elements include <fo:root> , <fo:layout-master-set> , and <fo:flow> .
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<fo:root>
<fo:layout-master-set>
<fo:simple-page-master master-name="A4" page-height="29.7cm" page-width="21cm" margin="2cm">
<fo:region-body/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="A4">
<fo:flow flow-name="xsl-region-body">
<fo:block font-size="18pt" font-weight="bold" space-after="10pt">
<xsl:value-of select="report/title"/>
</fo:block>
<fo:block font-size="10pt" space-after="5pt">
<xsl:value-of select="report/date"/>
</fo:block>
<xsl:for-each select="report/section">
<fo:block font-size="14pt" font-weight="bold" space-before="10pt" space-after="5pt">
<xsl:value-of select="heading"/>
</fo:block>
<fo:block font-size="11pt" space-after="10pt">
<xsl:value-of select="paragraph"/>
</fo:block>
</xsl:for-each>
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
</xsl:stylesheet>3. Process with Apache FOP
Install Apache FOP (available at //m.sbmmt.com/link/c4d2cec38fa1da8b5d37758873c3678d ) and run the transformation:
fop -xml input.xml -xsl stylesheet.xsl -pdf output.pdf
This command:
- Reads
input.xml - Applies
stylesheet.xslto generate FO in memory - Uses FOP to render the result as
output.pdf
You can also chain XSLT and FO processing in code (Java, Python, etc.) for automation.
Key XSL-FO Features for Professional Layouts
XSL-FO shines when you need precision control. Common use cases include:
- Page numbering : Use
fo:page-numberand running headers/footers. - Tables : Define column widths, borders, and alignment with
fo:table. - Margins and breaks : Control page breaks with
keep-together,break-before, etc. - Fonts and embedding : Specify font families and embed TrueType fonts in PDFs.
- Lists and indentation : Use
fo:list-blockfor ordered/unordered lists.
For example, adding a header:
<fo:static-content flow-name="xsl-region-before">
<fo:block text-align="center" font-size="10pt">
Confidential Report
</fo:block>
</fo:static-content> Then reference it in your page master with margin-top and a region.
Limitations and Alternatives
While powerful, XSL-FO has some drawbacks:
- Steeper learning curve than CSS-based tools.
- Less flexible for web-style layouts.
- Limited support for modern PDF features (eg, forms, interaction).
- Apache FOP doesn't fully implement the entire XSL-FO spec (eg, some advanced table features).
Alternatives to consider:
- CSS Paged Media PrinceXML or AntennaHouse : More modern, web-friendly syntax.
- JasperReports or Docx4j : Better for mixed-format reporting.
- Pandoc : Simpler if you're converting from Markdown or HTML.
But if you're already working with XML and need predictable, high-quality print output, XSL-FO remains a solid choice.
Basically, XSL-FO isn't flashy, but it's reliable for structured document generation. With a clear pipeline—XML to XSLT to FO to PDF—you can automate complex reports, invoices, or manuals with pixel-perfect consistency. Not everything needs to be HTML-based; sometimes, old-school XML tooling does the job best.
The above is the detailed content of From XML to PDF: A Guide to Generating Documents with XSL-FO. For more information, please follow other related articles on the PHP Chinese website!
Hot AI Tools
Undress AI Tool
Undress images for free
AI Clothes Remover
Online AI tool for removing clothes from photos.
Undresser.AI Undress
AI-powered app for creating realistic nude photos
ArtGPT
AI image generator for creative art from text prompts.
Stock Market GPT
AI powered investment research for smarter decisions
Hot Article
Popular tool
Notepad++7.3.1
Easy-to-use and free code editor
SublimeText3 Chinese version
Chinese version, very easy to use
Zend Studio 13.0.1
Powerful PHP integrated development environment
Dreamweaver CS6
Visual web development tools
SublimeText3 Mac version
God-level code editing software (SublimeText3)
Hot Topics
20519
7
13632
4
Doubao Ai web portal Doubao Ai official online use portal
Feb 04, 2026 pm 04:31 PM
The official online portal of Doubao Ai is https://www.doubao.com/chat/, which supports multi-modal interaction, cross-terminal collaboration, intelligent enhanced tool chain and lightweight resource occupation design.
How to add page numbers to PDF PDF batch insertion page number setting method [Tutorial]
Jan 30, 2026 pm 11:24 PM
You can use AdobeAcrobatProDC, PDFtk command line tool, PythonPyPDF2 reportlab or PDFescape online tool to add page numbers to PDF in batches; each method supports graphical interface operation, command line batch processing, highly customized programming and rapid web page editing.
Tencent Yuanbao official website URL Tencent Yuanbao web page login official channel
Feb 04, 2026 pm 06:39 PM
The official website of Tencent Yuanbao is https://yuanbao.tencent.com/. This address is the only official main entrance of Tencent. It supports three login methods: WeChat code scanning, QQ account and mobile phone number. It has the characteristics of millisecond response, responsive layout, multi-modal content generation and security compliance.
How to convert Word to PDF on Mac without using Office to convert document formats [Trick]
Feb 06, 2026 am 11:04 AM
Mac users can convert Word to PDF without Office: 1. Use the system print function to save as PDF; 2. Drag into the preview application to export; 3. Use Pages to export high-quality PDF; 4. Convert online through Smallpdf; 5. Install LibreOffice for local conversion.
Tencent Yuanbao official login entrance address Tencent Yuanbao AI platform entrance
Feb 04, 2026 pm 05:00 PM
The official login portal of Tencent Yuanbao is https://yuanbao.tencent.com, which supports simultaneous access from the web, APP and WeChat applet. It has six core functions: dual-model collaboration, in-depth document processing, natural voice interaction and personalized creation assistance.
How to enable the Qianwen plug-in extension function_How to enable the Qianwen plug-in [Guide]
Feb 04, 2026 pm 05:57 PM
The advanced functions of the Tongyi Qianwen browser plug-in are not responding, and four steps need to be completed in sequence: 1. Enable the plug-in on the extension management page and authorize "all website runs" and other permissions, and log in to the Alibaba Cloud account; 2. Enable the context menu permissions and test the word delineation floating menu; 3. Authorize the microphone in the plug-in settings and start real-time recording; 4. Synchronize the status of the web version and ensure that the "Qwen" button is displayed next to the input box; Noi browser users need to manually enable the sub-function through the AI toolbox.
How to extract text from pictures with MAC_How to use the OCR text recognition function of MAC [Hardcore]
Feb 06, 2026 am 09:37 AM
The Mac system comes with an OCR function that can extract image text: 1. Use the Preview App to identify and copy; 2. Search for keywords through Spotlight to locate the image; 3. Use shortcut commands to batch process multiple images; 4. Directly select the scanned text in the PDF.
How to electronically sign a PDF with MAC_MAC comes with its own signature collection and insertion method [Contract]
Feb 06, 2026 am 10:03 AM
The Mac Preview app supports three ways to create electronic signatures: touchpad handwriting, camera capture of paper signatures, iOS device synchronization, and can be inserted, zoomed, positioned, and batch copied to designated locations in PDF contracts.





