How to Convert HTML to PDF in .NET with C#
Why Convert HTML to PDF with C#?
The pdfRest Convert to PDF API Tool is a powerful solution for developers looking to integrate HTML-to-PDF conversion capabilities into their applications. This tutorial will guide you through the process of sending an API call to convert HTML to PDF using C#. By leveraging the pdfRest API, you can easily convert HTML files into PDFs, enhancing the functionality of your application with minimal effort.
Consider a scenario where a company needs to convert a batch of HTML reports into PDF documents for archival purposes. Using the Convert to PDF API, developers can automate this process, ensuring consistency and saving time. This not only streamlines workflows but also ensures that the output is in a universally accepted format, making it easier to share and store.
Convert HTML to PDF with C# Code Example
using System.Text; using (var httpClient = new HttpClient { BaseAddress = new Uri("https://api.pdfrest.com") }) { using (var request = new HttpRequestMessage(HttpMethod.Post, "pdf")) { request.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"); request.Headers.Accept.Add(new("application/json")); var multipartContent = new MultipartFormDataContent(); var byteArray = File.ReadAllBytes("/path/to/file"); var byteAryContent = new ByteArrayContent(byteArray); multipartContent.Add(byteAryContent, "file", "file_name"); byteAryContent.Headers.TryAddWithoutValidation("Content-Type", "text/html"); var byteArrayOption = new ByteArrayContent(Encoding.UTF8.GetBytes("A4")); multipartContent.Add(byteArrayOption, "page_size"); var byteArrayOption2 = new ByteArrayContent(Encoding.UTF8.GetBytes("output.pdf")); multipartContent.Add(byteArrayOption2, "output"); request.Content = multipartContent; var response = await httpClient.SendAsync(request); var apiResult = await response.Content.ReadAsStringAsync(); Console.WriteLine("API response received."); Console.WriteLine(apiResult); } }
Source: GitHub
Breaking Down the Code
The code begins by setting up an HttpClient
with a base address pointing to the pdfRest API. This client will be used to send HTTP requests.
using (var httpClient = new HttpClient { BaseAddress = new Uri("https://api.pdfrest.com") })
Next, an HttpRequestMessage
is created for a POST request to the "pdf" endpoint. The API key is added to the request headers to authenticate the request. The Accept header specifies that the response should be in JSON format.
using (var request = new HttpRequestMessage(HttpMethod.Post, "pdf")) { request.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"); request.Headers.Accept.Add(new("application/json"));
A MultipartFormDataContent
object is created to hold the file and additional parameters. The file to be converted is read into a byte array, which is then added to the multipart content with a specified content type of "text/html".
var byteArray = File.ReadAllBytes("/path/to/file"); var byteAryContent = new ByteArrayContent(byteArray); multipartContent.Add(byteAryContent, "file", "file_name"); byteAryContent.Headers.TryAddWithoutValidation("Content-Type", "text/html");
Additional options, such as "page_size" and "output", are added to the multipart content. These parameters control the page size of the PDF and the name of the output file, respectively.
var byteArrayOption = new ByteArrayContent(Encoding.UTF8.GetBytes("A4")); multipartContent.Add(byteArrayOption, "page_size"); var byteArrayOption2 = new ByteArrayContent(Encoding.UTF8.GetBytes("output.pdf")); multipartContent.Add(byteArrayOption2, "output");
The request content is set to the multipart content, and the request is sent asynchronously. The response is read and printed to the console.
request.Content = multipartContent; var response = await httpClient.SendAsync(request); var apiResult = await response.Content.ReadAsStringAsync(); Console.WriteLine("API response received."); Console.WriteLine(apiResult);
Beyond the Tutorial
In this tutorial, you learned how to use C# to send an API call to the pdfRest Convert to PDF endpoint for converting HTML files to PDF format using a multipart API call. To further explore the capabilities of pdfRest, you can demo all of the API Tools in the API Lab.
For more detailed information on the API endpoints and parameters, refer to the API Reference Guide. This is an example of a multipart API call, and code samples using JSON payloads can be found at GitHub.