How to Flatten Annotations in PDF Files with PHP

Learn how to use PHP to flatten annotations in PDF files by calling Flatten Annotations AI Tool by pdfRest.
Share this page

Why Use Flatten Annotations with PHP?

The pdfRest Flatten Annotations API Tool allows developers to programmatically flatten annotations in a PDF document. Flattening annotations means incorporating the annotations directly into the PDF content so they can no longer be modified or removed. This is useful in scenarios where you want to finalize comments, marks, or edits on a PDF document, such as when preparing a legal document for submission or an assignment for grading.

This tutorial will guide you through the process of sending an API call to the Flatten Annotations API using PHP. This is particularly useful for PHP developers who need to automate document processing within their applications.

Flatten Annotations with PHP Code Example

 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' // Set the API key in the headers for authentication.

$options = [
  'multipart' => [
      'name' => 'file', // Specify the field name for the file.
      'contents' => Utils::tryFopen('/path/to/file', 'r'), // Open the file specified by the '/path/to/file' for reading.
      'filename' => '/path/to/file', // Set the filename for the file to be processed, in this case, '/path/to/file'.
      'headers' => [
        'Content-Type' => '' // Set the Content-Type header for the file.
      'name' => 'output', // Specify the field name for the output option.
      'contents' => 'pdfrest_flattened_pdf' // Set the value for the output option (in this case, 'pdfrest_flattened_pdf').

$request = new Request('POST', '', $headers); // Create a new HTTP POST request with the API endpoint and headers.

$res = $client->sendAsync($request, $options)->wait(); // Send the asynchronous request and wait for the response.

echo $res->getBody(); // Output the response body, which contains the flattened annotations PDF content.

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

Breaking Down the Code

In the provided PHP code, we start by including the necessary dependencies and importing the classes for the Guzzle HTTP client and PSR-7 Request and Utils.

The $headers array contains the API key required for authentication with the pdfRest API. Replace 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' with your actual API key.

The $options array is configured for a multipart request. The 'file' part includes the actual PDF file that you want to flatten annotations for, and 'output' specifies the output option. Make sure to replace '/path/to/file' with the path to your PDF file and set the correct 'Content-Type' header for your file.

The $request object is created as a POST request to the '' endpoint with the necessary headers. The $client->sendAsync method sends the request and waits for the response, which is then outputted with echo $res->getBody().

Beyond the Tutorial

By following the steps above, you have successfully called the Flatten Annotations API using PHP to flatten annotations in a PDF document. You can now integrate this functionality into your PHP applications to automate the process of finalizing PDF documents.

To further explore and demo all of the pdfRest API Tools, visit the API Lab at For detailed API documentation, refer to

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.

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