How to Convert Microsoft Word to PDF with PHP

Learn how to convert Word docs to PDF files with pdfRest Convert to PDF API Tool using PHP
Share this page

Why Convert Word to PDF with PHP?

The pdfRest Convert to PDF API Tool provides a straightforward way to convert various file formats into PDFs using a simple API call. This tutorial will demonstrate how to send an API call to the pdfRest Convert to PDF endpoint using PHP, a popular server-side scripting language. By integrating this functionality into PHP applications, developers can automate the conversion process, making it efficient and scalable.

You might need to convert documents to PDF to ensure compatibility across different platforms and devices. For instance, businesses often need to convert invoices, reports, or contracts to PDF format before sharing them with clients or storing them for record-keeping. The Convert to PDF API allows for these conversions to be done programmatically, saving time and reducing the risk of human error.

Convert Word to PDF with PHP Code Example

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

$options = [
  'multipart' => [
    [
      'name' => 'file',
      'contents' => Utils::tryFopen('/path/to/file.docx', 'r'),
      'filename' => '/path/to/file.docx',
      'headers' => [
        'Content-Type' => ''
      ]
    ],
    [
      'name' => 'compression',
      'contents' => 'lossless'
    ],
    [
      'name' => 'downsample',
      'contents' => '300'
    ],
    [
      'name' => 'tagged_pdf',
      'contents' => 'off'
    ],
    [
      'name' => 'output',
      'contents' => 'pdfrest_pdf'
    ]
  ]
];

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

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

echo $res->getBody();
?>

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

Breaking Down the Code

This PHP script uses the Guzzle HTTP client library to make an HTTP POST request to the pdfRest Convert to PDF API endpoint. Here's a breakdown of the key components:

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

This array contains the headers for the HTTP request, including the 'Api-Key' which is used for authentication with the pdfRest API.

$options = [
  'multipart' => [
    // ... multipart form data ...
  ]
];

The $options array is used to define the multipart form data that will be sent with the request. Each element of the 'multipart' array represents a different part of the form data:

  • 'file': The file to be converted to PDF. The 'contents' key should point to the file stream, and the 'filename' key should specify the file path.
  • 'compression': The compression setting for the PDF.
  • 'downsample': The downsample resolution for images within the PDF.
  • 'tagged_pdf': Whether to create a tagged PDF, which is important for accessibility.
  • 'output': The output file name prefix for the converted PDF.
$request = new Request('POST', 'https://api.pdfrest.com/pdf', $headers);

A new HTTP POST request is created with the API endpoint URL and the headers.

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

The request is sent asynchronously, and the script waits for the response. The response body, which should contain the processed PDF content, is then printed out.

Beyond the Tutorial

This tutorial has shown how to use PHP to call the pdfRest Convert to PDF API. By sending a multipart request with the necessary parameters, you can convert files to PDF programmatically. To explore and test all of the pdfRest API Tools, visit the API Lab. For more detailed information, refer to the API Reference Guide.

Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at GitHub - datalogics/pdf-rest-api-samples (JSON Payload).

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