convert html to pdf

Learn how to convert HTML to PDF online.

HTML is a HyperText Markup Language file format used as the basis of a web page. It’s also one of the widely used formats for information sharing across the internet. 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 as we 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 to convert HTML to PDF using Cloud API.

HTML to PDF Conversion API

Aspose.HTML Cloud is a lightweight Cloud REST API providing the capabilities to implement HTML processing and conversion features within desktop, web & cloud-based applications. It enables us to convert HTML to fixed layout document formats such as PDF, XPS, or raster images (JPEG, PNG, BMP, or TIFF). So in order to implement the Web to PDF conversion routine in the .NET application, we are going to use Aspose.HTML Cloud SDK for .NET.

The SDK can be downloaded either from NuGet or GitHub and in order to install the SDK through NuGet, please execute the following command in a terminal window:

nuget install Aspose.HTML-Cloud

We can also install the package through the NuGet package manager by executing the following command:

PM> Install-Package Aspose.HTML-Cloud

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 the 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
HTML to PDF Conversion API

Image 1:- HTML to PDF Conversion API in NuGet Package Manager.

The next major 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.

HTML to PDF in C#

This section explains the conversion of Webpage to PDF. The input HTML is already stored in Cloud storage and after the conversion, the resultant file is also stored in cloud storage.

  • First of all, please create an HtmlApi instance while passing ClientID and ClientSecret details as arguments
  • Secondly, read the 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 2:- HTML to PDF conversion preview.

You may download the sample files used in the 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 the 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 the 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 the HTML file is uploaded to Cloud storage, please execute the following command to perform the conversion of HTML to PDF format, and the 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 an amazing feature of converting HTML to PDF using the C# .NET code snippet as well as through the cURL command. So with fewer code lines, we have managed to convert the HTML to PDF online. Please note that Aspose.HTML Cloud is developed under an 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 us via the Free Product support forum.

We recommend visiting the following links to learn more about