Convert HTML to PDF in C# using REST API

Convert HTML to PDF in C# using REST API

HTML files are widely used over the internet. HTML to PDF conversion is helpful for viewing, reading offline documents, printing or sharing converted PDF files in protected form. In various scenarios, you need to convert web pages or HTML files to PDF documents programmatically in your .NET applications. So in this article, we are going to discuss the steps on how to convert HTML to PDF in C# using REST API.

The following topics shall be discussed in this article:

HTML to PDF C# Converter API - Free Download

To convert HTML webpage to PDF file or for batch conversion of HTML files to PDF, I will be using .NET SDK of GroupDocs.Conversion Cloud API. The reason we are discussing this HTML file to PDF file converter free download C# library here is that it is a powerful, efficient, and high-performance file conversion solution. It provides high quality conversion of more than 50 types of documents and images, including all MS Office, PDF, HTML, CAD, raster images and many more. It converts a list of supported source document formats into any other supported file format. Currently, it provides C#, Java, PHP, Ruby, Python and Node.js SDKs as its document conversion family members for the Cloud API.

You can install GroupDocs.Conversion .Net SDK to your Visual Studio project from the NuGet Package manager or using the following command in .Net CLI:

dotnet add package GroupDocs.Conversion-Cloud --version 22.10.0

After installation, you need to get your Client ID and Client Secret from the dashboard before you start following the steps and available code samples. Add your Client ID and Client Secret in the code as shown below:

//Get clientId & clientSecret from https://dashboard.groupdocs.cloud (free registration is required).
string clientId = "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
string clientSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
string myStorage = "test-internal-storage";
var configuration = new Configuration(clientId, clientSecret);
configuration.ApiBaseUrl = "https://api.groupdocs.cloud";

How to Convert HTML to PDF in C# using REST API

In this section, we are going to discuss the details on how to convert HTML page to PDF file in C# using REST API by following the simple steps as given below:

Upload the Document

Firstly, upload the HTML document to the Cloud storage using any of the following methods:

  • Using the dashboard
  • Upload source file using Upload File API from the browser
  • Upload programmatically using the code example given below
using System;
using GroupDocs.Conversion.Cloud.Sdk.Api;
using GroupDocs.Conversion.Cloud.Sdk.Client;
using GroupDocs.Conversion.Cloud.Sdk.Model.Requests;
using System.IO;
namespace GroupDocs.Conversion.CSharp
{
// Upload Sample File
class Upload_File
{
static void Main(string[] args)
{
try
{
// initialize api
var fileApi = new FileApi(configuration);
// open file
var fileStream = File.Open(@"H:\groupdocs-cloud-data\input-sample-file.html", FileMode.Open);
// create file upload request
var request = new UploadFileRequest("csharp-testing/input-sample-file.html", fileStream, myStorage);
// upload file
var response = fileApi.UploadFile(request);
Console.WriteLine("HTML File Upload Process Completed: " + response.Uploaded.Count.ToString());
fileStream.Close();
}
catch (Exception e)
{
Console.WriteLine("Exception when calling GroupDocs FileApi: " + e.Message);
}
}
}
}

As the result, source HTML file will be uploaded to the Cloud Storage.

Convert HTML File to PDF Online

This section demonstrates how to convert HTML to PDF online in C# programmatically using REST API. Follow the steps mentioned below:

  • Firstly, create an instance of the ConvertApi
  • Secondly, create an instance of the ConvertSettings
  • Thirdly, provide the input HTML file path
  • Next, assign “pdf” to the format
  • Then, provide the output file path
  • Now, create ConvertDocumentRequest with ConvertSettings
  • Finally, convert by calling the convertDocument() method with ConvertDocumentRequest

The following code sample shows how to convert HTML document to PDF document in C# using REST API:

// How to Convert HTML to PDF in C# using REST API
using System;
using GroupDocs.Conversion.Cloud.Sdk.Api;
using GroupDocs.Conversion.Cloud.Sdk.Client;
using GroupDocs.Conversion.Cloud.Sdk.Model;
using GroupDocs.Conversion.Cloud.Sdk.Model.Requests;
namespace GroupDocs.Conversion.CSharp
{
// Change HTML to PDF File Online
class Convert_HTML_to_PDF
{
static void Main(string[] args)
{
try
{
// Create necessary API instances
var apiInstance = new ConvertApi(configuration);
// Prepare convert settings
var settings = new ConvertSettings
{
FilePath = "csharp-testing/input-sample-file.html",
Format = "pdf",
OutputPath = "csharp-testing/output-sample-file.pdf"
};
// Convert to specified format
var response = apiInstance.ConvertDocument(new ConvertDocumentRequest(settings));
Console.WriteLine("Successfully converted HTML to PDF format online in C#: " + response[0].Url);
}
catch (Exception e)
{
Console.WriteLine("Exception when calling GroupDocs ConvertApi: " + e.Message);
}
}
}
}

Converted PDF Document

How to convert HTML File to PDF Online in C#

Convert HTML File to PDF Online

Download the Converted File

This is how you can convert an HTML file to PDF. Next, download the converted PDF file from the cloud using the following code snippet:

