How to Convert HTML to DOCX in PHP

Cloudmersive
2 min readJul 22, 2021

--

While HTML documents may be ideal for the web, you may want to convert them to the more simple Word document format for other purposes. In this brief tutorial, we are going to demonstrate a way that you can fully transfer the elements of an HTML document to a DOCX file — and before you get worried about how time-consuming the process may be, let me stop you. The following simple API solution can be used in PHP to automatically convert the HTML document to a Word document without any additional coding needed from you.

To begin, we will run this command to install the SDK:

composer require cloudmersive/cloudmersive_document_convert_api_client

Now we are ready to call the conversion function by inputting the HTML document and API key into the below example 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\ConvertWebApi(


new GuzzleHttp\Client(),
$config
);
$input_request = new \Swagger\Client\Model\HtmlToOfficeRequest(); // \Swagger\Client\Model\HtmlToOfficeRequest | HTML input to convert to DOCX
try {
$result = $apiInstance->convertWebHtmlToDocx($input_request);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling ConvertWebApi->convertWebHtmlToDocx: ', $e->getMessage(), PHP_EOL;
}
?>

And there you have it — your new DOCX file will be ready to go at the end of the process. To retrieve your API key, head over to the Cloudmersive website to register for a free account; this provides 800 calls/month across any of our APIs.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet