How to Compress PDF Files with PHP

Learn how to use pdfRest Compress PDF API Tool with PHP to compress PDF files and reduce file size.
Share this page

Why Compress PDF with PHP?

The pdfRest Compress PDF API Tool is a powerful resource for developers looking to reduce the size of PDF files programmatically. Compressing PDFs can be crucial in many scenarios, such as when you need to send large documents via email, save storage space, or improve website load times by serving smaller files.

This tutorial will demonstrate how to send an API call to Compress PDF using PHP, a popular server-side scripting language.

Compress PDF 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 compressed, in this case, '/path/to/file'.
      'headers' => [
        'Content-Type' => '' // Set the Content-Type header for the file.
      ]
    ],
    [
      'name' => 'compression_level', // Specify the field name for the compression level.
      'contents' => 'medium' // Set the value for the compression level (in this case, 'medium').
    ],
    [
      'name' => 'output', // Specify the field name for the output option.
      'contents' => 'pdfrest_compressed_pdf' // Set the value for the output option (in this case, 'pdfrest_compressed_pdf').
    ]
  ]
];

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

Source of the provided code: GitHub

Breaking Down the PHP Code

This code snippet demonstrates how to use the Guzzle HTTP client to send a multipart/form-data POST request to the pdfRest API to compress a PDF file.

The $headers array includes the 'Api-Key' which is used for authentication with the pdfRest API.

The $options array specifies the multipart body of the request. It contains three parts:

  • file: The file to be compressed. It includes the 'contents', which is a stream of the file opened for reading, and 'filename', which is the path to the file.
  • compression_level: The desired level of compression, which can be 'low', 'medium', or 'high'.
  • output: The name of the output file or the option to return the compressed PDF content.

The new Request() function creates a new HTTP POST request with the specified endpoint and headers. The $client->sendAsync() function sends the request and wait() is called to wait for the response. Finally, $res->getBody() outputs the response body.

Beyond the Tutorial

In this tutorial, we have learned how to compress a PDF file using the pdfRest Compress PDF API Tool with PHP. The code sends a multipart request to the pdfRest API, which processes the file and returns the compressed PDF content.

To explore all of the pdfRest API Tools, you can demo them in the API Lab at API Lab. For more detailed information, refer to the API Reference documentation at Cloud API Reference Guide.

Note: This is an example of a multipart API call. 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.