Merge Multiple PDF Files

PSPDFKit Document Engine lets you merge multiple files together by sending requests to the /api/build endpoint. This is useful when you want to concatenate multiple documents and create a large document or add cover pages to an existing document.

Before you get started, make sure Document Engine 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 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 PSPDFKit Document Engine.

Merging a Document into a File on Disk

Send a multipart request to the /api/build endpoint, attaching an input file and the instructions JSON. This example will add a cover page to the document in the request by merging both documents together. Note that the order of the parts in the instructions corresponds to the order each part of the document will appear in in the final combined PDF after merging is complete.

curl -X POST http://localhost:5000/api/build \
  -H "Authorization: Token token=<API token>" \
  -F document=@/path/to/document.pdf \
  -F cover=@/path/to/cover.pdf \
  -F instructions='{
  "parts": [
    {
      "file": "cover"
    },
    {
      "file": "document"
    }
  ]
}' \
  -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="document.pdf"
Content-Type: application/pdf

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

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

{
  "parts": [
    {
      "file": "cover"
    },
    {
      "file": "document"
    }
  ]
}
--customboundary--

Merging a Document into a File from a URL

Send a request to the /api/build endpoint, attaching a URL pointing to an input file and the instructions JSON:

curl -X POST http://localhost:5000/api/build \
  -H "Authorization: Token token=<API token>" \
  -F cover=@/path/to/cover.pdf \
  -F instructions='{
  "parts": [
    {
      "file": "cover"
    },
    {
      "file": {
        "url": "https://pspdfkit.com/downloads/examples/paper.pdf"
      }
    }
  ]
}' \
  -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="cover"; filename="cover.pdf"
Content-Type: application/pdf

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

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

The code above adds a cover page to the document downloaded from the URL.