How to Convert a Word DOCX Document to HTML using PHP

Cloudmersive
2 min readFeb 13, 2023

--

An HTML version of a DOCX Document can be easily viewed in any web browser, making it a useful lightweight alternative for those who don’t own the Word editing application itself.

Using the complementary PHP code examples provided below in this article, you can easily implement a DOCX to HTML conversion API into your application with zero hassle. Your request will return an HTML string which can be easily saved as an .html text file and launched within any browser.

To take advantage of this API, first run this command to install the SDK:

composer require cloudmersive/cloudmersive_document_convert_api_client

After that, we can copy the final code example to call the function:

<?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;
}
?>

You’ll need to pass a valid API key with your request to authorize access, and you can get one for free by creating a free account on our website (free accounts yield a limit of 800 API calls per month). After that, you’re all done!

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet