How to delete PDF pages using PHP

Table of contents

    Remove pages from PDFs in PHP to delete confidential data, save storage space, or remove unnecessary information. This tutorial demonstrates programmatic page deletion using cURL and Nutrient DWS Processor API for document-heavy workflows.
    How to delete PDF pages using PHP
    Summary

    Delete PDF pages using our delete PDF page PHP API. Create a free account, get API credentials, and implement page deletion using cURL. Remove pages to save storage space or delete confidential information.

    This tutorial shows how to delete PDF pages using our delete PDF page PHP API. The free plan includes 200 credits. Different operations consume different credit amounts, so the number of documents you can process varies. Create a free account(opens in a new tab) to access your API key.

    Why delete PDF pages?

    Deleting PDF pages is essential for document workflows that require removing sensitive or unnecessary content. Common use cases include:

    • Data privacy compliance — Remove pages containing confidential information before sharing documents externally, ensuring sensitive data doesn’t leave your organization.
    • Storage optimization — Delete unnecessary pages to reduce file size and cloud storage costs, which is especially important for high-volume document processing.
    • Document preparation — Remove draft pages, cover sheets, or annotations before final distribution to clients or stakeholders.
    • Automated processing — Filter out specific page ranges as part of backend document workflows, streamlining document management pipelines.
    • Content curation — Extract relevant sections by removing everything else, creating focused documents for specific audiences or purposes.

    The delete PDF page API automates this process in your workflow.

    Nutrient DWS Processor API

    Deleting PDF pages is one of 30+ operations available through our PDF API tools. Combine deletion with other tools to build complex document processing workflows:

    After creating your account, you can access all our PDF API tools.

    Step 1 — Creating a free account on Nutrient

    Go to our website(opens in a new tab), where you’ll see the page below, prompting you to create your free account.

    Free account Nutrient DWS Processor API

    After creating your account, you’ll see your plan overview page.

    You start with 200 credits and can access all our PDF API tools.

    Step 2 — Obtaining the API key

    After you’ve verified your email, you can get your API key from the dashboard. In the menu on the left, click API keys. You’ll see the following page, which is an overview of your keys.

    Delete PDF Pages PHP API Key

    Copy the Live API key — you’ll need it for the delete PDF page API.

    Step 3 — Setting up folders and files

    Create a folder called delete_pdf and open it in a code editor. This tutorial uses VS Code. Create two folders inside delete_pdf named input_documents and processed_documents.

    In the root folder, delete_pdf, create a file called processor.php. This file will contain your code.

    Your folder structure will look like this:

    delete_pdf
    ├── input_documents
    ├── processed_documents
    └── processor.php

    Step 4 — Writing the code

    Open the processor.php file and paste the code below into it:

    <?php
    $FileHandle = fopen('processed_documents/result.pdf', 'w+');
    $curl = curl_init();
    curl_setopt_array($curl, array(
    CURLOPT_URL => 'https://api.nutrient.io/build',
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_POSTFIELDS => array(
    'instructions' => '{
    "parts": [
    {
    "file": "document",
    "pages": {
    "end": 2
    }
    },
    {
    "file": "document",
    "pages": {
    "start": 4
    }
    }
    ]
    }',
    'document' => new CURLFILE('input_documents/document.pdf')
    ),
    CURLOPT_HTTPHEADER => array(
    'Authorization: Bearer YOUR_API_KEY_HERE'
    ),
    CURLOPT_FILE => $FileHandle,
    ));
    $response = curl_exec($curl);
    curl_close($curl);
    fclose($FileHandle);

    Make sure to replace YOUR_API_TOKEN with your API key.

    Code explanation

    The "instructions" section keeps pages 1–3 (indices 0–2) and page 5 onward (index 4+), effectively deleting page 4 (index 3).

    The code makes the API call using the cURL library. The API response (the PDF with deleted page) is stored in the processed_documents folder.

    Output

    To execute the code, run the command below:

    Terminal window
    php processor.php

    After successful execution, a new file, result_php.pdf, appears in the processed_documents folder.

    The folder structure will look like this:

    delete_pdf
    ├── input_documents
    | └── document.pdf
    ├── processed_documents
    | └── result_php.pdf
    └── processor.php

    Additional resources

    Explore more ways to work with Nutrient API:

    Conclusion

    This tutorial showed how to delete pages from a PDF document using PHP and our delete PDF page API.

    Integrate PDF API functions into your existing applications to remove pages from PDFs. The same API token supports other operations like merging documents, adding watermarks, and more. Sign up(opens in a new tab) to start your free trial.

    FAQ

    How does page indexing work in Nutrient DWS Processor API?

    The API uses zero-based indexing, where page 0 is the first page. You can use negative indexing too: -1 refers to the last page, -2 to the second-to-last page. The code example keeps pages 1–3 (indices 0–2) and page 5 onward (index 4+), deleting page 4 (index 3).

    Can I delete pages from multiple PDFs in one API call?

    Yes. Upload multiple files with different names and reference them in the parts array. Each part can specify which source document and which pages to include, allowing complex multidocument processing workflows where you delete specific pages from different files.

    How many credits does the delete PDF API consume?

    Each deletion operation consumes 1 credit, regardless of the number of pages deleted or file size. Start with a free trial that includes 200 credits, allowing you to process up to 200 PDF deletion requests at no cost.

    Can I combine page deletion with other API operations?

    Yes. Nutrient DWS Processor API supports chaining multiple operations in a single request. Use the actions array to delete pages and then apply operations like watermarking, flattening, or OCR to the resulting document.

    How do I specify which pages to keep versus which to delete?

    The parts array defines which pages to keep — the API removes everything else. For example, to delete only page 3 from a 5-page document, specify parts for pages 1–2 ({"end": 2}) and 4 onward ({"start": 4}). The API keeps specified pages and deletes the rest.

    What PHP version and extensions are required?

    This code works with PHP 7.0 and higher. It requires the cURL extension, which is typically enabled by default in most PHP installations. Verify with php -m | grep curl to check if cURL is available.

    How do I handle errors in PHP when calling the API?

    Check the HTTP response status after curl_exec() using curl_getinfo($curl, CURLINFO_HTTP_CODE). A 200 status indicates success. For errors, read the response body which contains detailed error messages in JSON format explaining what went wrong.

    Jonathan D. Rhyne

    Jonathan D. Rhyne

    Co-Founder and CEO

    Jonathan joined PSPDFKit in 2014. As Co-founder and CEO, Jonathan defines the company’s vision and strategic goals, bolsters the team culture, and steers product direction. When he’s not working, he enjoys being a dad, photography, and soccer.

    Explore related topics

    FREE TRIAL Ready to get started?