How to duplicate a PDF page using PHP

Table of contents

    Duplicating PDF pages enables powerful document workflows — apply different watermarks to the same page for multiple recipients, create backups before batch processing, or build complex multipage documents from templates. This tutorial shows you how to duplicate specific PDF pages programmatically using PHP and Nutrient DWS Processor API.
    How to duplicate a PDF page using PHP
    Summary

    Duplicate PDF pages using the duplicate PDF page PHP API. Create a free account, get API credentials, and implement page duplication using PHP’s cURL library. Combine with 30+ other API tools for watermarking, merging, and flattening.

    This tutorial shows how to duplicate PDF pages using the duplicate PDF page PHP API. The free plan includes 200 credits. Different operations consume different credit amounts. Create a free account(opens in a new tab) to get your API key.

    Why duplicate PDF pages?

    Duplicating PDF pages is important for document workflows that require processing the same content differently. Common use cases include:

    • Multi-recipient workflows — Apply different watermarks or annotations to the same page for different recipients, e.g. watermarked versions for clients and clean versions for internal use.
    • Backup and versioning — Create page-level backups before performing destructive operations like flattening or redaction, preserving the original state.
    • Template-based generation — Build complex documents by duplicating template pages and customizing each copy with recipient-specific data.
    • Testing and validation — Duplicate pages to test different processing operations (compression, OCR, conversion) while keeping the original intact.
    • Workflow automation — Process batches of pages programmatically as part of your backend infrastructure, duplicating pages at specific positions for assembly workflows.

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

    Nutrient DWS Processor API

    Page duplication is one of 30+ PDF API tools available. Combine it with other tools for complex workflows:

    Your account gives you access to all PDF API tools.

    Step 1 — Creating a free account on Nutrient

    Go to the signup page(opens in a new tab) to create your free account.

    Free account Nutrient DWS Processor API

    After account creation, the dashboard shows your plan details.

    You get 200 processing credits and access to all PDF API tools.

    Step 2 — Obtaining the API key

    After email verification, get your API key from the dashboard. Click API keys in the left menu to see your keys.

    Duplicate PDF Page PHP API Key

    Copy the Live API key for use with the PHP API.

    Step 3 — Setting up folders and files

    Create a folder called duplicate_pdf and open it in your code editor. Create two subfolders: input_documents and processed_documents.

    In the root folder, create processor.php for your code. Place your PDF files in the input_documents folder.

    Your folder structure:

    duplicate_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": {
    "start": 0,
    "end": 0
    }
    },
    {
    "file": "document"
    },
    {
    "file": "document",
    "pages": {
    "start": -1,
    "end": -1
    }
    }
    ]
    }',
    '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_KEY_HERE with your API key.

    Code explanation

    The code creates a FileHandle variable, initiates curl, and sets up the instructions:

    {
    "parts": [
    {
    "file": "document",
    "pages": {
    "start": 0,
    "end": 0
    }
    },
    {
    "file": "document"
    },
    {
    "file": "document",
    "pages": {
    "start": -1,
    "end": -1
    }
    }
    ]
    }

    This duplicates the first and last page of the PDF, adding them at the end of the file. The API call to the duplicate PDF endpoint saves the response in the processed_documents folder.

    Output

    Run this command to execute the code:

    Terminal window
    php processor.php

    After successful execution, result.pdf appears in the processed_documents folder.

    Final folder structure:

    duplicate_pdf
    ├── input_documents
    ├── processed_documents
    | └── result.pdf
    └── processor.php

    Conclusion

    This tutorial covered duplicating PDF pages in PHP using the duplicate PDF page API.

    You can add this functionality to your existing applications. The same API token works for other operations like merging PDFs and adding watermarks. Sign up(opens in a new tab) for a free trial.

    FAQ

    How does page indexing work in the duplicate PDF 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, and so on. The code example duplicates both the first page (index 0) and last page (index -1).

    Can I duplicate multiple pages at once?

    Yes. You can duplicate multiple pages in a single API call by specifying different page ranges in the parts array. Each part can reference the same document with different page specifications, allowing complex duplication patterns.

    How many credits does page duplication consume?

    The free account includes 200 credits. Each duplication operation consumes 1 credit, regardless of the number of pages duplicated or file size. This means you can duplicate pages in up to 200 PDFs with your free account.

    Can I duplicate pages from multiple source PDFs?

    Yes. You can combine pages from multiple source documents in a single API call. Upload multiple files with different names (like 'document1', 'document2') and reference them in your instructions to duplicate specific pages from each source.

    How do I specify page ranges for duplication?

    Use the pages object with start and end properties to specify ranges. For example, {"start": 0, "end": 2} duplicates the first three pages (0, 1, 2). Omit the pages object to include all pages from the document.

    What PHP version do I need to run this code?

    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.

    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?