Convert HTML to PDF in C# .NET

The demand for efficient document management solutions continues to grow exponentially. In this article, we delve into the benefits and practical applications of leveraging .NET Cloud API for HTML to PDF conversion. From streamlining workflows to ensuring compatibility across various platforms, discover how this feature can significantly enhance productivity and simplify document management tasks.

This article is covering following topics:

HTML to PDF Conversion API

We are going to explore the robust capabilities of GroupDocs.Conversion Cloud SDK for .NET and its pivotal role in seamlessly accomplishing the HTML to PDF conversion requirement. As businesses and developers seek efficient solutions for document management, this API emerges as a powerful tool, offering unparalleled ease and versatility. Let’s delve into how this conversion SDK empowers you to effortlessly convert HTML files to PDF format, facilitating smoother workflows and enhanced productivity.

Now, in order to use the SDK, the first step is its installation. Simply search GroupDocs.Conversion-Cloud in NuGet package manager and click the Install button. Another option is to execute the following command in package manager console.

NuGet\Install-Package GroupDocs.Conversion-Cloud -Version 24.2.0

Once the REST API is successfully installed, make sure you have the API credentials(i.e. Client ID and Client Secret). You may visit this short tutorial to see how to get the API credentials.

Convert HTML to PDF using C# .NET

In this section, we are going to explore the details on how to convert HTML to PDF programmatically using C# .NET.

  1. Create an instance of Configuration class where we pass client credentials as arguments.
var configurations = new Configuration(clientId, clientSecret1);
  1. Initialize the ConvertApi where we pass Configuration object as an input argument.
var apiInstance = new ConvertApi(configurations);
  1. Upload the input HTML file to cloud storage, where we provide the name for input HTML file.
fileUpload.UploadFile(new UploadFileRequest("input.html", stream));
  1. Create an instance ConvertSettings where we specify the name for input HTML, output format as pdf and the name for resultant PDF document.
var settings = new ConvertSettings{...}
  1. Call the ConvertDocumentRequest API to convert HTML to PDF format. After successful conversion, the resultant PDF is stored in cloud storage.
var response = apiInstance.ConvertDocument(new ConvertDocumentRequest(settings));
A preview of HTML to PDF conversion.

Image:- A preview of HTML to PDF conversion.

The sample PDF generated in the above example can be downloaded from resultant.pdf.

HTML to PDF using cURL Commands

Another option for converting HTML to PDF is a combination of GroupDocs.Conversion Cloud and cURL commands. The conversion of HTML to PDF using GroupDocs.Conversion Cloud via cURL commands offers several notable benefits such as, it provides a seamless and straightforward method for converting HTML files to PDF format, eliminating the need for complex manual processes. Therefore, by leveraging the GroupDocs.Conversion Cloud API, users can automate the conversion process, saving time and effort on repetitive tasks.

Ensure you have obtained your API credentials (App Key and App SID) from the GroupDocs dashboard and generate JWT access token. Once we have JWT token, please execute the following cURL command, where we have also provided properties for text watermark to be added during this conversion process.

curl -v "https://api.groupdocs.cloud/v2.0/conversion" \
-X POST \
-H "accept: application/json" \
-H "authorization: Bearer {accessToken}" \
-H "Content-Type: application/json" \
-d "{  \"StorageName\": \"internal\",  \"FilePath\": \"{sourceFile}\",  \"Format\": \"pdf\",  \"LoadOptions\": {    \"Format\": \"html\"  },  \"ConvertOptions\": {    \"FromPage\": 1,    \"PagesCount\": 1,    \"Pages\": [      1    ],    \"WatermarkOptions\": {      \"Text\": \"GroupDocs.Cloud\",      \"FontName\": \"Arial\",      \"FontSize\": 4,      \"Bold\": false,      \"Italic\": true,      \"Color\": \"olive\",      \"Width\": 10,      \"Height\": 6,      \"Top\": 100,      \"Left\": 100,      \"RotationAngle\": 10,      \"Transparency\": 0.8,      \"Background\": true,      \"AutoAlign\": true    }  },  \"OutputPath\": \"{converted}\"}"

Please replace sourceFile with the name of input HTML file, resultantFile with the name of output PDF format to be generated and accessToken with JWT token generated above.

Free HTML to PDF Conversion App

Based on GroupDocs.Conversion Cloud API, we have developed online HTML to PDF Converter. It is a Free, lightweight, super-efficient, and provides a robust HTML to PDF conversion.

free html to pdf app

Conclusion

We have learned that GroupDocs.Conversion Cloud offers a seamless, efficient, and reliable solution for document conversion tasks, empowering you to streamline your workflows and enhance productivity. So, by automating the conversion process and ensuring consistency and accuracy in the results, GroupDocs.Conversion Cloud API simplifies complex tasks and saves valuable time and resources. Therefore, whether you’re a developer looking to integrate document conversion capabilities into your applications or a business seeking to optimize your document management processes, our APIs provide the tools you need to succeed.

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