How to Convert PDF Colors with PHP

Learn how to change PDF color profiles with pdfRest Convert PDF Colors API tool using PHP
Share this page

Why Convert PDF Colors with PHP?

The pdfRest Convert PDF Colors API Tool allows users to change the color profile of a PDF document programmatically. This tutorial will demonstrate how to send an API call to Convert PDF Colors using PHP, making it easier to automate the process of color conversion in PDF files.

Imagine you are a graphic designer working with multiple clients who require their PDF documents to adhere to specific color profiles for printing. Using the Convert PDF Colors API, you can automate the conversion process, ensuring that all PDFs meet the required color standards without manually adjusting each file.

Convert PDF Colors with 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.

$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 converted, in this case, '/path/to/file'.
      'headers' => [
        'Content-Type' => '' // Set the Content-Type header for the file.
      ]
    ],
    [
      'name' => 'color_profile', // Specify the field name for the color profile.
      'contents' => 'srgb' // Set the value for the color profile (in this case, 'srgb').
    ],
    [
      'name' => 'output', // Specify the field name for the output option.
      'contents' => 'pdfrest_pdf_with_converted_colors' // Set the value for the output option (in this case, 'pdfrest_pdf_with_converted_colors').
    ]
  ]
];

$request = new Request('POST', 'https://api.pdfrest.com/pdf-with-converted-colors', $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 converted PDF content.

Source: GitHub

Breaking Down the Code

The code begins by including the necessary libraries and classes:

require 'vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Utils;

Here, we load the Guzzle HTTP client, which is used to make HTTP requests, and the PSR-7 Request and Utils classes for handling HTTP requests and streams.

Next, we create a new instance of the Guzzle HTTP client:

$client = new Client();

This instance will be used to send the API request.

We then set up the headers for the request, including the API key for authentication:

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

Replace the placeholder API key with your actual API key.

The options array specifies the multipart form data for the request:

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

Here, we specify the file to be converted, the color profile to use (in this case, 'srgb'), and the desired output format.

We create a new HTTP POST request with the API endpoint and headers:

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

This sets up the request to be sent to the Convert PDF Colors endpoint.

We then send the asynchronous request and wait for the response:

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

This sends the request and waits for the response, which contains the converted PDF content.

Finally, we output the response body:

echo $res->getBody();

This prints the converted PDF content to the screen.

Beyond the Tutorial

In this tutorial, we demonstrated how to use the pdfRest Convert PDF Colors API Tool with PHP to convert the color profile of a PDF document. This can be particularly useful for automating the conversion process in workflows that require specific color profiles.

We encourage you to explore all of the pdfRest API Tools in the API Lab and refer to the API Reference Guide for more detailed information. Note that this is an example of a multipart API call, and code samples using JSON payloads can be found at GitHub.

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