HTML to PDF – Convert HTML to PDF in C#

Convert HTML to PDF

HTML is a HyperText Markup Language file format used as the basis of a web page. The HTML tags are used to define headings, paragraphs, lists, links, quotes, and interactive forms. Therefore, HTML is one of the widely used formats for information sharing across the internet. Furthermore, it is supported by the majority of web browsers on all leading platforms. However, if some custom fonts are used inside the HTML file, there can be formatting issues when viewing the files. So, one of the viable solutions is to convert the HTML files to PDF format. The best thing about PDFs is how you can compress unlimited information into a file size that is easy to share or receive without compromising on the quality.

In this article, we are going to discuss the steps for converting HTML to PDF using Cloud API.

HTML processing API

Aspose.HTML Cloud is a lightweight Cloud REST API providing the capabilities to build desktop, web & cloud-based applications to process, render & convert HTML pages. Perform HTML manipulation in the cloud to render & convert HTML cloud pages into other file formats. The conversion can be towards fixed layout document formats such as PDF, XPS, or to the raster images i.e. JPEG, PNG, BMP, or TIFF. For further details, please visit Supported File Formats. Perform all the document processing without any software installed.

To further facilitate our customers, we have also created programming SDKs are wrappers around REST APIs in various programming languages. They enable you to process documents in the language of your choice quickly and easily, gaining all benefits of strong types and IDE highlights. In this article, we are going to discuss the capabilities of Aspose.HTML Cloud SDK for .NET.

Installation

To get started with Cloud SDK, the first step is its installation. The SDK can be downloaded either from NuGet or GitHub. Please install the SDK through NuGet, please execute the following command in a terminal window:

nuget install Aspose.HTML-Cloud

In order to install the package through NuGet package manager, please execute the following command:

PM> Install-Package Aspose.HTML-Cloud
Aspose.HTML NuGet
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
NuGet Package Manager
Image 2:- Aspose.HTML Package in NuGet Package Manager.

Cloud Subscription

Our Cloud APIs are protected so that only authorized persons can access them. Reason being, the users upload their protected/personal files to cloud storage and in order to ensure data integrity and secrecy, the client credentials mechanism has been implemented. Please note that subscription on cloud storage is free and you can perform upto 150 free document processing operations. So once you are satisfied with the capabilities of API, you may consider opting for license subscription which can be as low as $0.005 / API call. For further details, please visit Pricing.

Now proceed with account creation by visiting 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 PDF in C#

This scenario explains the conversion of an HTML document already stored in Cloud storage to PDF and save the resultant file to the storage.

  • 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 PutConvertDocumentToPdf(…) method to perform HTML to PDF conversion and save the output to Cloud storage
Conversion preview
Image 4:- HTML to PDF conversion preview.

Please consider downloading the sample files used in above example from inputHTML.html and Resultant.pdf.

Convert HTML to PDF using cURL

Aspose.HTML Cloud is developed on REST architecture, so it can also be accessed via cURL commands. Please follow the steps given below to perform the HTML to PDF 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 PutConvertDocumentToPdf 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 HTML file 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":{}}

Now that HTML file is uploaded on Cloud storage, please execute the following command to perform the conversion of HTML to PDF format and resultant file is stored in Cloud storage.

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

Conclusion

In this article, we have discussed the features of Aspose.HTML Cloud. Specifically, we have discussed the details on how to convert HTML file to PDF format. The users can perform conversion using C# .NET code snippet or using the cURL command. 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 issue while using the API, please feel free to contact via Free Product support forum.

Related articles

We recommend visiting the followimng links to learn more about