// How to Download converted HTML file in C#
using System;
using GroupDocs.Conversion.Cloud.Sdk.Api;
using GroupDocs.Conversion.Cloud.Sdk.Client;
using GroupDocs.Conversion.Cloud.Sdk.Model.Requests;
namespace GroupDocs.Conversion.CSharp
{
// Download Output File
class Download_File
{
static void Main(string[] args)
{
try
{
// initialize api
var fileApi = new FileApi(configuration);
// create download file request
var downloadRequest = new DownloadFileRequest("csharp-testing/output-sample-file.pdf", myStorage);
// download file
Stream downloadResponse = fileApi.DownloadFile(downloadRequest);
// save file in working directory
using (var fileStream = System.IO.File.Create("H:\\groupdocs-cloud-data\\output-sample-file.pdf"))
{
downloadResponse.Seek(0, SeekOrigin.Begin);
downloadResponse.CopyTo(fileStream);
}
}
catch (Exception e)
{
Console.WriteLine("Exception when calling GroupDocs FileApi: " + e.Message);
}
}
}
}

Convert HTML to PDF in C# using Advanced Options

.NET SDK lets you convert HTML files to encrypted PDF documents. Convert HTML webpage to PDF file with some advanced settings by following the steps given below:

  • Firstly, create an instance of the ConvertApi
  • Secondly, create an instance of the ConvertSettings
  • Thirdly, provide the input html file path
  • Next, assign “pdf” to format
  • Then, create an instance of the PdfConvertOptions
  • You can optionally set various convert options such as CenterWindow, FromPage, margins (top, left, right, bottom), etc.
  • Next, provide the output file path
  • Then, create ConvertDocumentRequest with ConvertSettings
  • Lastly, convert HTML file to PDF document by calling the convertDocument() method with ConvertDocumentRequest

The following code example shows how to save HTML page as PDF online with advanced settings:

// How to Convert HTML to PDF in C# using Advanced Options
using System;
using GroupDocs.Conversion.Cloud.Sdk.Api;
using GroupDocs.Conversion.Cloud.Sdk.Client;
using GroupDocs.Conversion.Cloud.Sdk.Model;
using GroupDocs.Conversion.Cloud.Sdk.Model.Requests;
namespace GroupDocs.Conversion.CSharp
{
// Change HTML to PDF File Online
class Convert_HTML_to_PDF
{
static void Main(string[] args)
{
try
{
// Create necessary API instances
var apiInstance = new ConvertApi(configuration);
// Prepare convert settings
var settings = new ConvertSettings
{
FilePath = "csharp-testing/input-sample-file.html",
Format = "pdf",
ConvertOptions = new PdfConvertOptions
{
CenterWindow = true,
CompressImages = false,
DisplayDocTitle = true,
Dpi = 1024,
FitWindow = false,
FromPage = 1,
Grayscale = false,
ImageQuality = 100,
Linearize = false,
MarginTop = 5,
MarginLeft = 5,
Password = "password",
UnembedFonts = true,
RemoveUnusedStreams = true,
RemoveUnusedObjects = true,
RemovePdfaCompliance = false
},
OutputPath = "csharp-testing/output-sample-file.pdf"
};
// Convert to specified format
var response = apiInstance.ConvertDocument(new ConvertDocumentRequest(settings));
Console.WriteLine("Successfully converted HTML to PDF online using C#: " + response[0].Url);
}
catch (Exception e)
{
Console.WriteLine("Exception when calling GroupDocs ConvertApi: " + e.Message);
}
}
}
}

This is how to save HTML as PDF. Please follow the steps mentioned earlier to upload and download a file.

Output

How to convert HTML file to PDF in C# using advanced options

Convert HTML to PDF in C# using Advanced Options

Online HTML to PDF Converter Free

How to convert HTML to PDF free online? Please try the following free online HTML to PDF converter to convert HTML file to PDF online free, which is developed using the above API.

Conclusion

In this article, we have explored the steps on:

  • how to change HTML to PDF online in C# on the cloud;
  • programmatically upload the html webpage and then download the converted PDF file from the cloud;
  • online convert HTML to PDF without losing formatting in C# using advanced options;
  • HTML to PDF converter online free;

You can learn more about GroupDocs.Conversion Cloud API using the documentation. We also provide an API Reference section that lets you visualize and interact with our APIs directly through the browser. If you are interested to make changes to the source code of C# Cloud SDK, it can be downloaded from GitHub.

Finally, groupdocs.com is writing new blog articles. So, please stay in touch for regular updates.

Ask a question

In case you encounter any issues while using the HTML to PDF converter library, please feel free to contact us via our Free Support Forum.

FAQs

How to convert HTML pages into PDF files?

Please follow this link to learn the C# code snippet for how to save HTML file as a PDF quickly and easily.

How to download HTML to PDF converter SDK?

Install HTML to PDF file converter free download C# library to create, download, and process HTML to PDF conversion programmatically.

What is the best HTML to PDF converter online for free?

Online HTML to PDF converter allows you to convert single or multiple HTML pages to PDF documents for free. Convert HTML to PDF online for free using online HTML to PDF converter quickly, in seconds.

How do I convert HTML to PDF online free?

  • Open our HTML to PDF converter online free
  • Click inside the file drop area to upload HTML file or drag & drop HTML file.
  • Click on Convert Now button. Your HTML webpage will be uploaded and converted to PDF file format.
  • Download link of output files will be available instantly after conversion.

How to convert HTML to PDF in windows 10 free?

Please visit this link to download HTML to PDF converter software free. This free document converter software will convert HTML file to PDF in windows 10 with a single click.

See Also

We also recommend visiting following links for additional help and support: