How to duplicate a PDF page using PHP
Table of contents
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:
- Convert MS Office files and images to PDF, and then duplicate specific pages
- Merge multiple PDFs and duplicate selected pages
- Apply watermarks, and split or flatten PDFs with page duplication at any step
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.

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.

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.phpStep 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:
php processor.phpAfter successful execution, result.pdf appears in the processed_documents folder.
Final folder structure:
duplicate_pdf├── input_documents├── processed_documents| └── result.pdf└── processor.phpConclusion
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
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).
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.
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.
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.
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.
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.