QUESTIONS OR COMMENTS ABOUT PRODUCTS? WRITE US AT SUPPORT@BYTESCOUT.COM OR USE THIS FORM

Transforming XML using XSLT and PDFDoc Scout into PDF document using PDFDoc Scout library

How to convert XML into PDF using XSLT template transformation



See also: XML to PDF example for Visual C# here

XML to PDF example for VB.NET here

You can download the source code of this example (VBScript) here:

pdfdocscout_xml_xslt_transformation_example.zip

This example describes how to transform XML into PDF document using XSLT (.xsl) template using PDFDoc Scout library

Source XML data (simpsons.xml):

<?xml version="1.0" encoding="UTF-8"?>

<?xml-stylesheet type="text/xsl" href="Simpsons.xsl"?>

<Simpsons>

<FamilyMember>Homer Jay Simpson</FamilyMember>

<FamilyMember>Marjorie Marge Simpson (Bouvier)</FamilyMember>

<FamilyMember>Bartholomew Jojo Bart Simpson</FamilyMember>

<FamilyMember>Lisa Marie Simpson</FamilyMember>

<FamilyMember>Margaret Maggie Simpson</FamilyMember>

<ExtendedFamilyMember>Abraham Jay Simpson</ExtendedFamilyMember>

<ExtendedFamilyMember>Mona J. Simpson</ExtendedFamilyMember>

<ExtendedFamilyMember>Jacqueline Ingrid Bouvier</ExtendedFamilyMember>

<ExtendedFamilyMember>Clancy Bouvier</ExtendedFamilyMember>

<ExtendedFamilyMember>Patricia "Patty" Bouvier</ExtendedFamilyMember>

</Simpsons>

XSLT template (simpsons.xsl):

<?xml version="1.0" ?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="FamilyMember">

<p><b><u><xsl:value-of select="."/></u></b></p>

</xsl:template>



<xsl:template match="ExtendedFamilyMember">

<p><i><xsl:value-of select="."/></i></p>

</xsl:template>

<xsl:template match="/">

<html>

<body>

<xsl:apply-templates/>

</body>

</html>

</xsl:template>



</xsl:stylesheet>

Produced PDF document (XML2HTML.pdf generated from source Simpsons.xml XML data)

PDF document produced from transformed XML simpsons.xml

Code to transform XML and convert into PDF using PDFDoc Scout lib:

<font color="black" face="Courier New" size="2"><font color="#008000">' Transform XML using XSLT and get HTML code</font><br>
<font color="#008000">' then create PDF document from produced HTML code</font><br>
<br>
<font color="#0000ff">Set</font> PDFDoc = CreateObject(<font color="#a31515">"PDFDocScout.PDFDocument"</font>)<br>
<br>
PDFDoc.InitLibrary <font color="#a31515">"demo"</font>, <font color="#a31515">"demo"</font><br>
PDFDoc.OutputFilename = <font color="#a31515">"XML2HTML.pdf"</font><br>
PDFDoc.AutoOpenGeneratedPDF = <font color="#0000ff">true</font><br>
<br>
PDFDoc.BeginDocument<br>
<br>
<font color="#008000">' load source XML document</font><br>
<font color="#0000ff">Set</font> xmlDocument = CreateObject(<font color="#a31515">"Msxml2.DOMDocument.4.0"</font>)<br>
xmlDocument.async = <font color="#0000ff">false</font><br>
xmlDocument.load(<font color="#a31515">"Simpsons.xml"</font>)<br>
<br>
<font color="#008000">' load XSLT template</font><br>
<font color="#0000ff">Set</font> xslDocument = CreateObject(<font color="#a31515">"Msxml2.DOMDocument.4.0"</font>)<br>
xslDocument.async = <font color="#0000ff">false</font><br>
xslDocument.load(<font color="#a31515">"Simpsons.xsl"</font>)<br>
<br>
<font color="#008000">' create XML doc to store result of transformation</font><br>
<font color="#0000ff">Set</font> objResultXML = CreateObject(<font color="#a31515">"Msxml2.DOMDocument.4.0"</font>)<br>
objResultXML.async = <font color="#0000ff">false</font><br>
objResultXML.validateOnParse = <font color="#0000ff">true</font><br>
<br>
<font color="#008000">' transform XML using XSLT into objResultXML document</font><br>
xmlDocument.transformNodeToObject xslDocument, objResultXML<br>
<br>
<font color="#008000">' convert produced HTML code from objResultXML document into PDF</font><br>
PDFDoc.Page.AddHTMLBox objResultXML.xml, 0,0, PDFDoc.Page.Width, PDFDoc.Page.Height, <font color="#0000ff">true</font><br>
<br>
PDFDoc.EndDocument <font color="#008000">' close PDF document generation</font><br>
<br>
<font color="#008000">' disconnect from library</font><br>
<font color="#0000ff">Set</font> PDFDoc = Nothing</font>

You can download the source code of this example (VBScript) here:

pdfdocscout_xml_xslt_transformation_example.zip

See examples for other languages:

XML to PDF example for Visual C# here

XML to PDF example for VB.NET here

Tutorials: