How to Extract Pages from PDF Files with PHP

Learn how to Extract Pages from PDF Files with PHP by calling Split PDF API Tool by pdfRest.
Share this page

Why Extract PDF Pages with PHP?

The pdfRest Split PDF API Tool is a powerful resource for developers looking to programmatically extract PDF pages into separate documents. Whether you need to extract specific pages for reporting, distribute parts of a document to different team members, or simply organize large PDFs into manageable sections, the Split PDF API provides a straightforward solution.

This tutorial will guide you through making an API call to the Split PDF tool using PHP, a popular server-side scripting language.

Extract PDF Pages with PHP Code Example

?php
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' => 'xxxxxxxx-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', 'r'), // Open the file specified by '/path/to/file' for reading.
      'filename' => '/path/to/file', // Set the filename for the file to be processed, in this case, '/path/to/file'.
      'headers' => [
        'Content-Type' => '' // Set the Content-Type header for the file.
      ]
    ],
    [
      'name' => 'pages[]', // Specify the field name for the pages option as an array.
      'contents' => 'even' // Set the value for the pages option to 'even'.
    ],
    [
      'name' => 'pages[]', // Specify the field name for the pages option as an array.
      'contents' => 'odd' // Set the value for the pages option to 'odd'.
    ],
    [
      'name' => 'pages[]', // Specify the field name for the pages option as an array.
      'contents' => '1,3,4-6' // Set the value for the pages option to '1,3,4-6' (a combination of specific pages and page ranges).
    ],
    [
      'name' => 'output', // Specify the field name for the output option.
      'contents' => 'pdfrest_split_pdf' // Set the value for the output option to generate split PDFs.
    ]
  ]
];

$request = new Request('POST', 'https://api.pdfrest.com/split-pdf', $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 split PDFs.
?>

Source code reference: pdf-rest-api-samples

Breaking Down the Code

The above code is a PHP script that uses the Guzzle HTTP client to send a multipart POST request to the pdfRest Split PDF API endpoint.

$headers = [
  'Api-Key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
];

The $headers array includes the 'Api-Key' required for authentication with the pdfRest API.

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

The $options array contains the multipart form data, which includes the file to split and the split options such as 'pages[]' and 'output'. Each element within the 'multipart' array represents a different part of the form data.

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

A new Request object is created with the POST method, the API endpoint URL, and the headers array.

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

The client sends the request asynchronously and waits for the response, which is stored in $res.

echo $res->getBody();

Finally, the response body is echoed out, which contains the resulting split PDF files.

Beyond the Tutorial

In this tutorial, we have learned how to use PHP to call the pdfRest Split PDF API to extract PDF pages into separate documents based on page selections. To explore all the capabilities of the pdfRest API Tools, you can demo them in the API Lab, and 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.

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