How to Convert PDF Files from Color to Black and White (Grayscale) with PHP

Learn how to convert color PDFs to black and white with PHP using pdfRest Convert PDF Colors API Tool
Share this page

Why Convert PDFs from Color to Black and White with PHP?

The pdfRest Convert PDF Colors API Tool is a powerful resource for developers looking to programmatically alter the color profiles of PDF documents. This tutorial will guide you through the process of sending an API call to convert a color PDF to black and white using PHP, leveraging the capabilities of the pdfRest API. By following this tutorial, you'll learn how to integrate this functionality into your PHP applications, enabling automated color conversion for your PDF files.

In real-world scenarios, converting PDF colors to grayscale can be crucial for various purposes. For instance, a document preparation specialist might need to convert a colorful PDF report into a black and white version for archival purposes, to reduce file size, or to improve readability on certain devices. By using the Convert PDF Colors API, these conversions can be done quickly and efficiently, saving time and reducing the potential for human error.

Convert PDFs from Color to Black and White 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' => 'gamma-22' // Set the value for the color profile (in this case, 'gamma-22').
    ],
    [
      '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 requiring the autoload file to load the Guzzle HTTP client, which is essential for making HTTP requests in PHP:

require 'vendor/autoload.php';

Next, the Guzzle HTTP client and related classes are imported:

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

An instance of the Guzzle HTTP client is created to facilitate sending requests:

$client = new Client();

The headers array includes the API key for authentication. Replace 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' with your actual API key:

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

The options array is configured for a multipart request. It includes the file to be converted, specifying the field name and opening the file for reading:

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

The request is created with a POST method to the specified API endpoint, including the headers:

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

The request is sent asynchronously, and the response is awaited:

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

Finally, the response body, which contains the converted PDF content, is output:

echo $res->getBody();

Beyond the Tutorial

In this tutorial, you learned how to use PHP to call the pdfRest Convert PDF Colors API, which allows you to automate the process of converting color PDF documents to grayscale. This can be particularly useful for optimizing files for black and white printing. To further explore the capabilities of the pdfRest API, consider experimenting with other tools available in the API Lab.

For more detailed information about the API endpoints and parameters, consult the API Reference Guide. Additionally, note that this example demonstrates a multipart API call, and code samples using JSON payloads can be found at the GitHub Repository.

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