Documentation

Merge templates

The merge templates endpoint of our API allows you to combine multiple PDF templates into a single PDF document.

POST /api/templates/merge

Headers

Authorization*

The Authorization header is used to authenticate the request. You need to provide a valid API key in the format Bearer {token}. You can generate an API token from the API Tokens page. Read more about API Authentication for detailed instructions.

Accept Default: application/pdf

The Accept header is used to specify the format of the response. The API supports the following formats:

  • application/pdf - The API will respond with a PDF file in the response body.
  • application/json - The API will respond with a JSON object containing two keys: status and data. The status key will contain the HTTP status code of the response, and the data contains the PDF file encoded as a base64 string.
Content-Type Default: application/json

The Content-Type header is used to specify the format of the request payload. This must be application/json or null

Request body

templates*

The templates parameter is sent through the request payload. It should contain an array where each element contains the identifier of the template you want to merge, aswell as the data to replace in the template. You can find the template identifiers in the template index page.

output Default: pdf

The output parameter is optional and can be used to specify the format of the response. The API supports two output formats: pdf and url. By default, this parameter is set to pdf and the API will respond with the PDF file in the response body. If the output parameter is set to url, the API will respond with a URL to the generated PDF file.

Please note that the URL expires after 15 minutes and the PDF file will be deleted from our servers.

Example Request

Here is an example of a POST request to the merge templates endpoint. In this example, we have an invoice and a shipping label template that we want to merge into a single PDF document. We are using the curl command-line tool to send a POST request to the API.

curl -X POST "https://pdf-api.io/api/templates/merge" \
     -H "Content-Type: application/json" \
     -H "Accept: application/pdf" \
     -d '{
         "templates": [
             {
                 "id": "templateId1",
                 "data": {
                     "name": "John Doe",
                     "order_id": "ABC123",
                     "total": "$ 120.00"
                 }
             },
             {
                 "id": "templateId2",
                 "data": {
                     "address": "123 Main St",
                     "city": "New York",
                     "tracking_code": "XYZ789"
                 }
             }
         ]
     }'

Response

PDF Content

The API will respond with a PDF file containing the merged content of the specified templates. The content of the response will depend on the value of the Accept header in the request. If the Accept header is set to application/pdf, the API will respond with binary data in the response body. If the Accept header is set to application/json, the API will respond with a JSON object containing the PDF file encoded as a base64 string.

JSON Response

If the Accept header is set to application/json, the API will respond with a JSON object containing two keys: status and data. The status key will contain the HTTP status code of the response, and the data contains the PDF file encoded as a base64 string.

{
    "status": 200,
    "data": "CONTENT_OF_THE_PDF_FILE"
}

URL Response

If the output parameter is set to url, the API will respond with a URL to the generated PDF file.

{
    "status": 200,
    "data": "URL_TO_PDF_FILE"
}

 

Note
The URL will expire after 15 minutes, and the PDF file will be deleted from our servers.