Converting rich Word documents into web‑ready HTML is a frequent need for modern PHP applications. GroupDocs.Conversion Cloud SDK for PHP provides a powerful API that handles the heavy lifting in the cloud. In this tutorial we demonstrate DOCX to HTML in PHP using the SDK. You will learn how to set up the library, run a complete code example, and perform the same conversion with cURL calls.
How to Convert DOCX to HTML in PHP - Step by Step
- Install the SDK via Composer: Add the library to your project with the official Composer command.
composer require groupdocs-conversion-cloud
- Initialize the Configuration: Create a
Configurationobject and set your App SID and App Key.
$config = new GroupDocs\Conversion\Cloud\Configuration();
$config->setAppSid('YOUR_APP_SID');
$config->setAppKey('YOUR_APP_KEY');
- Configure HTML Conversion Options: Adjust settings such as image extraction and page number handling.
$htmlOptions = new GroupDocs\Conversion\Cloud\Model\HtmlConvertOptions();
$htmlOptions->setExtractImages(true);
$htmlOptions->setPreserveOriginalPageNumbers(false);
$htmlOptions->setZipOutput(false);
$htmlOptions->setShowHiddenText(false);
$htmlOptions->setRenderToSinglePage(false);
- Create Convert Settings: Define the source file, target format, and attach the HTML options.
$convertSettings = new GroupDocs\Conversion\Cloud\Model\ConvertSettings();
$convertSettings->setFilePath('sample.docx');
$convertSettings->setOutputPath('output.html');
$convertSettings->setFormat('html');
$convertSettings->setOptions($htmlOptions);
- Execute the Conversion: Call the
convertDocumentmethod and handle the result.
$convertApi = new GroupDocs\Conversion\Cloud\Api\ConvertApi($config);
try {
$result = $convertApi->convertDocument($convertSettings);
echo "Conversion succeeded. HTML file saved to: " . $result->getPath() . PHP_EOL;
} catch (Exception $e) {
echo "Conversion failed: " . $e->getMessage() . PHP_EOL;
}
For more details on each class and method, refer to the API reference.
Complete Code Example: Generate HTML from DOCX in PHP
The following example demonstrates a full end‑to‑end conversion using the SDK.
<?php
require __DIR__ . '/vendor/autoload.php';
use GroupDocs\Conversion\Cloud\Configuration;
use GroupDocs\Conversion\Cloud\Api\ConvertApi;
use GroupDocs\Conversion\Cloud\Model\ConvertSettings;
use GroupDocs\Conversion\Cloud\Model\HtmlConvertOptions;
$config = new Configuration();
$config->setAppSid('YOUR_APP_SID');
$config->setAppKey('YOUR_APP_KEY');
$convertApi = new ConvertApi($config);
$inputFilePath = 'sample.docx';
$outputFilePath = 'output.html';
$htmlOptions = new HtmlConvertOptions();
$htmlOptions->setExtractImages(true); // extract images to separate files
$htmlOptions->setPreserveOriginalPageNumbers(false);
$htmlOptions->setZipOutput(false); // generate plain HTML, not a zip archive
$htmlOptions->setShowHiddenText(false);
$htmlOptions->setRenderToSinglePage(false);
$convertSettings = new ConvertSettings();
$convertSettings->setFilePath($inputFilePath);
$convertSettings->setOutputPath($outputFilePath);
$convertSettings->setFormat('html');
$convertSettings->setOptions($htmlOptions);
try {
$result = $convertApi->convertDocument($convertSettings);
echo "Conversion succeeded. HTML file saved to: " . $result->getPath() . PHP_EOL;
} catch (Exception $e) {
echo "Conversion failed: " . $e->getMessage() . PHP_EOL;
}
Note: This code example demonstrates the core functionality. Before using it in your project, make sure to update the file paths (
sample.docx,output.html) to match your actual file locations, verify that all required dependencies are properly installed, and test thoroughly in your development environment. If you encounter any issues, please refer to the official documentation or reach out to the support team for assistance.
Converting DOCX Files to HTML Using cURL and the REST API
The SDK also offers a REST endpoint that can be called with cURL. Below is the typical workflow.
Obtain an Access Token
ReplaceYOUR_CLIENT_IDandYOUR_CLIENT_SECRETwith your credentials.curl -X POST "https://api.groupdocs.cloud/v2.0/oauth2/token" \ -H "Content-Type: application/json" \ -d '{"client_id":"YOUR_CLIENT_ID","client_secret":"YOUR_CLIENT_SECRET"}'Upload the DOCX File
Use the token from the previous step.curl -X POST "https://api.groupdocs.cloud/v2.0/conversion/storage/file/sample.docx" \ -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \ -F "file=@sample.docx"Start the Conversion
Request conversion to HTML.curl -X POST "https://api.groupdocs.cloud/v2.0/conversion/convert" \ -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \ -H "Content-Type: application/json" \ -d '{ "filePath": "sample.docx", "outputPath": "output.html", "format": "html", "options": { "extractImages": true, "zipOutput": false } }'Download the Resulting HTML
curl -X GET "https://api.groupdocs.cloud/v2.0/conversion/storage/file/output.html" \ -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \ -o output.html
For a complete list of parameters, see the official API documentation.
Installing and Configuring GroupDocs.Conversion Cloud SDK for PHP
composer require groupdocs-conversion-cloud
Download the latest package from the release page. The SDK requires PHP 7.4 or later and an active GroupDocs Cloud account with valid credentials.
require __DIR__ . '/vendor/autoload.php';
use GroupDocs\Conversion\Cloud\Configuration;
$config = new Configuration();
$config->setAppSid('YOUR_APP_SID'); // obtain from your GroupDocs Cloud dashboard
$config->setAppKey('YOUR_APP_KEY');
GroupDocs.Conversion Cloud SDK for PHP Capabilities for DOCX to HTML
- Image Extraction -
extractImageslets you pull embedded pictures into separate files, preserving layout fidelity. - Page Number Control -
preserveOriginalPageNumberscan be disabled to produce a continuous HTML flow. - Plain HTML Output - Setting
zipOutputto false generates a single HTML file instead of a ZIP archive, simplifying downstream processing. - Hidden Text Handling -
showHiddenTextdetermines whether hidden content appears in the final HTML. - Single‑Page Rendering -
renderToSinglePagecan combine all pages into one HTML document for easier embedding.
All features are described in the documentation.
Conclusion
Converting DOCX to HTML in PHP becomes straightforward with the GroupDocs.Conversion Cloud SDK for PHP. The library handles complex formatting, image extraction, and page management without requiring Microsoft Office on the server. After following the steps above, you can integrate DOCX to HTML conversion into any PHP‑based workflow, whether you prefer native SDK calls or RESTful cURL requests. Remember to acquire a proper license for production use; pricing details are available on the temporary license page. Start converting today and deliver rich, web‑ready content to your users.
FAQs
How do I implement DOCX to HTML in PHP without installing additional software?
The cloud‑based SDK performs all processing on GroupDocs servers, so you only need the PHP library and your credentials.Can I convert multiple DOCX files in a single request?
The API supports batch conversion by looping over files in your code; each call returns its own HTML result.What happens to images inside the DOCX file?
WithextractImagesenabled, images are saved as separate files and referenced in the generated HTML, allowing you to serve them directly.Is there a way to customize the generated HTML layout?
You can post‑process the HTML output or adjust conversion options such asrenderToSinglePageto influence the structure.
