PDF to DOCX document conversion in PHP is a frequent requirement when you need editable Word files from read‑only PDFs. The GroupDocs.Conversion Cloud SDK for PHP provides a robust library that handles this task with high accuracy. In this tutorial you will set up the library, walk through a detailed code example, and see how to call the REST API with cURL for cloud deployment. By the end you’ll be ready to integrate PDF to DOCX conversion into your own PHP applications.
Before You Begin: Prerequisites and Installation
To follow this guide you need:
- PHP 7.4 or higher installed on your development machine or server.
- Composer for dependency management.
- A GroupDocs Cloud account (client ID and client secret) - you can create one on the GroupDocs portal.
- Sufficient disk space for the source PDF and the resulting DOCX file.
Install the library with Composer:
composer require groupdocs-conversion-cloud
Download the latest package or view the source on GitHub: Download URL. After installation, you are ready to start coding.
PDF to DOCX Document Conversion in PHP - Building It Step by Step
Below is a concise walkthrough that shows each essential operation. The full source code appears later in the Complete PHP Implementation for Converting Documents to DOCX section.
Step 1: Load the Source PDF
Create a ConversionApi instance and point it to the PDF you want to convert.
use GroupDocs\Conversion\Configuration;
use GroupDocs\Conversion\Api\ConversionApi;
$config = new Configuration('YOUR_CLIENT_ID', 'YOUR_CLIENT_SECRET');
$api = new ConversionApi($config);
$sourcePath = 'input.pdf';
Step 2: Initialize Conversion Settings
Define the output format and destination path. The ConvertOptions class lets you fine‑tune memory limits for large PDFs, which improves PDF to DOCX Conversion Performance in PHP.
use GroupDocs\Conversion\Model\ConvertOptions;
$options = new ConvertOptions();
$options->setFilePath($sourcePath);
$options->setOutputPath('output.docx');
$options->setFormat('docx');
// Optional: $options->setMemoryLimit(1024); // in MB
Step 3: Execute the Conversion
Send the request to the cloud service. This single API call performs the heavy lifting.
use GroupDocs\Conversion\Model\Requests\ConvertDocumentRequest;
$request = new ConvertDocumentRequest($options);
$api->convertDocument($request);
Step 4: Retrieve the Result
The converted DOCX file is saved to the path you specified. You can now serve it to the user or store it for later processing.
echo "Conversion completed. DOCX saved to output.docx";
Step 5: Clean Up Resources
Dispose of the API instance if you are running many conversions in a loop. Reusing the same instance is more efficient for a PDF to DOCX conversion microservice in PHP.
unset($api);
Complete PHP Implementation for Converting Documents to DOCX
The following code puts all the steps together into a single, runnable script.
<?php
require 'vendor/autoload.php';
use GroupDocs\Conversion\Configuration;
use GroupDocs\Conversion\Api\ConversionApi;
use GroupDocs\Conversion\Model\ConvertOptions;
use GroupDocs\Conversion\Model\Requests\ConvertDocumentRequest;
// ==== Configuration ====
$clientId = 'YOUR_CLIENT_ID';
$clientSecret = 'YOUR_CLIENT_SECRET';
$config = new Configuration($clientId, $clientSecret);
$api = new ConversionApi($config);
// ==== Input / Output ====
$sourcePath = __DIR__ . '/input.pdf';
$outputPath = __DIR__ . '/output.docx';
// ==== Conversion Options ====
$options = new ConvertOptions();
$options->setFilePath($sourcePath);
$options->setOutputPath($outputPath);
$options->setFormat('docx');
// Example of performance tuning
$options->setMemoryLimit(1024); // 1 GB
// ==== Convert ====
$request = new ConvertDocumentRequest($options);
try {
$api->convertDocument($request);
echo "PDF successfully converted to DOCX. File saved at: $outputPath\n";
} catch (Exception $e) {
echo "Conversion failed: " . $e->getMessage() . "\n";
}
// ==== Cleanup ====
unset($api);
?>
Note: This code example demonstrates the core functionality. Before using it in your project, make sure to update the file paths (
input.pdf,output.docx, etc.) 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 forum for assistance.
Executing Document Conversion via REST API Using cURL
If you prefer to interact with the service directly, the following cURL commands illustrate the full workflow.
1. Authenticate and Get Access Token
curl -X POST "https://api.groupdocs.cloud/v2.0/oauth2/token" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "grant_type=client_credentials&client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET"
2. Upload the Source PDF
curl -X POST "https://api.groupdocs.cloud/v2.0/storage/file/upload?path=/input.pdf" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-F "file=@/path/to/input.pdf"
3. Request PDF to DOCX Conversion
curl -X POST "https://api.groupdocs.cloud/v2.0/conversion/convert?outputFormat=docx&outputPath=/output.docx" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{"filePath":"/input.pdf"}'
4. Download the Converted DOCX
curl -X GET "https://api.groupdocs.cloud/v2.0/storage/file/download?path=/output.docx" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-o output.docx
These commands can be wrapped in a simple PHP script or used in CI pipelines for a PDF to DOCX conversion CLI tool in PHP. For more details, see the API reference.
Conclusion
Integrating PDF to DOCX conversion in PHP is straightforward with the GroupDocs.Conversion Cloud SDK for PHP. You have learned how to set up the library, run a step‑by‑step conversion, and invoke the same functionality via REST calls. The library supports both cloud‑based deployment and on‑premise scenarios, giving you flexibility for microservice architectures or traditional server installations. Remember to review the pricing options on the product page and obtain a temporary license from the temporary license page before moving to production. Happy coding!
FAQs
How do I implement PDF to DOCX document conversion in PHP on‑premise?
Deploy the library on your own server, configure the client ID and secret, and run the conversion code locally. No external network traffic is required after authentication.
What is the best way to improve conversion speed for large PDFs?
Reuse a single ConversionApi instance, increase PHP’s memory limit, and enable streaming by setting appropriate options in ConvertOptions. This reduces overhead and boosts PDF to DOCX Conversion Performance in PHP.
Can I use the library in a Docker container for a microservice?
Yes. Include the Composer installation step in your Dockerfile, copy your PHP script, and expose an endpoint that calls the conversion logic. This creates a portable PDF to DOCX conversion microservice in PHP.
Is there a Composer package for this functionality?
The library is distributed via Composer. Install it with composer require groupdocs-conversion-cloud and manage updates through your composer.json.
