Add Pages to PDFs 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 add new pages to a document by sending a multipart request to the /build endpoint and attaching the input file(s) and the instructions JSON. This guide provides some examples of adding new pages to PDFs.

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.

Adding a Page to a File on Disk

The instructions in this example will insert a new A4-size page after the first page of the document:

curl -X POST http://localhost:5000/build \
  -F document=@/path/to/example-document.pdf \
  -F instructions='{
  "parts": [
    {
      "file": "document",
      "actions": [
        {
          "type": "insertPage",
          "afterPageIndex": 0,
          "pageWidth": 595,
          "pageHeight": 842
        }
      ]
    }
  ]
}' \
  -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",
      "actions": [
        {
          "type": "insertPage",
          "afterPageIndex": 0,
          "pageWidth": 595,
          "pageHeight": 842
        }
      ]
    }
  ]
}
--customboundary--

You can also specify the beforePageIndex option for the insertPage action, but note that either afterPageIndex or beforePageIndex is required, and you cannot provide both. /build will return error messages if you attempt a request specifying both options for an insertPage action.

You can also add a new page using the NewPagePart action instead of the insertPage action.

This example adds two blank pages after the first page of the document.

The instructions in this example will insert two new A4 pages after the first page of the document:

curl -X POST http://localhost:5000/build \
  -F document=@/path/to/example-document.pdf \
  -F instructions='{
  "parts": [
    {
      "file": "document",
      "pages": {
        "start": 0,
        "end": 0
      }
    },
    {
      "page": "new",
      "pageCount": 2
    },
    {
      "file": "document",
      "pages": {
        "start": 1,
        "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
      }
    },
    {
      "page": "new",
      "pageCount": 2
    },
    {
      "file": "document",
      "pages": {
        "start": 1,
        "end": 7
      }
    }
  ]
}
--customboundary--

Adding a Page to a File from a URL

To add an A4 page after the first page of the document at the provided URL, use the following code example:

curl -X POST http://localhost:5000/build \
  -F instructions='{
  "parts": [
    {
      "file": {
        "url": "https://pspdfkit.com/downloads/examples/paper.pdf"
      },
      "actions": [
        {
          "type": "insertPage",
          "afterPageIndex": 0,
          "pageWidth": 595,
          "pageHeight": 842
        }
      ]
    }
  ]
}' \
  -o result.pdf
POST /process HTTP/1.1
Content-Type: multipart/form-data; boundary=customboundary

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

{
  "parts": [
    {
      "file": {
        "url": "https://pspdfkit.com/downloads/examples/paper.pdf"
      },
      "actions": [
        {
          "type": "insertPage",
          "afterPageIndex": 0,
          "pageWidth": 595,
          "pageHeight": 842
        }
      ]
    }
  ]
}
--customboundary--