How to Flatten PDF Transparencies with PHP

Learn how to flatten PDF transparencies with the Flatten Transparencies API Tool by pdfRest using PHP
Share this page

Why Use Flatten Transparencies with PHP?

The pdfRest Flatten Transparencies API Tool is a powerful resource for developers who need to process PDFs to ensure compatibility across various viewers and printers. This tool allows you to flatten transparencies within a PDF, which can be crucial for eliminating rendering issues and ensuring that the document appears the same across different platforms. This tutorial will guide you through the process of making an API call to the Flatten Transparencies endpoint using PHP.

Consider a scenario where you're a print shop owner and receive a PDF with complex graphics for printing. These graphics might contain transparencies that can cause issues with certain printing equipment or result in unexpected visual artifacts. By using the Flatten Transparencies API, you can programmatically prepare the PDF for printing by flattening these transparencies, ensuring that the printed output matches the intended design.

Flatten Transparencies with PHP Code Example

require 'vendor/autoload.php';

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Utils;

$client = new Client();

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

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

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

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

echo $res->getBody();

Source of the provided code: GitHub - datalogics/pdf-rest-api-samples

Breaking Down the Code

The provided code snippet shows how to use the Guzzle HTTP client in PHP to make a multipart/form-data POST request to the pdfRest Flatten Transparencies API.

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

This section sets up the headers for the HTTP request, including the 'Api-Key' which is required for authentication with the pdfRest API.

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

The $options array contains the 'multipart' key, which defines the different parts of the multipart request. Each part is an associative array with keys like 'name', 'contents', 'filename', and 'headers' that specify the details of the file to be uploaded, the quality setting, and the output filename.

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

This line creates a new HTTP POST request object with the API endpoint URL and the headers array.

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

The Guzzle HTTP client sends the asynchronous request with the provided options and waits for the response.

echo $res->getBody();

Finally, the response body is printed out. This body will contain the content of the flattened transparencies PDF.

Beyond the Tutorial

By following the steps outlined above, you have learned how to make an API call to the pdfRest Flatten Transparencies API using PHP. This allows you to programmatically process PDFs to flatten transparencies, which is especially useful for ensuring consistent printing and viewing experiences.

To further explore the capabilities of the pdfRest API, consider trying out all of the API Tools in the API Lab. For a deeper understanding of the API's functionality and additional endpoints, 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.

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