Duplicate PDF Pages in Linux

Information

PSPDFKit Processor has been deprecated and replaced by PSPDFKit Document Engine. All PSPDFKit Processor licenses will work as before and be supported until 15 May 2024 (we will contact you about license migration). To start using Document Engine, refer to the migration guide. With Document Engine, you’ll have access to robust new capabilities (read the blog for more information).

PSPDFKit Processor lets you duplicate pages of a document using the /build endpoint.

Before you get started, make sure Processor is up and running.

You can download and use either of the following sample documents for the examples in this guide:

You’ll be sending multipart POST requests with instructions to Processor’s /build endpoint. To learn more about multipart requests, refer to our blog post on the topic, A Brief Tour of Multipart Requests.

Check out the API Reference to learn more about the /build endpoint and all the actions you can perform on PDFs with PSPDFKit Processor.

Duplicating Pages of a File on Disk

Send a multipart request to the /build endpoint, attaching the input file(s) and the instructions JSON:

The following example duplicates the second page (with index 1) of the document before merging it with the other pages.

It splits the eight-page document into four parts. The first part is the first page of the document. The second and third parts are the second page, which is the page we want to duplicate. This is why it appears twice. The fourth part is the remainder of the pages.

curl -X POST http://localhost:5000/build \
  -F document=@/path/to/example-document.pdf \
  -F instructions='{
  "parts": [
    {
      "file": "document",
      "pages": {
        "start": 0,
        "end": 0
      }
    },
    {
      "file": "document",
      "pages": {
        "start": 1,
        "end": 1
      }
    },
    {
      "file": "document",
      "pages": {
        "start": 1,
        "end": 1
      }
    },
    {
      "file": "document",
      "pages": {
        "start": 2,
        "end": 7
      }
    }
  ]
}' \
  -o result.pdf
POST /process HTTP/1.1
Content-Type: multipart/form-data; boundary=customboundary

--customboundary
Content-Disposition: form-data; name="document"; filename="example-document.pdf"
Content-Type: application/pdf

<PDF data>
--customboundary
Content-Disposition: form-data; name="instructions"
Content-Type: application/json

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

The following example merges three documents and duplicates the second document. The output PDF contains two copies of the pages from the second document, merged with the pages from the other documents in the order that they were specified in in instructions.parts.

curl -X POST http://localhost:5000/build \
  -F document1=@/path/to/example-document1.pdf \
  -F document-to-duplicate=@/path/to/example-document-to-duplicate.pdf \
  -F document3=@/path/to/document3.pdf \
  -F instructions='{
  "parts": [
    {
      "file": "document1"
    },
    {
      "file": "document-to-duplicate"
    },
    {
      "file": "document-to-duplicate"
    },
    {
      "file": "document3"
    }
  ]
}' \
  -o result.pdf
POST /process HTTP/1.1
Content-Type: multipart/form-data; boundary=customboundary

--customboundary
Content-Disposition: form-data; name="document1"; filename="example-document1.pdf"
Content-Type: application/pdf

<PDF data>
--customboundary
Content-Disposition: form-data; name="document-to-duplicate"; filename="example-document-to-duplicate.pdf"
Content-Type: application/pdf

<PDF data>
--customboundary
Content-Disposition: form-data; name="document3"; filename="document3.pdf"
Content-Type: application/pdf

<PDF data>
--customboundary
Content-Disposition: form-data; name="instructions"
Content-Type: application/json

{
  "parts": [
    {
      "file": "document1"
    },
    {
      "file": "document-to-duplicate"
    },
    {
      "file": "document-to-duplicate"
    },
    {
      "file": "document3"
    }
  ]
}
--customboundary--

Duplicating Pages of a File from a URL

The following example specifies the document-to-duplicate part of the multipart request with a URL, instead of with a path to the document on the disk:

curl -X POST http://localhost:5000/build \
  -F document1=@/path/to/example-document1.pdf \
  -F document3=@/path/to/document3.pdf \
  -F instructions='{
  "parts": [
    {
      "file": "document1"
    },
    {
      "file": {
        "url": "https://pspdfkit.com/downloads/examples/paper.pdf"
      }
    },
    {
      "file": {
        "url": "https://pspdfkit.com/downloads/examples/paper.pdf"
      }
    },
    {
      "file": "document3"
    }
  ]
}' \
  -o result.pdf
POST /process HTTP/1.1
Content-Type: multipart/form-data; boundary=customboundary

--customboundary
Content-Disposition: form-data; name="document1"; filename="example-document1.pdf"
Content-Type: application/pdf

<PDF data>
--customboundary
Content-Disposition: form-data; name="document3"; filename="document3.pdf"
Content-Type: application/pdf

<PDF data>
--customboundary
Content-Disposition: form-data; name="instructions"
Content-Type: application/json

{
  "parts": [
    {
      "file": "document1"
    },
    {
      "file": {
        "url": "https://pspdfkit.com/downloads/examples/paper.pdf"
      }
    },
    {
      "file": {
        "url": "https://pspdfkit.com/downloads/examples/paper.pdf"
      }
    },
    {
      "file": "document3"
    }
  ]
}
--customboundary--

This creates a copy of the first page of a document and places it directly after the first page.