How to Convert PDF to Word in PHP

Cloudmersive
2 min readMar 6, 2024

--

With only a few lines of PHP code, we can make PDF to Word conversions a breeze.

Using the below code examples, we can take advantage of a free API to convert our PDF documents to Word (DOCX) format with extremely high fidelity. All we need is a free API key, and we can make this conversion up to 800 times per month with no commitments.

Our first step is to install the SDK. We can install using Composer by executing the following command from the command line:

composer require cloudmersive/cloudmersive_document_convert_api_client

After that, we can copy the remaining code into our file to call the function. We can copy our API key where indicated in the $config snippet, and we can then provide our PDF file path in the indicated request variable:

<?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->convertDocumentPdfToDocx($input_file);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling ConvertDocumentApi->convertDocumentPdfToDocx: ', $e->getMessage(), PHP_EOL;
}
?>

That’s all there is to it — now we can easily make PDF to Word conversions with minimal code.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet