Para transformar XML en PDF, usaremos la API Aspose.PDF for Java, que es una API de conversión para la plataforma Java con muchas funciones, potente y fácil de usar. Puede descargar su última versión directamente desde [Maven](https://repository.aspose.com/webapp/ #/artifacts/browse/tree/general/repo/com/aspose/aspose-pdf) e instalarla en su proyecto basado en Maven añadiendo las siguientes configuraciones al 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>
Transforma XML y cárgalo en PDF a través de Java
Necesita Aspose.PDF for Java para probar el código en su entorno.
- Configure los parámetros de la página.
- Cargue el archivo XSLT.
- Luego carga y transforma.
Transforma XML en 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());
}