How to Resize PDF Pages with PHP

Learn how to resize PDF pages using PHP with Set Page Boxes API by pdfRest.
Share this page

Why Resize PDF Pages with PHP?

The pdfRest Set Page Boxes API Tool allows you to modify the page boxes of a PDF document, specifically the MediaBox, to control the physical dimensions of the pages. This tutorial will guide you through the process of sending an API call to the Set Page Boxes API Tool endpoint using PHP to resize all pages of a PDF document.

Imagine you have a PDF document where all pages need to conform to a specific standard dimension for consistent display or processing. By using the Set Page Boxes API Tool and the "all" range, you can efficiently resize all PDF pages to meet your requirements in a single API call.

Resize PDF Pages with PHP Code Example


$apiKey = 'YOUR_API_KEY'; // Replace with your actual API key
$pdfFilePath = '/path/to/your/file.pdf'; // Replace with the actual path to your PDF file
$outputName = 'resized_all_to_a4_out.pdf';

$pdf_with_page_boxes_endpoint_url = 'https://api.pdfrest.com/pdf-with-page-boxes-set';

// Define the MediaBox adjustments to resize all pages from Letter to A4
// Letter: 612 x 792 points
// A4:    595 x 842 points
$deltaWidthInward = (612 - 595) / 2;   // Positive margin to shrink width inward
$deltaHeightOutward = (792 - 842) / 2;  // Negative margin to expand height outward

$resizeBoxOptions = json_encode([
    'boxes' => [
        [
            'box' => 'media',
            'pages' => [
                [
                    'range' => 'all',
                    'left' => $deltaWidthInward,
                    'top' => $deltaHeightOutward,
                    'bottom' => $deltaHeightOutward,
                    'right' => $deltaWidthInward,
                ],
            ],
        ],
    ],
]);

$payload = [
    [
        'name' => 'file',
        'contents' => fopen($pdfFilePath, 'r'),
        'filename' => basename($pdfFilePath),
        'headers' => [
            'Content-Type' => 'application/pdf',
        ],
    ],
    [
        'name' => 'boxes',
        'contents' => $resizeBoxOptions,
    ],
    [
        'name' => 'output',
        'contents' => pathinfo($outputName, PATHINFO_FILENAME),
    ],
];

$headers = [
    'Accept' => 'application/json',
    'Api-Key' => $apiKey,
];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $pdf_with_page_boxes_endpoint_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true); // Required for PHP 5.6+

echo "Sending POST request to pdf-with-page-boxes-set endpoint to resize all PDF pages to A4...\n";
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

echo "Response status code: " . $httpCode . "\n";

if ($response === false) {
    echo 'Curl error: ' . curl_error($ch) . "\n";
} else {
    $responseJson = json_decode($response, true);
    echo json_encode($responseJson, JSON_PRETTY_PRINT) . "\n";
}

curl_close($ch);

Source: GitHub (Note: This example has been modified to focus on resizing all pages)

Breaking Down the Code for Resizing All Pages

The code starts by defining the API key, input PDF file path, and desired output file name. Remember to replace 'YOUR_API_KEY' with your actual pdfRest API key and '/path/to/your/file.pdf' with the correct path to your PDF file.

$apiKey = 'YOUR_API_KEY';
$pdfFilePath = '/path/to/your/file.pdf';
$outputName = 'resized_all_to_a4_out.pdf';

$pdf_with_page_boxes_endpoint_url = 'https://api.pdfrest.com/pdf-with-page-boxes-set';

This section sets the API endpoint URL.

// Define the MediaBox adjustments to resize all pages from Letter to A4
// Letter: 612 x 792 points
// A4:    595 x 842 points
$deltaWidthInward = (612 - 595) / 2;   // Positive margin to shrink width inward
$deltaHeightOutward = (792 - 842) / 2;  // Negative margin to expand height outward

$resizeBoxOptions = json_encode([
    'boxes' => [
        [
            'box' => 'media',
            'pages' => [
                [
                    'range' => 'all',
                    'left' => $deltaWidthInward,
                    'top' => $deltaHeightOutward,
                    'bottom' => $deltaHeightOutward,
                    'right' => $deltaWidthInward,
                ],
            ],
        ],
    ],
]);

The $resizeBoxOptions array is configured to resize all PDF pages by adjusting the MediaBox. The 'box' key is set to 'media'. Setting 'range' to 'all' ensures these MediaBox adjustments are applied to every page. Positive $deltaWidthInward values shrink the width, and positive $deltaHeightOutward values expand the height relative to the top and bottom.

$payload = [
    [
        'name' => 'file',
        'contents' => fopen($pdfFilePath, 'r'),
        'filename' => basename($pdfFilePath),
        'headers' => [
            'Content-Type' => 'application/pdf',
        ],
    ],
    [
        'name' => 'boxes',
        'contents' => $resizeBoxOptions,
    ],
    [
        'name' => 'output',
        'contents' => pathinfo($outputName, PATHINFO_FILENAME),
    ],
];

$headers = [
    'Accept' => 'application/json',
    'Api-Key' => $apiKey,
];

This section constructs the multipart/form-data payload, including the PDF file, the JSON-encoded $resizeBoxOptions, and the desired output filename. It also sets the necessary headers, including your API key.

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $pdf_with_page_boxes_endpoint_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true); // Required for PHP 5.6+

echo "Sending POST request to pdf-with-page-boxes-set endpoint to resize all PDF pages to A4...\n";
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

echo "Response status code: " . $httpCode . "\n";

if ($response === false) {
    echo 'Curl error: ' . curl_error($ch) . "\n";
} else {
    $responseJson = json_decode($response, true);
    echo json_encode($responseJson, JSON_PRETTY_PRINT) . "\n";
}

curl_close($ch);

Finally, this part of the code uses curl to send the POST request to the pdfRest API endpoint to resize all PDF pages using the specified MediaBox adjustments and handles the API response.

Next Steps for Resizing All PDF Pages

This tutorial demonstrated how to use PHP and the pdfRest Set Page Boxes API Tool to resize all pages of a PDF from US Letter to A4 by setting the MediaBox with the 'range' => 'all' option. You can adapt the $deltaWidthInward and $deltaHeightOutward calculations to resize to other dimensions. This approach efficiently applies the same resizing to every page in your document.

To learn more about the Set Page Boxes API Tool and its capabilities, including setting other page boxes and using different page ranges, you can demo all of the pdfRest API Tools in the API Lab and refer to the API Reference Guide for detailed documentation.

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