HyperText Markup Language (HTML) is a standard markup language for web page creation. It allows the creation and structure of sections, paragraphs, and links using HTML elements/tags. And in case some custom fonts are used in an HTML file or any dynamic object is referenced which requires an active connection to the source machine/server, there are chances of compromising the fidelity of the document. On the other side, in XML Paper Specification(XPS), the page elements are defined independently of a particular operating system, printer, or viewing application. So an intelligent approach is to convert HTML to XPS format.
HTML Conversion API
Aspose.HTML Cloud SDK for Java is a lightweight REST architecture-based API providing the capabilities to create, manipulate and convert HTML files to PDF, DOCX, TIFF, JPEG, etc. It also supports the conversion of HTML to XPS. So, first, we need to add the following details in pom.xml of the Maven build type project to include the SDK in our Java project.
<repositories> <repository> <id>aspose-cloud</id> <name>artifact.aspose-cloud-releases</name> <url>http://artifact.aspose.cloud/repo</url> </repository> </repositories> <dependencies> <dependency> <groupId>com.aspose</groupId> <artifactId>aspose-html-cloud</artifactId> <version>20.7.0</version> <scope>compile</scope> </dependency> </dependencies>
After installation, the next step is a free subscription to our cloud services via Aspose.Cloud dashboard using GitHub or Google account. Or, simply create a new Account and obtain your Client Credentials details.
Convert HTML to XPS in Java
Please follow the instructions specified below to develop an HTML to XPS converter.
- First of all, we need to specify details against Configuration.setAPP_SID and Configuration.setAPI_KEY methods
- Secondly, we set details for setBasePath(..), setAuthPath(..) and specify setUserAgent(…) as WebKit
- Thirdly, for our own assistance, we are going to set setDebug(..) as true
- Now create an object of ConversionApi class
- Specify the margin details and name for information for the resultant file
- Finally, call GetConvertDocumentToXps(…) method which requires input HTML name, dimensions and margin details as arguments
The above code returns the result in the response stream so, in order to save the output on a local drive, we may consider using the following custom method.
HTML to XPS using cURL Commands
The REST APIs can also be accessed via cURL commands so in this section, we are going to learn the steps on how to perform the HTML to XPS conversion using the cURL command. Now as a pre-requisite, we need to first generate a JSON Web Token (JWT) based on your individual client credentials. Please execute the following command to generate the JWT token.
curl -v "https://api.aspose.cloud/connect/token" \ -X POST \ -d "grant_type=client_credentials&client_id=bbf94a2c-6d7e-4020-b4d2-b9809741374e&client_secret=1c9379bb7d701c26cc87e741a29987bb" \ -H "Content-Type: application/x-www-form-urlencoded" \ -H "Accept: application/json"
Now that we have the JWT token, please execute the following command on the terminal to perform HTML to XPS conversion.
curl -v -X GET "https://api.aspose.cloud/html/list.html/convert/xps" \ -H "accept: multipart/form-data" \ -H "authorization: Bearer <JWT Token>" \ -o final.xps
This article has explained the details for converting HTML to XPS using REST API. We have learned the steps to convert HTML to XPS using Java code snippets as well as through cURL commands. Furthermore, please note that the Product Documentation is a great source for learning the amazing capabilities being offered by the API. Furthermore, if you encounter any issues while using the API, please feel free to contact the Free product support forum.
We also recommend visiting the following blogs for further details regarding: