DEV Community

IDRSolutions
IDRSolutions

Posted on

How to convert PDF to HTML in PHP

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 in PHP. 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 PHP

Although the aforementioned services can be accessed using plain HTTP requests, this tutorial utilises our open-source PHP IDRCloudClient, which offers a straightforward PHP wrapper around the REST API.

Prerequisites

To install the idrsolutions-csharp-client package using NuGet, run the following command:

nuget install idrsolutions-php-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.

 ‘input’ => IDRCloudClient::INPUT_UPLOAD,
‘file’ => __DIR__ . ‘path/to/file.pdf’
);

$results = IDRCloudClient::convert(array(
‘endpoint’ => $endpoint,
‘parameters’ => $parameters
));

IDRCloudClient::downloadOutput($results, __DIR__ . ‘/’);

echo $results[‘downloadUrl’];
Enter fullscreen mode Exit fullscreen mode

Return result to a callback URL

The BuildVu Microservice supports a callback URL to send the status upon conversion completion, eliminating the need to constantly poll the service. You can provide the callback URL to the parameters array as demonstrated below:

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

$parameters = array(
//’token’ => ‘Token’, // Required only when connecting to the IDRsolutions trial and cloud subscription service
‘input’ => IDRCloudClient::INPUT_UPLOAD,
‘callbackUrl’ => ‘http://listener.url’,
‘file’ => __DIR__ . ‘path/to/file.pdf’
);
Enter fullscreen mode Exit fullscreen mode

Configuration Options

The BuildVu API allows for conversion customization using a stringified JSON object with key-value pair configuration options. Add these settings to the parameters array. A comprehensive list of options for converting PDF files to HTML or SVG can be found 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. Simply replace the input and file values in the parameters array with the following.

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

Using Authentication

If you require authentication for your BuildVu Microservice, provide username and password when converting and downloading HTML from PDF. Add two variables named username and password to the parameters array, as shown below.

‘username’ => ‘Username_If_Required’,
‘password’ => ‘Password_If_Required’,
Enter fullscreen mode Exit fullscreen mode

In such cases, you’ll also need to provide the authentication values to the downloadOutput method.

IDRCloudClient::downloadOutput($results, __DIR__ . ‘/’,’newFileName’,’username’,’password’);
Enter fullscreen mode Exit fullscreen mode

Top comments (0)