DEV Community

IDRSolutions
IDRSolutions

Posted on

How to convert PDF to HTML in C#

In this article I will show you how you can use our PDF files to HTML API to convert documents to HTML 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. 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
Enter fullscreen mode Exit fullscreen mode

Code Example

Here is a basic code example for converting PDF files to HTML or SVG. Detailed configuration options and advanced features are provided below.

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);
}
}
}
Enter fullscreen mode Exit fullscreen mode

Return result to a callback url

The BuildVu Microservice supports a callback URL to notify you on the status of conversion completion. Using a callback URL eliminates the need to poll the service to check when the conversion is complete.

The callback URL can be provided to the convert method as demonstrated below:

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"
};
Enter fullscreen mode Exit fullscreen mode

Configuration Options

The BuildVu API allows for conversion customization using a stringified JSON object with key-value pairs. These settings should be added to the parameters array. A comprehensive list of configuration options for converting PDF files to HTML or SVG is available here.

["settings"] = "{\"key\":\"value\",\"key\":\"value\"}"
Enter fullscreen mode Exit fullscreen mode

Upload by URL

In addition to uploading a local file, you can provide a URL for the BuildVu Microservice to download and convert. Replace the input and file values in the parameters variable with the following settings.

["input"] = IDRCloudClient.DOWNLOAD
["url"] = "http://exampleURL/exampleFile.pdf"
Enter fullscreen mode Exit fullscreen mode

Using Authentication

If you’ve deployed your own BuildVu Microservice that requires a username and password for converting PDF files to HTML or SVG, you must provide these credentials for each conversion. Pass two variables named username and password to the convert

var client = new IDRCloudClient("http://exampleURL.com/" + IDRCloudClient.BUILDVU, "username", "password");
Enter fullscreen mode Exit fullscreen mode

Top comments (0)