Duplicate PDF Pages

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

For more information, refer to the API reference to learn about the /api/build endpoint and all the actions you can perform on PDFs with Document Engine.

For an overview of multipart requests, refer to the brief tour of multipart requests blog post.

Duplicating Pages of a File on Disk

Send a multipart request to the /api/build endpoint(opens in a new tab), 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.

Terminal window
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

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(opens in a new tab).

Terminal window
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

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:

Terminal window
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

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