How to Delete Pages from PDF Files with PHP

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

Why Delete PDF Pages with PHP?

The pdfRest Split PDF API Tool is a powerful resource for developers who need to programmatically delete pages from PDF documents. This tutorial will guide you through the process of sending an API call to the Split PDF endpoint using PHP.

This can be useful in situations where PDF documents contain sensitive information on certain pages.

Delete PDF Pages with PHP Code Example

require 'vendor/autoload.php';

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Utils;

$client = new Client();

$headers = [
  'Api-Key' => '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' => '1-3,5-last'
    ],
    [
      '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: pdf-rest-api-samples on GitHub

Breaking Down the Code

The provided PHP code uses the Guzzle HTTP client to send a multipart POST request to the Split PDF API endpoint. Here's a breakdown of the key components:

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

This snippet loads the required classes from the Guzzle library and sets up the necessary namespaces.

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

The API key is passed in the headers for authentication purposes.

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

The $options array contains the multipart form data, including the file to split and the splitting criteria. In this example, a single pages[] parameter is submitted with a page range value that encompasses all pages except for page 4.

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

A new POST request is created with the API endpoint and the headers.

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

The request is sent asynchronously, and the response is printed. The response includes the output document's ID and a download link. The result is a single document that includes all but the fourth page from the original file.

Beyond the Tutorial

By following the steps above, you've learned how to delete PDF pages using the pdfRest API and PHP. You can now apply this knowledge to delete pages with your own PHP applications. We encourage you to demo all of the pdfRest API Tools in the API Lab and refer to the API Reference documentation for further exploration.

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