convert html to pdf

Learn how to convert HTML to PDF online.

In today’s digital age, the need to convert HTML to PDF has become increasingly important. HTML, the language of the web, allows us to create dynamic and interactive content that can be accessed across various devices and platforms. However, there are instances when we require a more static and universally accessible format for our web content. PDF, with its consistent layout, cross-platform compatibility, and print-ready nature, serves as an ideal solution. In this article, we will explore the ins and outs of HTML to PDF conversion using .NET REST API, empowering you to harness the power of PDF and take your web content to new heights.

HTML to PDF Conversion REST API

Aspose.HTML Cloud SDK for .NET is a powerful tool that enables seamless conversion of HTML to PDF. The SDK offers a user-friendly interface and a rich set of methods and options to customize the conversion process according to specific requirements. Therefore, with Aspose.HTML Cloud SDK for .NET, developers can effortlessly integrate HTML to PDF conversion capabilities into their .NET applications, streamlining workflows and enhancing the accessibility and usability of web content.

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

nuget install Aspose.HTML-Cloud

You may 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.

  • Firstly, please create an instance of HtmlApi while passing ClientID and ClientSecret details as arguments.
  • Secondly, read the HTML file content using File.OpenRead(..) method.
  • Thirdly, create an instance of StorageApi and use UploadFile(…) method to upload the input HTML to the cloud storage.
  • Lastly, call the method PutConvertDocumentToPdf(…) 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, respectively.

Convert Webpage to PDF using cURL Commands

Converting HTML to PDF using cURL commands offers several benefits. First, it provides a simple and straightforward method to initiate the conversion process without the need for complex coding or integration. Secondly, by leveraging cURL, you can easily send HTTP requests and handle responses, making it a convenient option for performing HTML to PDF conversion. Additionally, the cURL commands can be easily incorporated into scripts or automation workflows, allowing for efficient and automated batch conversions.

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.

Now, 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 input 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, please execute the following command to save webpage as HTML. The resultant file is then 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 conclusion, converting HTML to PDF is a crucial requirement for many applications, and with the availability of powerful tools like Aspose.HTML Cloud SDK for .NET and cURL commands, you have multiple options to achieve this task. Whether you prefer the simplicity and convenience of a dedicated SDK or the flexibility and automation capabilities of cURL commands, both approaches offer efficient and reliable solutions.

So, whether you’re building web applications, generating reports, or archiving web content, the ability to convert HTML to PDF using .NET REST API ensures that you can easily and effectively meet your business needs.

We highly recommend visiting the following links to learn more about: