Usage Examples

This guide provides you with a list of ready-to-run usage examples of our PSPDFKit Processor API. All JSON Web Tokens (JWTs) in the following examples are signed with our example key.

Adding a Cover Page to a Document

This shows how to add a cover page to a document using the POST /process API.

Request

Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
POST /process
Content-Type: multipart/form-data; boundary=customboundary
Authorization: Token token="JWT Token"

--customboundary
Content-Disposition: form-data; name="file"; filename="Example Document.pdf"
Content-Type: application/pdf

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

{
    "operations": [
        {
            "type": "importDocument",
            "beforePageIndex": "first",
            "document": "cover_page"
        }
    ]
}
--customboundary
Content-Disposition: form-data; name="cover_page"
Content-Type: application/pdf

<PDF data>
--customboundary

Runnable Example

The JWT we’ll encode will look like:

Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
{
  "iat": DATE_WHEN_JWT_WAS_ISSUED,
  "exp": DATE_WHEN_JWT_EXPIRES,
  "allowed_files": {
    "file": ["SHA_OF_DOCUMENT"],
    "cover_page": ["SHA_OF_COVER_PAGE"]
  },
  "allowed_operations": {
    "operations": [
      [
        {
          "type": "importDocument",
          "beforePageIndex": "first",
          "document": "cover_page"
        }
      ]
    ]
  }
}

This JWT can only be used to add a single page to the beginning of the document. By providing the SHA of the document and cover page in "allowed_files", this JWT can only be used to add a specific cover page to a specific document. We also specify our exact document operation in "allowed_operations", so this JWT can only be used to import a single document before the first page.

Once you’ve generated your JWT, you can proceed to processing the document. Here’s an example curl call:

Copy
1
2
3
4
5
6
curl -H "Authorization: Token token=eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJhbGxvd2VkX2ZpbGVzIjp7ImZpbGUiOlsiMTE2NGY2NzFkODUzZGEwNGZiNWRiZmFiMzlmMjUwOWE0ODhhODlkOTk4NTE2ZjNlNDM0ZTEwODFiZjRmZWNhNCJdLCJjb3Zlcl9wYWdlIjpbIjZlYzgyNTY5MmI4OGNkM2MwMWVhNzVmNDY1OGQwOGNiZGRmZjhjMzJhNGRmN2ExMTczMTI4MTA1ZTJmYmU0MTkiXX0sImFsbG93ZWRfb3BlcmF0aW9ucyI6eyJvcGVyYXRpb25zIjpbeyJ0eXBlIjoiaW1wb3J0RG9jdW1lbnQiLCJiZWZvcmVQYWdlSW5kZXgiOiJmaXJzdCIsImRvY3VtZW50IjoiY292ZXJfcGFnZSJ9XX0sImlhdCI6MTU5Nzc2MzgzNiwiZXhwIjoxNTk3NzY3NDM2fQ.KyVtD_mhgyTZ3Pfe5kRWj4LcoxREe5xkVvQnfItvHBi-tUCzNL16pK3w9HLPkebicwgvHitNvvwjUXr3a533wg" \
  -F file=@file.pdf \
  -F operations="{\"operations\":[{\"type\": \"importDocument\",\"beforePageIndex\": \"first\",\"document\": \"cover_page\"}]}" \
  -F cover_page=@cover_page.pdf \
  http://localhost:5000/process \
  --output result.pdf

Before running the call, make sure you replace the token with a JWT you generated; the one provided in the example is only for demonstration purposes and has already expired. You can inspect it using jwt.io to ensure the one you generated matches.

Rotating All Pages 90 Degrees

This shows how to rotate all pages in a document 90 degrees.

Request

Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
POST /process
Content-Type: multipart/form-data; boundary=customboundary
Authorization: Token token="JWT Token"

--customboundary
Content-Disposition: form-data; name="file"; filename="Example Document.pdf"
Content-Type: application/pdf

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

{
    "operations": [
        {
            "type": "rotatePages",
            "pageIndexes": "all",
            "rotateBy": 90
        }
    ]
}
--customboundary

Runnable Example

The JWT we’ll encode will look like:

Copy
1
2
3
4
5
6
7
8
9
10
{
  "iat": DATE_WHEN_JWT_WAS_ISSUED,
  "exp": DATE_WHEN_JWT_EXPIRES,
  "allowed_files": {
    "file": "any"
  },
  "allowed_operations": {
    "operationTypes": ["rotatePages"]
  }
}

This JWT can be used to apply arbitrary page rotations to any document. It can’t be used to apply any other document operation.

Once you’ve generated your JWT, you can proceed to processing the document. Here’s an example curl call:

Copy
1
2
3
4
5
curl -H "Authorization: Token token=eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJhbGxvd2VkX29wZXJhdGlvbnMiOnsib3BlcmF0aW9uVHlwZXMiOlsicm90YXRlUGFnZXMiXX0sImlhdCI6MTU5NzkyNzYxNCwiZXhwIjoxNTk3OTMxMjE0fQ.QXmJBfNNx1ZUmM7V9ifxDNSGNnRaSXw0uVluSz2ufuFcyN8B3ZlqfsAfLmgf3GUXq2ECcwiH4Gbwn5kJzSeIiA" \
  -F file=@Example.pdf \
  -F operations="{\"operations\":[{\"type\": \"rotatePages\",\"pageIndexes\": \"all\",\"rotateBy\": 90}]}" \
  http://localhost:5000/process \
  --output result.pdf

