New feature HTML to PDF  Create templates with HTML, read the announcement 

Guides

Generate a PDF using the API

Welcome to our guide on generating a PDF document using our API. This page will walk you through the process of creating a PDF from your custom template, authenticating with our API, and sending your first request. Whether you're a seasoned developer or new to API integrations, this guide is designed to provide you with a straightforward path to success.

Prerequisites

To follow this guide, you'll need to sign up for a PDF-API.io account. If you don't have an account yet, you can create one for free.

Step 1: Create Your PDF Template

Before you can generate a PDF, you need to design your template using our visual tool. This template can include static elements as well as variables for dynamic data.

Note
When creating a new template, you can select from a variety of pre-built templates or start from scratch.

Go to the Template Designer: Log in to your account and go to the "Templates" page. Here you can create a new template or edit an existing one.

Design Your Template: Use the drag-and-drop interface to design your PDF template. You can add text, images, and other elements as needed.

Define Variables:

Identify the parts of your template where dynamic data will be inserted. Assign unique variable names to these sections for easy reference. You can use the variable names in the template designer by wrapping them in curly braces, like this: {variable_name}. For example, if you want to insert the customer's name in your template, you can add a variable named customer_name and use it in the template like this: Hello {customer_name}.

If you want to use a variable in a table, you can use the same syntax. For example, if you want to display a list of items in a table, you can set a table row to be repeatable, give the table row a variable name, and then use the variable name in the table cells.

Item Name Quantity Price
{items.name} {items.qty} {items.price}
Total {total}

You can read more about using variables in our template designer documentation.

Step 2: Authenticate with the API

To ensure secure communication between your application and our API, you'll need to authenticate each request.

  1. Obtain Your API Key: Go to the "API Tokens" page and create an API key. Keep this key confidential. You will not be able to view this key again after generating it.
  2. Include Your API Key in every Requests: When making requests to our API, include this key in the header for authentication. The header should look like this: Authorization: Bearer YOUR_API_KEY.
curl -X POST "https://pdf-api.io/api/{endpoint}" \
     -H "Content-Type: application/json" \
     -H "Authorization: Bearer {token}"

Step 3: Send Your First Request

Now that you have your template and API key, you're ready to generate a PDF.

Prepare Your Data: Gather the dynamic data that will populate the variables in your template.

Create the Request: Construct an HTTP POST request to our API's PDF generation endpoint. The request should include: - The ID of your template. You can find this ID in the template index page. - The dynamic data in JSON format.

Send the Request:

Use a tool like cURL or Postman, or write a script in your preferred programming language to send the request. Here's a basic example using cURL:

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",
         }
     }'
Note
In this example, we're sending a POST request to the /api/templates/{templateId}/pdf endpoint. We're also including the Accept: application/pdf header to indicate that we want to receive the response as a binary PDF file. If you prefer to receive the response as JSON, you can set the Accept header to application/json instead. **This will return a JSON object with the contents of the PDF as a base64-encoded string.**

Conclusion

Congratulations! You've just learned how to create a PDF using our API. This process can be customized and automated to fit your specific needs. For further assistance or to explore more advanced features, please refer to our comprehensive API documentation or contact our support team.