Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active October 3, 2025 13:58
Show Gist options
  • Select an option

  • Save aspose-com-gists/1a525203492c6cafa61f9c05acb23fac to your computer and use it in GitHub Desktop.

Select an option

Save aspose-com-gists/1a525203492c6cafa61f9c05acb23fac to your computer and use it in GitHub Desktop.
Convert XML to PDF in Python | Convert XML File to PDF
<?xml version="1.0" encoding="utf-8" ?>
<catalog>
<cd>
<Content>Hello World!</Content>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
<cd>
<title>Greatest Hits</title>
<artist>Dolly Parton</artist>
<country>USA</country>
<company>RCA</company>
<price>9.90</price>
<year>1982</year>
</cd>
<cd>
<title>Still got the blues</title>
<artist>Gary Moore</artist>
<country>UK</country>
<company>Virgin records</company>
<price>10.20</price>
<year>1990</year>
</cd>
<cd>
<title>Eros</title>
<artist>Eros Ramazzotti</artist>
<country>EU</country>
<company>BMG</company>
<price>9.90</price>
<year>1997</year>
</cd>
</catalog>
import aspose.pdf as ap
# Create a new PDF document
pdfDocument = ap.Document();
# Transform and bind XML
pdfDocument.bind_xml( "C:\\Files\\data.xml", "C:\\Files\\template.xslt");
# Generate PDF from XML
pdfDocument.save( "C:\\Files\\generated-pdf-table.pdf");
<?xml version="1.0" encoding="utf-8" ?>
<Document xmlns="Aspose.Pdf">
<Page>
<TextFragment>
<TextSegment>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla odio lorem, luctus in lorem vitae, accumsan semper lectus. Cras a auctor leo, et tincidunt lacus.</TextSegment>
</TextFragment>
</Page>
</Document>
import aspose.pdf as ap
# Create a new PDF document
pdfDocument = ap.Document();
# Transform and bind XML
pdfDocument.bind_xml( "C:\\Files\\sample.xml");
# Generate PDF from XML
pdfDocument.save( "C:\\Files\\generated-pdf.pdf");
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<Document xmlns="Aspose.Pdf">
<Page>
<PageInfo IsLandscape="false" Height="595" Width="420">
<Margin Top="71" Bottom="71" Left="28" Right="28" />
</PageInfo>
<Header>
<Margin Top="20" />
<Table ColumnAdjustment="AutoFitToWindow">
<Row>
<Cell Alignment="1">
<TextFragment>
<TextSegment>Date: 11/01/2024</TextSegment>
</TextFragment>
</Cell>
<Cell Alignment="3">
<TextFragment>
<TextSegment>Page $p / $P</TextSegment>
</TextFragment>
</Cell>
</Row>
</Table>
</Header>
<HtmlFragment>
<![CDATA[
<h1 style="font-family:Tahoma; font-size:16pt;">My CD Collection</h1>
]]>
</HtmlFragment>
<TextFragment>
<TextSegment>Welcome</TextSegment>
</TextFragment>
<Table ColumnAdjustment="AutoFitToWindow" ColumnWidths ="10 10 10 10">
<DefaultCellPadding Top="5" Left="0" Right="0" Bottom="5" />
<Border>
<Top Color="Black"></Top>
<Bottom Color="Black"></Bottom>
<Left Color="Black"></Left>
<Right Color="Black"></Right>
</Border>
<Margin Top="15" />
<Row BackgroundColor="LightGray" MinRowHeight="20">
<Border>
<Bottom Color="Black"></Bottom>
</Border>
<Cell Alignment="2">
<TextFragment>
<TextSegment>Title</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>Artist</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>Price</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>Year</TextSegment>
</TextFragment>
</Cell>
</Row>
<xsl:for-each select="catalog/cd">
<Row>
<Cell Alignment="2">
<TextFragment>
<TextSegment><xsl:value-of select="title"/></TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment><xsl:value-of select="artist"/></TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment><xsl:value-of select="price"/></TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment><xsl:value-of select="year"/></TextSegment>
</TextFragment>
</Cell>
</Row>
</xsl:for-each>
</Table>
</Page>
</Document>
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment