Duplicate PDF Pages

Document Engine lets you duplicate pages of a document using the /build endpoint.

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

You’ll be sending multipart POST requests with instructions to Document Engine’s /api/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 /api/build endpoint and all the actions you can perform on PDFs with Document Engine.

Duplicating Pages of a File on Disk

Send a multipart request to the /api/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/api/build \
  -H "Authorization: Token token=<API token>" \
  -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 /api/build HTTP/1.1
Content-Type: multipart/form-data; boundary=customboundary
Authorization: Token token=<API token>

--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/api/build \
  -H "Authorization: Token token=<API token>" \
  -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 /api/build HTTP/1.1
Content-Type: multipart/form-data; boundary=customboundary
Authorization: Token token=<API token>

--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/api/build \
  -H "Authorization: Token token=<API token>" \
  -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 /api/build HTTP/1.1
Content-Type: multipart/form-data; boundary=customboundary
Authorization: Token token=<API token>

--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.