How to convert Word DOCX to PDF in PHP

The first step for us is to add the library to our composer.json:

"require": {
"cloudmersive/cloudmersive_validate_api_client": "^1.4",

Or we can run this command:

composer require cloudmersive/cloudmersive_validate_api_client

Now, all we need to do is call the convertDocumentDocxToPdf PHP function to convert the input Word Document (DOCX) file into a PDF file:

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(),
$input_file = "/path/to/file.docx"; // \SplFileObject | Input file to perform the operation on.
try {
$result = $apiInstance->convertDocumentDocxToPdf($input_file);
} catch (Exception $e) {
echo 'Exception when calling ConvertDocumentApi->convertDocumentDocxToPdf: ', $e->getMessage(), PHP_EOL;

Note that here we will want to replace file.docx with the path to the file we would like to convert.

That’s it — now we are done. The result includes the result converted bytes from converting the Word Document into PDF.

This same library also includes functions for converting other popular Office file formats such as Excel XLSX, XLS, PPTX and PPT. There is also an Auto-detect method which will automatically detect the input document type and convert it to PDF. Other functions in the library can also edit the DOCX, including adding headers, footers, tables, images, replacing text, and generating DOCX files from templates with parameterized fields.

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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store