How to Crop PDF with PHP

Learn how to crop PDF files by setting the CropBox with pdfRest Set Page Boxes API using Python.
Share this page

Why Crop PDF with PHP?

The pdfRest Set Page Boxes API Tool allows developers to programmatically define and modify the page boxes of a PDF document, specifically the CropBox, to control the visible content area. This tutorial demonstrates how to use PHP to send an API call to set the CropBox using this tool to crop PDF files.

Consider a scenario where you have PDF documents with excessive margins that you want to remove for better readability or to fit specific layout requirements. By using the Set Page Boxes tool to adjust the CropBox via the pdfRest API, you can automate the process of cropping PDFs to focus on the essential content.

Crop 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' => 'YOUR_API_KEY' // Set the API key in the headers for authentication. Replace with your actual API key.
];

// Define the CropBox settings to crop the first page
$crop_box_options = [
    'boxes' => [
        [
            'box' => 'crop',
            'pages' => [
                [
                    'range' => '1',
                    'left' => 50,  // Adjust these values to your desired crop
                    'top' => 50,
                    'bottom' => 50,
                    'right' => 50
                ]
            ]
        ]
    ]
];

$options = [
    'multipart' => [
        [
            'name' => 'file', // Specify the field name for the file.
            'contents' => Utils::tryFopen('/path/to/your/file.pdf', 'r'), // Open the file specified by the '/path/to/your/file.pdf' for reading. Replace with your actual file path.
            'filename' => 'filename.pdf', // Set the filename for the file to be processed.
            'headers' => [
                'Content-Type' => 'application/pdf' // Set the Content-Type header for the file.
            ]
        ],
        [
            'name' => 'boxes', // Specify the field name for the text options.
            'contents' => json_encode($crop_box_options) // Set the value for the boxes option as a JSON string.
        ],
        [
            'name' => 'output', // Specify the field name for the output option.
            'contents' => 'cropped_example_out' // Set the desired name for the output file.
        ]
    ]
];

$request = new Request('POST', 'https://api.pdfrest.com/pdf-with-page-boxes-set', $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 cropped PDF.

Source: GitHub Repository (Note: This example has been modified to focus on cropping)

Breaking Down the Code for Cropping

require 'vendor/autoload.php';

This line includes the Composer autoloader to load the Guzzle HTTP client library.

$client = new Client();

This creates a new instance of the Guzzle HTTP client, which will be used to make the API request.

$headers = [
    'Api-Key' => 'YOUR_API_KEY'
];

Here, you define the headers for the API request, including your unique API key. Replace 'YOUR_API_KEY' with your actual pdfRest API key.

$crop_box_options = [
    'boxes' => [
        [
            'box' => 'crop',
            'pages' => [
                [
                    'range' => '1',
                    'left' => 50,  // Adjust these values to your desired crop
                    'top' => 50,
                    'bottom' => 50,
                    'right' => 50
                ]
            ]
        ]
    ]
];

The $crop_box_options array is configured to specifically crop the PDF. The 'box' is set to 'crop', and the 'pages' array targets the first page ('range' => '1'). The 'left', 'top', 'bottom', and 'right' values define the new boundaries of the visible content area in points. Modify these values to achieve your desired cropping.

$options = [
    'multipart' => [
        [
            'name' => 'file',
            'contents' => Utils::tryFopen('/path/to/your/file.pdf', 'r'), // Replace with your actual file path
            'filename' => 'filename.pdf',
            'headers' => [
                'Content-Type' => 'application/pdf'
            ]
        ],
        [
            'name' => 'boxes',
            'contents' => json_encode($crop_box_options)
        ],
        [
            'name' => 'output',
            'contents' => 'cropped_example_out'
        ]
    ]
];

This section sets up the multipart form data for the API request. It includes the PDF file (remember to replace '/path/to/your/file.pdf' with your file's actual path), the $crop_box_options encoded as a JSON string, and the desired output filename.

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

This creates a new HTTP POST request to the pdfRest API endpoint for setting page boxes.

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

This line sends the API request and waits for the response from the pdfRest server.

echo $res->getBody();

Finally, the response from the API, which will be the cropped PDF data, is outputted.

Further Exploration for PDF Cropping

This tutorial showed you how to crop PDF files using PHP and the pdfRest Set Page Boxes API by specifically adjusting the CropBox. You can experiment with different margin values in the $crop_box_options to achieve various cropping results and apply these settings to different page ranges. For more advanced control and to explore setting other page boxes, visit the API Lab and consult the API Reference Guide.

Note: This example uses a multipart API call. JSON payload examples for setting page boxes, including the CropBox, 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.