Creating PDF from XML using XSLT

Sometimes you may have existing XML files that contain application data and you want to generate PDF report using these files. You can use XSLT to transform your existing XML document to Aspose.Pdf’s compatible XML document and then generate PDF file. There are 3 steps to generate PDF using XML and XSLT.

Please follow these steps to convert an XML file into a PDF document using XSLT:

  • Create an instance of PDF class that represents a PDF document
  • If you have purchased a license then you should also embed the code to use that license with the help of License class in Aspose.Pdf namespace
  • Bind the input XML and XSLT files to the instance of PDF class by calling its BindXML method
  • Save the bound XML with PDF instance as a PDF document

Input XML File

<?xml version="1.0" encoding="utf-8" ?>
  <Content>Hello World!</Content>

Input XSLT File

<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="">
    <xsl:template match="text()"/>
    <xsl:template match="/Contents">
      <Document xmlns="Aspose.Pdf" IsAutoHyphenated="false">
                            Font = "Helvetica" FontSize="8" LineSpacing="4"/>
          <Margin Left="5cm" Right="5cm" Top="3cm" Bottom="15cm" />
        <Page id="mainSection">
              <xsl:value-of select="Content"/>
using namespace System;
using namespace Aspose::Pdf;

static System::SharedPtr<System::IO::MemoryStream> TransformXmltoHtml(String inputXml, String xsltString);

void WorkingWithXML::CreatingPDFfromXMLusingXSLT()
 String _dataDir("C:\\Samples\\");
 //Create pdf document
 auto pdf = MakeObject<Aspose::Pdf::Document>();
 //Bind XML and XSLT files to the document
  pdf->BindXml(_dataDir + u"\\HelloWorld.xml", _dataDir + u"\\HelloWorld.xslt");
 catch (System::Exception ex)
  std::cerr << ex.what();

 //Save the document
 pdf->Save(_dataDir + u"HelloWorldUsingXmlAndXslt.pdf");