In order to transform XML into PDF, we’ll use Aspose.PDF for Java API which is a feature-rich, powerful and easy to use conversion API for Java platform. You can download its latest version directly from Maven and install it within your Maven-based project by adding the following configurations to the pom.xml.
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java AP</name>
<url>https://releases.aspose.com/java/repo/</url>
</repository>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-pdf</artifactId>
<version>version of aspose-pdf API</version>
</dependency>
Transform XML and load into PDF using Java
You need Aspose.PDF for Java to try the code in your environment.
- Set page parameters.
- Upload XSLT file.
- Then Load and Transform.
Transform XML into PDF - Java
try {
String xslFile = DATA_DIR.resolve("XMLFile1.xml").toString();
String xmlFile = DATA_DIR.resolve("XSLTFile1.xslt").toString();
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer =
factory.newTransformer(new StreamSource(xslFile));
StreamSource xmlSource = new StreamSource(xmlFile);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
StreamResult output = new StreamResult(baos);
transformer.transform(xmlSource, output);
com.aspose.pdf.HtmlLoadOptions options = new com.aspose.pdf.HtmlLoadOptions();
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document(bais, options);
pdfDocument.save(DATA_DIR.resolve("data_xml.pdf").toString());
} catch (Exception e) {
System.out.println(e.getMessage());
}