---
title: "Process documents with Document Engine and PHP"
canonical_url: "https://www.nutrient.io/sdk/document-engine/getting-started/php/"
md_url: "https://www.nutrient.io/sdk/document-engine/getting-started/php.md"
last_updated: "2026-06-08T16:38:57.357Z"
description: "Master PDF document processing with Document Engine. Follow our guide for step-by-step instructions on how to process and merge PDF documents using HTTP API and PHP."
---

# Process documents with Document Engine and PHP

This guide walks you through the steps necessary to start Document Engine. It also shows you how to use it to process documents. By the end, you’ll be able to merge two PDF documents into one using Document Engine’s HTTP API from PHP.

## Requirements

Document Engine is compatible with a range of platforms. Below is the list of supported operating systems.

- **macOS**:
  - Ventura
  - Monterey
  - Mojave
  - Catalina
  - Big Sur

- **Linux**:
  - Ubuntu, Fedora, Debian, and CentOS
  - Ubuntu and Debian derivatives (such as Kubuntu, Xubuntu) are also supported

**Processor requirements**:

- 64-bit Intel (x86_64) processors

- ARM (AArch64) processors

**Minimum system requirements**:

- At least 4GB of RAM, regardless of the operating system

## Installing Docker

Document Engine is distributed as a Docker container. To run it on your computer, you need to install a Docker runtime distribution for your operating system.

### macOS

