How to Flatten PDF Layers with PHP

Flatten optional-content layers in PDF files with PHP so recipients and print systems receive one consistent rendering.
Share this page

In this tutorial, we'll use PHP and the Flatten Layers API Tool to flatten PDF layers. The complete Flatten PDF Layers repository sample stays visible while we explain the request fields and response behavior a PHP application must adapt safely.

Why Flatten PDF Layers with PHP?

Layered PDFs can show different optional content depending on viewer controls and saved visibility settings. Flattening incorporates the visible layer content into an ordinary PDF presentation that no longer depends on those controls.

A PHP-based production portal might receive engineering drawings with dimensions, revision notes, and manufacturing details stored in separate layers. Before sending the approved view to a supplier, the portal can flatten it so the same details appear in browser previews and print output.

The operation creates a new PDF rather than altering the uploaded source. Because alternate layer views cannot be recovered from the flattened result, keep the layered original whenever later editing or another visibility state may be required.

PHP Code Example

require 'vendor/autoload.php'; // Require the autoload file to load Guzzle HTTP client.

use GuzzleHttp\Client; // Import the Guzzle HTTP client namespace.
use GuzzleHttp\Psr7\Request; // Import the PSR-7 Request class.
use GuzzleHttp\Psr7\Utils; // Import the PSR-7 Utils class for working with streams.

// By default, we use the US-based API service. This is the primary endpoint for global use.
$apiUrl = "https://api.pdfrest.com";

/* For GDPR compliance and enhanced performance for European users, you can switch to the EU-based service by uncommenting the URL below.
 * For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work
 */
//$apiUrl = "https://eu-api.pdfrest.com";

$client = new Client(); // Create a new instance of the Guzzle HTTP client.

$headers = [
  'Api-Key' => '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', $apiUrl.'/flattened-layers-pdf', $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 layers PDF content.

Source for Flatten PDF Layers: View the PHP sample on GitHub.

Breaking Down the Code

Load Guzzle and its stream helpers

require 'vendor/autoload.php'; // Require the autoload file to load Guzzle HTTP client.

use GuzzleHttp\Client; // Import the Guzzle HTTP client namespace.
use GuzzleHttp\Psr7\Request; // Import the PSR-7 Request class.
use GuzzleHttp\Psr7\Utils; // Import the PSR-7 Utils class for working with streams.

Guzzle supplies the HTTP client and PSR-7 request objects used throughout the Flatten PDF Layers example. For this PHP Flatten PDF Layers flow, Utils::tryFopen creates readable multipart streams without loading every uploaded byte into one string.

Select the pdfRest service region

// By default, we use the US-based API service. This is the primary endpoint for global use.
$apiUrl = "https://api.pdfrest.com";

/* For GDPR compliance and enhanced performance for European users, you can switch to the EU-based service by uncommenting the URL below.
 * For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work
 */
//$apiUrl = "https://eu-api.pdfrest.com";

$client = new Client(); // Create a new instance of the Guzzle HTTP client.

The active $apiUrl selects the US service for Flatten PDF Layers; the commented assignment shows the EU alternative. Keep the PHP uploads, request IDs, and generated resources for Flatten PDF Layers on that same regional host.

Build the flattening multipart body

$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', $apiUrl.'/flattened-layers-pdf', $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 layers PDF content.

This block constructs the operation-specific input for Flatten PDF Layers. Its file parts carry binary content, while the named Flatten PDF Layers fields describe pdfRest behavior rather than syntax supplied by PHP.

Send the flatten-layers request

$request = new Request('POST', $apiUrl.'/flattened-layers-pdf', $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 layers PDF content.

The PHP request joins the Flatten PDF Layers endpoint, authentication header, and prepared body. For this Flatten PDF Layers call, the multipart metadata generated by PHP must remain paired with the body so pdfRest can separate files from options.

Beyond the Tutorial

You now have a PHP pattern for turning the approved visible layer state into a stable PDF that no longer depends on optional-content controls.

Compare the generated PDF with the approved layer state before distribution. Drawings, maps, and production artwork deserve a visual check because flattening faithfully fixes the current appearance, including any layer that was unintentionally visible or hidden.

Try the Flatten PDF Layers workflow with a representative file in API Lab, then adapt the request in PHP. The Flatten Layers API Tool documentation provides the complete PHP context for Flatten PDF Layers values, defaults, and limitations.

Note: This PHP Flatten PDF Layers tutorial uses a multipart file upload. For Flatten PDF Layers content already stored in pdfRest, the PHP JSON payload example supplies a managed resource ID instead of uploading the file again.

Generate a self-service API Key now!
Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.