How to Convert PDF Files from RGB to CYMK with PHP

Learn how to convert PDFs from RGB to CMYK with PHP using pdfRest API Toolkit
Share this page

Why Convert PDF from RGB to CMYK with PHP?

The pdfRest Convert PDF Colors API Tool offers a flexible and efficient way to modify the color profile of PDF documents. This tutorial will demonstrate how to use PHP, specifically with the Guzzle HTTP client, to send an API call to convert PDF colors, focusing on the common RGB to CMYK conversion.

Converting RGB (Red, Green, Blue) colors to CMYK (Cyan, Magenta, Yellow, Black) is essential for preparing PDF documents for printing. CMYK is the color model used by most printers, and accurate RGB to CMYK conversions ensure that the printed colors align with the desired appearance. This is particularly important for projects like brochures, magazines, and marketing materials where color fidelity is crucial.

Convert PDF from RGB to CMYK 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' => 'acrobat9-cmyk' // Set the value for the color profile (in this case, 'acrobat9-cmyk').
    ],
    [
      '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 Repository

Breaking Down the Code

The code begins by including the necessary libraries using the line:

require 'vendor/autoload.php';

This line loads the Guzzle HTTP client, which is used to make HTTP requests.

Next, we instantiate the Guzzle client:

$client = new Client();

This creates a new HTTP client that will be used to send requests.

The API key is set in the headers for authentication purposes:

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

Ensure you replace the placeholder with your actual API key.

The options array is constructed to include the multipart 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' => 'acrobat9-cmyk'
    ],
    [
      'name' => 'output',
      'contents' => 'pdfrest_pdf_with_converted_colors'
    ]
  ]
];

The 'file' part specifies the PDF file to be converted. The 'color_profile' part sets the desired color profile, and the 'output' part determines the output format.

The request is created and sent asynchronously:

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

This sends a POST request to the specified endpoint with the provided headers and options. The response is then outputted:

echo $res->getBody();

Beyond the Tutorial

In this tutorial, you successfully learned how to use PHP to send an API request to convert the colors of a PDF document from RGB to CMYK using the pdfRest Convert PDF Colors API Tool. This process can be integrated into larger applications where PDF color conversion is needed.

For further exploration, consider trying out all the pdfRest API Tools available in the API Lab. You can also refer to the API Reference Guide for more detailed information on each endpoint and its parameters.

Note: This example demonstrates a multipart API call. Code samples using JSON payloads can be found at GitHub Repository.

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