Guides Editors API Reference
Help
Documentation

Render PDF from Template

The PDF generation endpoint of our API allows you to dynamically create PDF documents based on pre-defined templates you created.

POST /api/templates/{templateId}/pdf

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

URL parameters

templateId*

The templateId parameter in the URL is a required parameter that identifies the PDF template to be used for generating the PDF. You can find the template identifier in the template index page.

Request body

data*

The data parameter is sent through the request payload. It should contain key-value pairs representing the data to be replaced in the template. The keys should match the placeholders you defined in the PDF template.

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 PDF generation endpoint. In this example, 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/{templateId}/pdf" \
-H "Content-Type: application/json" \
-H "Accept: application/pdf" \
-H "Authorization: Bearer {token}"
-d '{
    "data": {
        "name": "John Doe",
        "order_id": "ABC123",
        "total": "$ 120.00",
    }
}'

When using dynamic tables with a repeatable row, the "data" should contain an array of objects, where each object represents a row in the table. The keys in the objects should match the placeholders in the table row.

{
    "data": {
        "invoice_items": [
            { "name": "Item 1", "qty": "2", "price": "$ 120.00" },
            { "name": "Item 2", "qty": "1", "price": "$ 65.00" }
        ]
    }
}

Response

PDF Content

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.

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

PDF URL

If the output parameter is set to url, the API will respond with a JSON object containing a URL to the generated PDF file. You can use this URL to download the PDF file.

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

 

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

In case of any errors during the generation process, appropriate error codes and messages will be provided in the response.