Install and start Docker Desktop for Mac. For detailed instructions, refer to the [Docker website](https://docs.docker.com/docker-for-mac/install/).

### Windows

Install and start Docker Desktop for Windows. For detailed instructions, refer to the [Docker website](https://docs.docker.com/docker-for-windows/install/).

> Document Engine runs as a Linux container. If you’re using Docker Desktop for Windows, ensure it’s configured to work with Linux containers. For detailed steps, refer to the **How do I switch between Windows and Linux containers?** section in the [Docker documentation](https://docs.docker.com/desktop/setup/install/windows-install/). Users with Docker already set up might need to switch from Windows containers to Linux containers for compatibility.

### Linux

Install and start Docker Engine. For detailed instructions on how to install Docker Engine for your Linux distribution, refer to the [Docker website](https://docs.docker.com/engine/install/#server).

Once you finish installing Docker Engine, follow the [instructions](https://docs.docker.com/compose/install/#install-compose-on-linux-systems) to install Docker Compose.








## Starting Document Engine

To start Document Engine, follow the steps below.

1. Open your terminal emulator.

   ### macOS

   Use the terminal emulator integrated with your code editor or IDE. Alternatively, you can use `Terminal.app` or [iTerm2](https://iterm2.com/).

   ### Windows

   Use your code editor’s integrated terminal or [PowerShell](https://learn.microsoft.com/en-us/powershell/scripting/windows-powershell/starting-windows-powershell?view=powershell-7.5).

   ### Linux

   Use the terminal emulator integrated with your code editor or IDE, or the one bundled with your desktop environment.

2. Run the following command to start the Document Engine container:

```sh

docker run --rm -t -p 5000:5000 -e API_AUTH_TOKEN=secret pspdfkit/document-engine:1.16.0

```

This command may take some time to complete depending on your internet connection speed, as it needs to pull the Docker image. You’ll know that Document Engine is successfully running when you see a message similar to the following in your terminal:

```

[info]  2024-02-05 18:56:45.286  Running Document Engine version 1.16.0

```

Document Engine is now up and running!




## Installing PHP

The interaction with Document Engine happens through its HTTP API — You send documents and commands in the request and receive the resulting file in the response. To do this, you’ll invoke the API from the PHP script. But first, you need to install PHP for your operating system:

### macOS

The most direct way to install PHP on macOS is using Homebrew. Follow the instructions on the [Homebrew website](https://brew.sh) to install it. Then, to install PHP, run:

```sh

brew install php@7.4 && brew link php@7.4

```

Verify the installation by running the following command in the terminal:

```

php --version

```

The output should start with `PHP 7.4` — you can ignore the rest of the message.

> If the output doesn’t match the above, try restarting your terminal app by typing `exit` and opening it again.

### Windows

1. Download the PHP ZIP archive from the [PHP website](https://windows.php.net/download/#php-7.4-ts-vc15-x86) (pick the x86 Thread Safe build of the 7.4 release).

2. Create a folder anywhere on your C: drive.

3. Extract the ZIP archive into the folder you just created.

4. Open the terminal and switch to that folder:

```powershell

cd C:\path\to\directory

```

Now run the `.\php.exe --version` command. The output should start with `PHP 7.4` — you can ignore the rest of the message.

To proceed, you’ll also need to create a PHP configuration file to enable a specific extension. So in the same directory, create a `php.ini` file with the following content:

```php.ini

[PHP]
extension=curl

```

Save the file, as you’ll need it shortly.

### Linux

You can install PHP using your distribution’s package manager:

```sh

apt-get update && apt-get install -y php

```

### Fedora/Centos

```sh

dnf install -y php

```

Now run the `php --version` command. The output should start with `PHP 7.4` — you can ignore the rest of the message.

## Handling file uploads

In this example project, the PDF files you’ll merge will be uploaded through a simple webpage via a standard HTML form. Create a file called `index.php` with the following content:

```index.php

<!DOCTYPE html>
<html>
<head>
    <title>Merge PDFs with Document Engine</title>
</head>
<body>
    <p>Upload the files to merge:</p>
    <form enctype="multipart/form-data" action="merge.php" method="post">
        <div>File 1: <input name="file1" type="file" accept="application/pdf"></div>
        <div>File 2: <input name="file2" type="file" accept="application/pdf"></div>
        <input type="submit" value= "Merge PDFs">
    </form>
</body>
</html>

```

Now open the terminal and type the following command in the same directory where you created the `index.php` file:

### macOS

```sh

php -S localhost:8000

```

### Windows

```powershell.\php.exe -c php.ini -S localhost:8000

```

### Linux

```sh

php -S localhost:8000

```

Go to `http://localhost:8000` in the browser. You should see a webpage similar to this:

When you choose files and click the Merge PDFs button, you’ll receive an error. This is because you haven’t yet written any code to handle the form submission.

Create a `merge.php` file in the same directory and add the following content to it:

```merge.php

<?php
$file1 = $_FILES['file1'];
$file2 = $_FILES['file2'];

echo $file1['name'], ", ", $file2['name'];?>

```

Now when you go back to `http://localhost:8000`, choose the files, and submit the form, you should see the names of the files you picked printed on the screen:

## Merging PDFs

You can now use Document Engine’s API to merge the files uploaded from the browser. Replace the contents of the `merge.php` file with:

```merge.php

<?php
$file1 = $_FILES["file1"];
$file2 = $_FILES["file2"];

$headers = [
    "Content-Type" => "multipart/form-data",
    "Authorization" => "Token token=secret"
];
$postFields = [];
$postFields["document1"] = curl_file_create(
    $file1["tmp_name"],
    $file1["type"],
    $file1["name"]
);

$postFields["document2"] = curl_file_create(
    $file2["tmp_name"],
    $file2["type"],
    $file2["name"]
);

$postFields["instructions"] = json_encode([
    "parts" => [
        [
            "file" => "document1"
        ],
        [
            "file" => "document2"
        ]
    ],
]);

$request = curl_init();
curl_setopt($request, CURLOPT_URL, "http://localhost:5000/api/build");
curl_setopt($request, CURLOPT_HTTPHEADER, $headers);
curl_setopt($request, CURLOPT_POST, true);
curl_setopt($request, CURLOPT_POSTFIELDS, $postFields);
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($request);

$status = curl_getinfo($request, CURLINFO_RESPONSE_CODE);
$file_size = curl_getinfo($request, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
curl_close($request);

if ($status!= 200) {
    echo "Request to Document Engine failed with status code ".
        $status.
        ': "'.
        $response.
        '".';
} else {
    header("Content-type: application/pdf");
    header('Content-Disposition: attachment; filename="result.pdf"');
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: ". $file_size);
    header("Accept-Ranges: bytes");
    echo $response;
}

```

Most of this code, up until the `curl_exec` function, constructs a request that’s sent to Document Engine. It includes two files — `document1` and `document2` — and a list of `instructions` for Document Engine. By default, Document Engine’s output for the `/api/build` endpoint is the result of merging all documents or parts of the `instructions`. To learn more about the `/api/build` instructions, go to Document Engine’s [API Reference](https://www.nutrient.io/api/reference/document-engine/upstream/#tag/Build-API).

The rest of the code deals with error handling, and if everything goes well, it returns the resulting file back to the browser.

You can check how it works in practice yourself! Go to `http://localhost:8000`, pick any two PDFs on your disk (or use [file1.pdf](https://www.nutrient.io/assets/nutrient-media/files/cover.pdf) and [file2.pdf](https://www.nutrient.io/assets/nutrient-media/files/document.pdf) if you don’t have any), and click **Merge PDFs** again. Depending on your browser, it will either automatically download the file for you or ask you for permission to download. In any case, look for the `result.pdf` file in the downloads folder on your computer. Open that file in any PDF viewer application. If you used the two files from the links above, you should see a five-page PDF document like what’s shown below.

That’s it! Now you know how to use Document Engine’s API to perform operations on documents with PHP. To learn more about the various operations you can perform on documents with Document Engine, visit Document Engine’s [API Reference](https://www.nutrient.io/api/reference/document-engine/upstream/)
---

## Related pages

- [Getting started with Document Engine](/sdk/document-engine/getting-started.md)
- [Process documents with Document Engine and curl](/sdk/document-engine/getting-started/curl.md)
- [Process documents with Document Engine and Rust](/sdk/document-engine/getting-started/rust.md)
- [Process documents with Document Engine and Golang](/sdk/document-engine/getting-started/golang.md)
- [Document Engine with Docker](/sdk/document-engine/getting-started/docker-deployment-react-frontend.md)
- [Process documents with Document Engine and Python](/sdk/document-engine/getting-started/python.md)
- [Document Engine with Docker and EJS templates](/sdk/document-engine/getting-started/docker-deployment-ejs-templates.md)

