How to Split PDF with PHP

Share this page

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

Breakdown of the Code

The PHP script starts by including the necessary libraries and classes for making HTTP requests using Guzzle, a PHP HTTP client.

require 'vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Utils;

Next, an instance of the Guzzle HTTP client is created, and the API key is set in the headers for authentication purposes.

$client = new Client();
$headers = [
  'Api-Key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
];

The $options array is then constructed to define the multipart form data. The 'file' part includes the file to be split, the 'pages[]' parts specify how to split the PDF (by even, odd, or specific page numbers), and the 'output' part names the resulting split PDFs.

$options = [
  'multipart' => [
    // ... (multipart form data)
  ]
];

A POST request is created with the API endpoint and headers. The request is sent asynchronously, and the script waits for the response.

$request = new Request('POST', 'https://api.pdfrest.com/split-pdf', $headers);
$res = $client->sendAsync($request, $options)->wait();

Finally, the response body is echoed out, which contains the split PDFs.

echo $res->getBody();

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.

Generate a self-service API Key now!

Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.

Compare Plans
Contact Us