HTML to XPS – Convert HTML to XPS in C#

Convert HTML to XPS

The HTML file is widely used over the internet for information sharing. Since it is supported by the majority of platforms and almost all web browsers are capable of loading this format, so it makes it one of the acceptable formats. However, owing to its wide acceptance, many hackers try to embed malicious scripts inside HTML files. The high-profile attacks of malicious HTML and JavaScript code have seen a dramatic increase in both awareness and exploitation in recent years. On the other end, an XPS file represents page layout files that are based on XML Paper Specifications. It is similar to PDF file format but uses XML in the layout, appearance, and printing information of a document. Therefore, in this article, we are going to discuss the details on how to convert the HTML file to XPS format using C# .NET.

HTML processing API

Aspose.HTML Cloud is specifically designed to provide the capabilities to process HTML files. Apart from input HTML, we can also load XHTML, MHTML, EPUB, Markdown, or a Web page by its URL. The resultant output can be PDF, XPS, DOCX, or raster image formats: JPEG, PNG, TIFF, BMP, GIF. As the API is developed according to REST architecture, so it can be used in Desktop, Mobile, Web, and Hybrid applications, as well as desktop or mobile platforms.

In order to further facilitate our customers, we have created programming language-specific SDKs. The Cloud SDKs are wrappers around REST APIs in various programming languages, allowing you to process documents in the language of your choice. This way, you can quickly and easily, gain all benefits of strong types and IDE highlights. Please check the Aspose.HTML GitHub repository for SDKs along with working examples, to get you started in no time. In this article, we are going to discuss the document conversion features of Aspose.HTML Cloud SDK for .NET.

Installation

To get started with using the Cloud SDK, the first step is to install it over the system. The SDK is available over NuGet and GitHub. Please execute the following command in the terminal window to perform the installation for NuGet

nuget install Aspose.HTML-Cloud

Another option is to install the SDK is through Package Manager Console within Visual Studio. Please execute the following command:

 PM> Install-Package Aspose.HTML-Cloud
Package Manager Console
Image 1:- Aspose.HTML NuGet package installed.

NuGet Package Manager

Another easiest approach is to install the SDK via NuGet Package Manager within Visual Studio IDE.

  1. Open the Solution Explorer.
  2. Expand the project and Right-click Packages folder within your solution.
  3. Select Manage NuGet Packages… option
  4. Click on the Browse tab and search for “Aspose.HTML-Cloud
  5. Click on the Aspose.HTML-Cloud package, select the appropriate version in the right-tab and click Add Package button
Image 2:- Aspose.HTML Package in NuGet Package Manager.

Free Cloud Subscription

The next important step is a free account subscription on Aspose.Cloud dashboard. No credit card or other payment details are required. If you have GitHub or Google account, simply Sign Up or, click on the Create a new Account button and provide the required information. Now login to the dashboard using credentials and expand the Applications section from the dashboard and scroll down towards the Client Credentials section to see Client ID and Client Secret details.

Client Credentials
Image 3:- Client Credentials at Aspose.Cloud dashboard.

HTML to XPS in C#

In this section, we are going to discuss the conversion of HTML files to XPS format for the following two scenarios.

HTML from Cloud Storage

Please follow the instructions below to upload an HTML file to Cloud storage and then perform conversion to XPS format.

  • First of all, please create HtmlApi instance while passing ClientID and ClientSecret details as arguments
  • Secondly, read HTML file using File.OpenRead(..) method
  • Thirdly, create an instance of StorageApi and use UploadFile(…) method to upload HTML to Cloud storage
  • Then create PutConvertDocumentToXps(…) method to perform HTML to PDF conversion and save the output to Cloud storage
Image 4:- HTML to XPS conversion preview.

For testing purposes, the input HTML and resultant XPS can be downloaded from inputHTML.html and Resultant.xps.

Convert HTML to XPS using cURL

As our APIs are developed according to REST architecture, so they can also be accessed via cURL commands. In this section, we are going to discuss the conversion of the HTML page to XPS format using the cURL command. But before we proceed further, the first step is to create JSON Web Token (JWT) based on client credentials from Aspose.Cloud dashboard. Please follow the steps given below to perform the HTML to XPS conversion.

  1. Get your Client Id and Client Secret from Cloud dashboard
  2. Get JSON Web Access Token based on client credentials
  3. Call the PutConvertDocumentToXps API
  4. The resultant PDF file will be saved in Cloud storage

Please execute the following cURL command to generate the JWT access token.

 curl -v "https://api.aspose.cloud/connect/token" \
 -X POST \
 -d "grant_type=client_credentials&client_id=4db2f826-bf9c-42e7-8b2a-8cbca2d15553&client_secret=d757548a9f2558c39c2feebdf85b4c44" \
 -H "Content-Type: application/x-www-form-urlencoded" \
 -H "Accept: application/json"

Please execute the following command to upload the HTML files to Cloud storage:

curl -X PUT "https://api.aspose.cloud/html/storage/file/inputHTML.html" \
-H  "accept: application/json" \
-H  "authorization: Bearer <JWT Token>" \
-H  "Content-Type: multipart/form-data" \
-d {"File":{}}

After the HTML is uploaded to Cloud storage, the following command can be used to perform the HTML to XPS conversion and save the resultant file in Cloud storage.

curl -X PUT "https://api.aspose.cloud/html/inputHTML.html/convert/xps?outPath=converted.xps&width=800&height=1000&leftMargin=10&rightMargin=10&topMargin=10&bottomMargin=10" \
-H  "accept: application/json" \
-H  "authorization: Bearer <JWT Token>"

Conclusion

We have explored the capabilities of Aspose.HTML Cloud for the conversion of HTML files to fixed page layout and other raster images. We have explored the features to convert HTML using C# .NET code snippet as well as using the cURL commands.

Nevertheless, Aspose.HTML Cloud SDK for .NET which is a wrapper around Aspose.HTML Cloud is developed under MIT license, so the complete code snippet can be downloaded from GitHub. If you encounter any issues while using the API, please feel free to contact via the Free Product support forum.

Please use GroupDocs.viewer App to view resultant XPS for free.

Related articles

We recommend visiting the following links to learn more about