How to Easily Convert Common Documents to HTML in PHP

Cloudmersive
4 min readMay 20, 2024

Converting our documents to HTML presents numerous advantages — especially when we can boil our conversions down to quick, low-code API calls.

Using the PHP code examples provided below, we can easily take advantage of several free APIs optimized to convert common file types to HTML. Specifically, we’ll learn how to convert Word (DOCX), Excel (XLSX), CSV, and two different email file types (EML and MSG).

To authorize our API calls, we can use a single free Cloudmersive API key (this allows a limit of 800 API calls per month), and we can run a single Composer command to install the PHP client.

On that note, let’s execute the below command from our command line to install the PHP client with Composer:

composer require cloudmersive/cloudmersive_document_convert_api_client

With that step out of the way, we can copy code to call the HTML conversion function we need. Within each function, we can replace the ‘YOUR_API_KEY’ placeholder string in the $config snippet with our own free API key to authorize our requests.

Let’s use the below code to convert our Word documents to HTML:

<?php
require_once(__DIR__ . '/vendor/autoload.php');

// Configure API key authorization: Apikey
$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');



$apiInstance = new Swagger\Client\Api\ConvertDocumentApi(


new GuzzleHttp\Client(),
$config
);
$input_file = "/path/to/inputfile"; // \SplFileObject | Input file to perform the operation on.

try {
$result = $apiInstance->convertDocumentDocxToHtml($input_file);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling ConvertDocumentApi->convertDocumentDocxToHtml: ', $e->getMessage(), PHP_EOL;
}
?>

And let’s use the following examples to convert our Excel documents to HTML:

<?php
require_once(__DIR__ . '/vendor/autoload.php');

// Configure API key authorization: Apikey
$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');



$apiInstance = new Swagger\Client\Api\ConvertDocumentApi(


new GuzzleHttp\Client(),
$config
);
$input_file = "/path/to/inputfile"; // \SplFileObject | Input file to perform the operation on.

try {
$result = $apiInstance->convertDocumentXlsxToHtml($input_file);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling ConvertDocumentApi->convertDocumentXlsxToHtml: ', $e->getMessage(), PHP_EOL;
}
?>

To convert our CSV documents to HTML, let’s use the below code:

<?php
require_once(__DIR__ . '/vendor/autoload.php');

// Configure API key authorization: Apikey
$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');



$apiInstance = new Swagger\Client\Api\ConvertDocumentApi(


new GuzzleHttp\Client(),
$config
);
$input_file = "/path/to/inputfile"; // \SplFileObject | Input file to perform the operation on.

try {
$result = $apiInstance->convertDocumentCsvToHtml($input_file);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling ConvertDocumentApi->convertDocumentCsvToHtml: ', $e->getMessage(), PHP_EOL;
}
?>

And finally, to convert our Email documents to HTML, we can use the remaining two functions respectively. Note that when we call either of these APIs, we can set optional parameters to 1) exclude all content apart from the email body and 2) elect to include attachments or not.

This will convert our EML files to HTML:

<?php
require_once(__DIR__ . '/vendor/autoload.php');

// Configure API key authorization: Apikey
$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');



$apiInstance = new Swagger\Client\Api\ConvertDocumentApi(


new GuzzleHttp\Client(),
$config
);
$input_file = "/path/to/inputfile"; // \SplFileObject | Input file to perform the operation on.
$body_only = true; // bool | Optional; If true, the HTML string will only include the body of the email. Other information such as subject will still be given as properties in the response object. Default is false.
$include_attachments = true; // bool | Optional; If false, the response object will not include any attachment files from the input file. Default is true.

try {
$result = $apiInstance->convertDocumentEmlToHtml($input_file, $body_only, $include_attachments);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling ConvertDocumentApi->convertDocumentEmlToHtml: ', $e->getMessage(), PHP_EOL;
}
?>

And this will convert our MSG files to HTML:

<?php
require_once(__DIR__ . '/vendor/autoload.php');

// Configure API key authorization: Apikey
$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');



$apiInstance = new Swagger\Client\Api\ConvertDocumentApi(


new GuzzleHttp\Client(),
$config
);
$input_file = "/path/to/inputfile"; // \SplFileObject | Input file to perform the operation on.
$body_only = true; // bool | Optional; If true, the HTML string will only include the body of the MSG. Other information such as subject will still be given as properties in the response object. Default is false.
$include_attachments = true; // bool | Optional; If false, the response object will not include any attachment files from the input file. Default is true.

try {
$result = $apiInstance->convertDocumentMsgToHtml($input_file, $body_only, $include_attachments);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling ConvertDocumentApi->convertDocumentMsgToHtml: ', $e->getMessage(), PHP_EOL;
}
?>

Each solution will return an HTML string version of our input document. The EML and MSG conversion APIs will structure the response object in a useful way for email content, as shown in the JSON response model below:

{
"Successful": true,
"Content": "string",
"Body": "string",
"From": "string",
"To": "string",
"Cc": "string",
"ReceivedTime": "string",
"Subject": "string",
"Attachments": [
{
"Name": "string",
"Content": "string"
}
]
}

And that’s all there is to it — no more code required! Now we can easily convert a few common document types to HTML using a few lines of PHP code.

--

--

Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.