In this article I will show you how you can use our PDF files to HTML API and generate HTML from PDF with our library BuildVu. BuildVu is the best PDF to HTML conversion tool for developers. PDF to HTML conversion helps you to optimise your PDF content for display on browsers.
PDF vs HTML for C# Developers
Incorporating documents in your .NET applications can be tricky if you want to optimise it for web usage. HTML is much more efficient from a searchability perspective as well, something that will crucial in determining the success of your application.
We have a separate article explaining the benefits of converting PDF to HTML.
Convert PDF to HTML using C#
Although the aforementioned services can be accessed using plain HTTP requests, this tutorial utilizes our open-source C# IDRCloudClient, which offers a straightforward C# wrapper around the REST API.
Prerequisites
To install the idrsolutions-csharp-client package using NuGet, run the following command:
nuget install idrsolutions-csharp-client
Code Example
This example demonstrates a simple way to convert PDF documents into HTML or SVG formats. You’ll find more advanced functionality and configuration options outlined in the sections that follow.
using System;
using System.Collections.Generic;
using idrsolutions-csharp-client;
class ExampleUsage
{
static void Main(string[] args)
{
var client = new IDRCloudClient("https://cloud.idrsolutions.com/cloud/" + IDRCloudClient.BUILDVU);
try
{
Dictionary<string, string> parameters = new Dictionary<string, string>
{
//["token"] = "Token", //Required only when connecting to the IDRsolutions trial and cloud subscription service
["input"] = IDRCloudClient.UPLOAD,
["file"] = "path/to/input.pdf"
};
Dictionary<string, string> results = client.Convert(parameters);
String outputUrl = results.GetValueOrDefault("downloadUrl", "No download URL provided");
client.DownloadResult(results, "path/to/output/dir");
Console.WriteLine("Converted: " + outputUrl);
}
catch (Exception e)
{
Console.WriteLine("Conversion failed: " + e.Message);
}
}
}
Return result to a callback url
You can configure the BuildVu Microservice to send a notification to a callback URL once the conversion process finishes. This approach removes the need for repeatedly checking the service for updates.
To enable this, simply pass the callback URL into the convert method as shown in the following example.
Dictionary<string, string> parameters = new Dictionary<string, string>
{
//["token"] = "Token", //Required only when connecting to the IDRsolutions trial and cloud subscription service
["callbackUrl"] = "http://listener.url",
["input"] = IDRCloudClient.UPLOAD,
["file"] = "path/to/input.pdf"
};
Configuration Options
To tailor the conversion process, the BuildVu API accepts a JSON-formatted string containing key-value pairs. These customization settings must be included in the parameters array. You can explore the full set of available options for converting PDF files into HTML or SVG in the configuration guide.
["settings"] = "{\"key\":\"value\",\"key\":\"value\"}"
Upload by URL
Instead of uploading a file directly, you can instruct the BuildVu Microservice to fetch and convert a document from a specified URL.
To do this, update the parameters variable by replacing the input and file entries with the following configuration.
["input"] = IDRCloudClient.DOWNLOAD
["url"] = "http://exampleURL/exampleFile.pdf"
Using Authentication
When using a self-hosted BuildVu Microservice that enforces authentication, you’ll need to supply a username and password for every conversion request. These credentials should be passed as two separate variables—username and password—to the convert method, as illustrated below.
var client = new IDRCloudClient("http://exampleURL.com/" + IDRCloudClient.BUILDVU, "username", "password");
By applying the methods outlined earlier, you can seamlessly convert PDF files into HTML within your C# projects using BuildVu.