How to Resize PDF Pages with PHP
Change PDF Page Dimensions with PHP
PDF page dimensions are defined by page boxes. The MediaBox establishes the physical page boundary, while other boxes can define the visible, printed, or finished regions. The pdfRest Set Page Boxes API Tool lets a PHP application adjust these boundaries programmatically through the /pdf-with-page-boxes-set endpoint.
This distinction matters: changing a MediaBox changes the page canvas, but it does not scale the text, images, or vector artwork already on the page. Expanding the boundary can add surrounding page area, while reducing it can place existing content outside the new boundary. If a workflow needs to proportionally enlarge or shrink the page content itself, page-box adjustments alone are not the same operation.
For example, an archival intake system may receive a batch containing both US Letter and A4 scans. If the archive requires an A4 page boundary before documents are assembled into a single package, the application can apply the approved Letter-to-A4 MediaBox calculation to every file. The content stays at its original scale while the page boundary is standardized consistently across the batch.
The PHP example calculates the difference between Letter and A4 dimensions in PDF points and sends the resulting MediaBox adjustments as multipart form data.
PHP Set Page Boxes Code Example
Install Guzzle with Composer, replace the API-key and local-file placeholders, and select the US or EU base URL shown in the official sample. The Letter-to-A4 values remain together in the boxes JSON so the calculation is easy to review.
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.
// By default, we use the US-based API service. This is the primary endpoint for global use.
$apiUrl = "https://api.pdfrest.com";
/* For GDPR compliance and enhanced performance for European users, you can switch to the EU-based service by uncommenting the URL below.
* For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work
*/
//$apiUrl = "https://eu-api.pdfrest.com";
$client = new Client(); // Create a new instance of the Guzzle HTTP client.
$headers = [
'Api-Key' => 'xxxxxxxxx-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.pdf', 'r'), // Open the file specified by the '/path/to/file' for reading.
'filename' => 'file.pdf', // Set the filename for the file to be processed, in this case, '/path/to/file'.
'headers' => [
'Content-Type' => 'application/pdf' // Set the Content-Type header for the file.
]
],
[
'name' => 'boxes', // Specify the field name for the text options.
'contents' => '{"boxes":[{"box":"media","pages":[{"range":"all","left":8.5,"top":-25,"bottom":-25,"right":8.5}]}]}' // Set the value for the boxes option. This is a JSON-formatted string consisting of an array with sets of text options.
],
[
'name' => 'output', // Specify the field name for the output option.
'contents' => 'resized_all_to_a4_out' // Set the value for the output option (in this case, 'pdfrest_pdf_with_boxes_set').
]
]
];
$request = new Request('POST', $apiUrl.'/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 PDF with the boxes set.
Structural reference: pdfRest Set Page Boxes multipart PHP sample
Calculate MediaBox Adjustments Carefully
PDF dimensions use points, with 72 points per inch. US Letter is 612 by 792 points, and A4 is approximately 595 by 842 points. The difference is applied across the page edges according to the desired alignment. A centered adjustment divides the horizontal and vertical differences between opposite sides; an anchored adjustment can leave one edge fixed and apply the full difference to the other.
The signs are significant because these values act as margins from the current boundary. Positive left and right values move those edges inward, while negative top and bottom values extend those edges outward. For Letter-to-A4 conversion, the target becomes narrower and taller, so the centered calculation applies 8.5 to both horizontal sides and -25 to both vertical sides. This explicit Letter-to-A4 calculation can be reused wherever the same source and target page dimensions apply.
Use the pages parameter when only part of the document needs a new boundary. A long document may contain covers, foldouts, or attachments that should retain their original dimensions even when the main pages are standardized.
Continue with the New Page Boundary
After a successful response, download the output or pass its resource ID into the next pdfRest step. Approved page-box settings can account for annotations, bleed, trim marks, or nonstandard CropBox values, allowing each document template to move through a consistent resizing and delivery path.
Check the HTTP status and returned JSON before using the output. Production code should add timeouts, handle Guzzle request and connection exceptions separately from API errors, and close file streams. Keep credentials out of logs and source control.
If the PDF is already on the pdfRest processing server, send a JSON request with its resource ID instead of uploading it again. The official Set Page Boxes JSON-payload PHP sample shows that pattern. Use API Lab to configure page-box values and review the Set Page Boxes API reference for current request details.