How to Split PDF with PHP
Why Would You Split a PDF with PHP?
The pdfRest Split PDF API Tool is a powerful resource that allows users to split a PDF document into multiple parts programmatically. This tutorial will demonstrate how to make an API call to the Split PDF endpoint using PHP.
The Split PDF API is useful in situations where a user needs to separate a large PDF report into individual sections for easier distribution and review.
PHP Code Sample to Split a PDF
'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
];
$options = [
'multipart' => [
[
'name' => 'file',
'contents' => Utils::tryFopen('/path/to/file', 'r'),
'filename' => '/path/to/file',
'headers' => [
'Content-Type' => ''
]
],
[
'name' => 'pages[]',
'contents' => 'even'
],
[
'name' => 'pages[]',
'contents' => 'odd'
],
[
'name' => 'pages[]',
'contents' => '1,3,4-6'
],
[
'name' => 'output',
'contents' => 'pdfrest_split_pdf'
]
]
];
$request = new Request('POST', 'https://api.pdfrest.com/split-pdf', $headers);
$res = $client->sendAsync($request, $options)->wait();
echo $res->getBody();
?>
Source code reference: pdf-rest-api-samples on GitHub
Breaking Down the PHP Code
The sample uses Guzzle and its PSR-7 utilities to construct and send the multipart request. Install Guzzle with composer require guzzlehttp/guzzle and keep Composer’s autoloader available to the script.
require 'vendor/autoload.php'; use GuzzleHttpClient; use GuzzleHttpPsr7Request; use GuzzleHttpPsr7Utils;
Client sends the request, Request represents the POST operation, and Utils::tryFopen opens the PDF as a stream rather than copying it into a text value.
$headers = [ 'Api-Key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' ];
The API key authenticates the call. Guzzle generates the multipart Content-Type and boundary from the multipart option, so they should not be hard-coded separately.
[
'name' => 'file',
'contents' => Utils::tryFopen('/path/to/file', 'r'),
'filename' => 'file_name.pdf',
'headers' => ['Content-Type' => 'application/pdf']
]
The file part carries the source PDF. Its filename and media type allow the service to validate the uploaded input.
[ 'name' => 'pages[]', 'contents' => 'even' ], [ 'name' => 'pages[]', 'contents' => 'odd' ], [ 'name' => 'pages[]', 'contents' => '1,3,4-6' ]
Each repeated pages[] field creates one output PDF, in the order supplied. Page expressions support comma-separated pages and ranges, last, reversed ranges, even, and odd. If no pages[] field is sent, each source page becomes its own single-page PDF.
[ 'name' => 'output', 'contents' => 'pdfrest_split_pdf' ]
output is the filename prefix. The service appends a sequence number and .pdf to distinguish the generated files.
$request = new Request('POST', 'https://api.pdfrest.com/split-pdf', $headers);
$res = $client->sendAsync($request, $options)->wait();
echo $res->getBody();
The call waits for completion and prints JSON describing the output resources. Production code should inspect the HTTP status before deserializing the success response.
Summarizing What We Did
This tutorial has walked through the process of making an API call to the Split PDF endpoint of the pdfRest API using PHP. By following these steps, a user can programmatically split PDF documents based on their requirements.
To explore and demo all of the pdfRest API Tools, visit the API Lab. For more detailed information, refer to the API Reference documentation.
Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at pdf-rest-api-samples on GitHub.