Java: Convert XML to PDF

An Extensible Markup Language (XML) file is a plain text file that uses custom tags to describe the structure and other features of a document. In some cases, you may need to convert XML to PDF because the latter one is easier for others to access. This article will show you how to programmatically convert XML to PDF using Spire.Doc for Java.

Install Spire.Doc for Java

First of all, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.doc</artifactId>
        <version>12.10.3</version>
    </dependency>
</dependencies>
    

Convert XML to PDF

Spire.Doc for Java supports converting XML to PDF using the Document.saveToFile() method. The following are detailed steps.

  • Create a Document instance.
  • Load an XML sample document using Document.loadFromFile() method.
  • Save the document as a PDF file using Document.saveToFile() method.
  • Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;

public class XMLToPDF {
    public static void main(String[] args) {
        //Create a Document instance
        Document document = new Document();
        //Load a XML sample document
        document.loadFromFile("toXML.xml");
        //Save  the document to PDF
        document.saveToFile("output/XMLToPDF.pdf", FileFormat.PDF );
    }
}

Java: Convert XML to PDF

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.