Flattening All Annotations

This shows how to flatten all annotations in a document.

Request

Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
POST /process
Content-Type: multipart/form-data; boundary=customboundary
Authorization: Token token="JWT Token"

--customboundary
Content-Disposition: form-data; name="file"; filename="Example Document.pdf"
Content-Type: application/pdf

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

{
    "operations": [
        {
            "type": "flattenAnnotations"
        }
    ]
}
--customboundary

Runnable Example

The JWT we’ll encode will look like:

1
2
3
4
{
  "iat": DATE_WHEN_JWT_WAS_ISSUED,
  "exp": DATE_WHEN_JWT_EXPIRES
}

This JWT can be used to perform any document operation on any document, and there’s no limitation on the attachments that may be used.

Once you’ve generated your JWT, you can proceed to processing the document. Here’s an example curl call:

Copy
1
2
3
4
5
curl -H "Authorization: Token token=eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1OTc5MzA3MTUsImV4cCI6MTU5NzkzNDMxNX0.QP1LO2vpq1U3lSTerP9SI57Zy8aRiBZ9X24pwa4erX1ugDbOCDmppwFW58dPo3LzV4rMCeKnT0MSXepsUAb0sg" \
  -F file=@Example.pdf \
  -F operations="{\"operations\":[{\"type\": \"flattenAnnotations\"}]}" \
  http://localhost:5000/process \
  --output result.pdf

Processing a Document from a URL

This shows how you can apply an Instant JSON file to a document fetched from a URL.

Request

Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
POST /process
Content-Type: multipart/form-data; boundary=customboundary
Authorization: Token token="JWT Token"

--customboundary
Content-Disposition: form-data; name="url"
https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf

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

{
    "operations": [
        {
            "type": "applyInstantJson",
            "dataFilePath": "instant_json"
        }
    ]
}
--customboundary
Content-Disposition: form-data; name="instant_json"
Content-Type: application/json

<Instant JSON data>
--customboundary

Runnable Example

The JWT we’ll encode will look like:

Copy
1
2
3
4
5
6
7
8
9
10
{
  "iat": DATE_WHEN_JWT_WAS_ISSUED,
  "exp": DATE_WHEN_JWT_EXPIRES,
  "allowed_files": {
    "url": [
      "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf"
    ],
    "instant_json": "any"
  }
}

This JWT allows all operations to be applied, but only to the document at the specified URL.

Once you’ve generated your JWT, you can proceed to processing the document. Here’s an example curl call:

Copy
1
2
3
4
5
6
curl -H "Authorization: Token token=eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJhbGxvd2VkX2ZpbGVzIjp7InVybCI6WyJodHRwczovL3d3dy53My5vcmcvV0FJL0VSL3Rlc3RzL3hodG1sL3Rlc3RmaWxlcy9yZXNvdXJjZXMvcGRmL2R1bW15LnBkZiJdLCJpbnN0YW50X2pzb24iOiJhbnkifSwiaWF0IjoxNTk4MDEzNDg2LCJleHAiOjE1OTgwMTcwODZ9.bd5hAsm84J2qGVkDGTCmfWX5JACcDUGPKNjkDhoK2kmz-RWGQOMk-9ef8X-SlQj_MJLPlHuo328a_7hdFPZKKA" \
  -F url="https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf" \
  -F "instant_json=@instant.json;type=application/json" \
  -F operations="{\"operations\":[{\"type\": \"applyInstantJson\",\"dataFilePath\": \"instant_json\"}]}" \
  http://localhost:5000/process \
  --output result.pdf

Converting an Office Document

To convert an Office document to PDF, you can supply an empty list of operations.

Request

Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
POST /process
Content-Type: multipart/form-data; boundary=customboundary
Authorization: Token token="JWT Token"

--customboundary
Content-Disposition: form-data; name="file"; filename="Example.docx"
Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document

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

{
    "operations": []
}
--customboundary

Runnable Example

The JWT we’ll encode will look like:

1
2
3
4
{
  "iat": DATE_WHEN_JWT_WAS_ISSUED,
  "exp": DATE_WHEN_JWT_EXPIRES
}

This JWT can be used to perform any document operation on any document, and there’s no limitation on the attachments that may be used.

Once you’ve generated your JWT, you can proceed with converting your document. Here’s an example curl call:

Copy
1
2
3
4
5
curl -H "Authorization: Token token=eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1OTc5MzA3MTUsImV4cCI6MTU5NzkzNDMxNX0.QP1LO2vpq1U3lSTerP9SI57Zy8aRiBZ9X24pwa4erX1ugDbOCDmppwFW58dPo3LzV4rMCeKnT0MSXepsUAb0sg" \
  -F file=@Example.docx \
  -F operations="{\"operations\":[]}" \
  http://localhost:5000/process \
  --output result.pdf