How to convert HTML to PDF using C# in .NET Framework

Cloudmersive
1 min readSep 20, 2019

HTML is great for web design, but not always the most convenient format for other uses. Converting between the two can be a hassle, especially in large volumes. Today, however, we will be showing you how to harness one of our cloud APIs to quickly accomplish this task, even on numerous files.

Let’s start by installing the API client that we need using the console of the Package Manager.

Install-Package Cloudmersive.APIClient.NET.DocumentAndDataConvert -Version 1.3.4

Now simply call ConvertDocumentHtmlToPdf and provide it with your HTML file.

using System;
using System.Diagnostics;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Api;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Client;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Model;
namespace Example
{
public class ConvertDocumentHtmlToPdfExample
{
public void main()
{
// Configure API key authorization: Apikey
Configuration.Default.AddApiKey("Apikey", "YOUR_API_KEY");
// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
// Configuration.Default.AddApiKeyPrefix("Apikey", "Bearer");
var apiInstance = new ConvertDocumentApi();
var inputFile = new System.IO.Stream(); // System.IO.Stream | Input file to perform the operation on.
try
{
// HTML to PDF
byte[] result = apiInstance.ConvertDocumentHtmlToPdf(inputFile);
Debug.WriteLine(result);
}
catch (Exception e)
{
Debug.Print("Exception when calling ConvertDocumentApi.ConvertDocumentHtmlToPdf: " + e.Message );
}
}
}
}

And it’s as simple as that. This function supports CSS, JavaScript, and images, provided they have an absolute URL.

--

--

Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.