Aby przekształcić XML w PDF, użyjemy interfejsu API Aspose.PDF for Java, który jest bogatym w funkcje, wydajnym i łatwym w użyciu interfejsem API konwersji dla platformy Java. Możesz pobrać jego najnowszą wersję bezpośrednio z [Maven](https://repository.aspose.com/webapp/ #/artifacts/browse/tree/general/repo/com/aspose/aspose-pdf) i zainstalować ją w projekcie opartym na Maven, dodając następujące konfiguracje do 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>
Przekształć XML i załaduj do pliku PDF poprzez Java
Potrzebujesz Aspose.PDF for Java, aby wypróbować kod w swoim środowisku.
- Ustaw parametry strony.
- Prześlij plik XSLT.
- Następnie załaduj i przekształć.
Przekształć XML w 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());
}