How to Convert XML to PDF Using Python?

Estimated read time 2 min read

To convert XML to PDF using Python, you can use the XSL-FO (XSL Formatting Objects) technology, along with the xmlfo library. Here’s an example code snippet:

from xmlfo import XSLFormatter

# Read the XML data from a file
with open('input.xml', 'r') as f:
    xml_data =

# Read the XSL stylesheet from a file
with open('stylesheet.xsl', 'r') as f:
    xsl_data =

# Create an XSLFormatter object
formatter = XSLFormatter()

# Transform the XML data using the XSL stylesheet
pdf_data = formatter.render(xml_data, xsl=xsl_data, output_format='pdf')

# Write the PDF data to a file
with open('output.pdf', 'wb') as f:

In this example, we start by reading the XML data from a file and storing it in a string variable (xml_data). We also read the XSL stylesheet from a file and store it in a string variable (xsl_data).

Next, we create an XSLFormatter object from the xmlfo library. This object is used to transform the XML data using the XSL stylesheet.

We call the render() method of the XSLFormatter object to transform the XML data. We pass in the XML data, the XSL stylesheet, and specify the output format as 'pdf'.

Finally, we write the resulting PDF data to a file (output.pdf) using the write() method of a file object opened in binary mode ('wb').

Note that you need to have an XSL-FO processor installed on your system to use the xmlfo library. One popular open-source XSL-FO processor is Apache FOP. You can download and install Apache FOP from its official website ( Once installed, you can specify the path to the FOP executable as the fop_path argument when creating the XSLFormatter object. For example:

formatter = XSLFormatter(fop_path='/path/to/fop')

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply