How to Convert Word DOCX to PDF in PHP with a Free API

Cloudmersive
2 min readMay 6, 2024

Converting Word documents to PDF is an extremely common operation, but writing code to make that conversion at scale can be challenging.

Thankfully, we can easily overcome this challenge by incorporating a free Word to PDF conversion API into our document processing workflow.

We can quickly structure our API call with ready-to-run PHP code examples (provided below), and we can then authorize our API calls using a free Cloudmersive API key (this allows 800 API calls per month with no commitments).

To structure our API call, we can start by installing the PHP client. Let’s use the below command to install with Composer:

composer require cloudmersive/cloudmersive_document_convert_api_client

Now let’s copy the below code to call the function. Our free API key goes in the $config snippet:

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

That’s all there is to it! Now we can easily make reliable Word to PDF conversions in PHP using just a few lines of code.

--

--

Cloudmersive

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