How to Convert PDF to Word with PHP

Share this page

Why Convert PDF to Word?

The pdfRest PDF to Word API Tool is a powerful resource that allows users to convert PDF documents into editable Word files. This tutorial will guide you through the process of sending an API call to the PDF to Word API using PHP.

A common example of using this API is for businesses that need to convert a batch of contract PDFs into Word documents for editing and review.

PHP Code Sample

 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
];

$options = [
  'multipart' => [
    [
      'name' => 'file',
      'contents' => Utils::tryFopen('/path/to/file', 'r'),
      'filename' => '/path/to/file',
      'headers' => [
        'Content-Type' => ''
      ]
    ],
    [
      'name' => 'output',
      'contents' => 'pdfrest_word'
    ]
  ]
];

$request = new Request('POST', 'https://api.pdfrest.com/word', $headers);

$res = $client->sendAsync($request, $options)->wait();

echo $res->getBody();
?>

Source: GitHub - datalogics/pdf-rest-api-samples

Breaking Down the PHP

The code starts by including the necessary libraries for sending HTTP requests with the Guzzle HTTP client. It then sets up the API key in the headers for authentication with the pdfRest API.

$headers = [
  'Api-Key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
];

The options array is configured to send a multipart request, which includes the file to be converted and the desired output format.

'options' => [
  'multipart' => [
    [
      'name' => 'file',
      'contents' => Utils::tryFopen('/path/to/file', 'r'),
      'filename' => '/path/to/file',
      'headers' => [
        'Content-Type' => ''
      ]
    ],
    [
      'name' => 'output',
      'contents' => 'pdfrest_word'
    ]
  ]
];

The 'file' part of the request contains the file stream opened for reading, the path to the file, and the content type of the file. The 'output' part specifies the output format, which is 'pdfrest_word' for a Word document.

A new POST request is created with the specified endpoint and headers, and the request is sent asynchronously. The response is then retrieved and the body of the response, which contains the converted Word document, is outputted.

$request = new Request('POST', 'https://api.pdfrest.com/word', $headers);
$res = $client->sendAsync($request, $options)->wait();
echo $res->getBody();

Next Steps with pdfRest

In this tutorial, we've walked through the process of setting up and making an API call to convert a PDF to a Word document using the pdfRest API and PHP. By following these steps, you can integrate PDF to Word conversion into your PHP applications.

To explore and demo all of the pdfRest API Tools, visit the API Lab. For more detailed information, refer to the API Reference documentation.

Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at GitHub - JSON Payload Example.

Generate a self-service API Key now!

Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.

Compare Plans
Contact Us