Add Pages to PDFs
Document Engine lets you add new pages to a document by sending a multipart request to the /api/build
endpoint(opens in a new tab) and attaching the input file(s) and the instructions
JSON. This guide provides some examples of adding new pages to PDFs.
- Ensure Document Engine is up and running.
- Send a multipart POST request(opens in a new tab) with instructions to Document Engine’s
/api/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.
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/api/build \ -H "Authorization: Token token=<API token>" \ -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 /api/build HTTP/1.1Content-Type: multipart/form-data; boundary=customboundaryAuthorization: Token token=<API token>
--customboundaryContent-Disposition: form-data; name="document"; filename="example-document.pdf"Content-Type: application/pdf
<PDF data>--customboundaryContent-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(opens in a new tab), 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(opens in a new tab) 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/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 } }, { "page": "new", "pageCount": 2 }, { "file": "document", "pages": { "start": 1, "end": 7 } } ]}' \ -o result.pdf
POST /api/build HTTP/1.1Content-Type: multipart/form-data; boundary=customboundaryAuthorization: Token token=<API token>
--customboundaryContent-Disposition: form-data; name="document"; filename="example-document.pdf"Content-Type: application/pdf
<PDF data>--customboundaryContent-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/api/build \ -H "Authorization: Token token=<API token>" \ -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 /api/build HTTP/1.1Content-Type: multipart/form-data; boundary=customboundaryAuthorization: Token token=<API token>
--customboundaryContent-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--