Filling PDF templates

Once you have created a template with input fields, you can fill this template in three different ways to generate a PDF document:

  1. Fill it via API
  2. Fill it via the fomular on the wunderdocs web
  3. Use an integration to automate your workflow

Every time you fill a template, a new document is generated. The document is a snapshot of the template at the time of filling. If you change the template after filling it, the document will not be affected.

Working with template fields

Everyone of the mentioned methods above requires you to provide the data for the template fields. To list the fields of a template, you can use the Retrieve template fields endpoint. The response will look like this:

{
  "fields": [
    {
      "label": "First Name",
      "key": "firstName",
      "type": "string"
    },
    {
      "label": "Last Name",
      "key": "lastName",
      "type": "string"
    },
    {
      "label": "Age",
      "key": "age",
      "type": "number"
    },
    {
      "label": "Married",
      "key": "isMarried",
      "type": "boolean"
    }
  ]
}

The data is provided as a JSON object. The keys of the object must match the keys of the template fields. If they don't match, the field will not be filled. For every field you want to fill you need to provide a key-value pair. For example, if you have a field with the key firstName, you need to provide a key-value pair with the key firstName and the value John.

{
  "firstName": "John",
  "isMarried": true,
  "age": 42
}

If you don't provide to correct type of data, the field will not be filled. For example, if you have a field with the key age and the type number, you need to provide a number as value. If you provide a string, the field will not be filled.


POST/v1/templates/:uid

Fill a template via API

To fill a template via API, you need to send a POST request to the following endpoint: /v1/templates/:uid. The :uid parameter is the unique identifier of the template you want to fill. You can find the template uid in the template list or in the template details.

In the response you will get the uid of the generated document and the download url. You can use the download url to download the generated document.

Properties

  • Name
    label
    Type
    string
    Description

    A human readable name for the field.

  • Name
    key
    Type
    string
    Description

    The unique identifier for the field. Currently this is the same as the label.

  • Name
    type
    Type
    text/number/checkbox
    Description

    The ID of the user who created the template.

Example payload

{
  "title": "My new Document",
  "data": {
    "firstName": "John",
    "lastName": "Doe",
    "isMarried": true,
    "age": 42
  }
}

Request

POST
/v1/templates/:uid
curl -X POST  https://api.wunderdocs.io/v1/templates/WAz8eIbvDR60rouK \
  -H "X-API-KEY: {api-key}" \
  -H "Content-Type: application/json" \
  -d '{ "title": "My new Document", "data": {"firstName": "John", "lastName": "Doe", "isMarried": true, "age": 42}}'

Response

{
  "uid": "d531d102-ebeb-48b2-8caf-08e23bd2942f",
  "name": "My Document",
  "templateName": "Untitled (1)",
  "downloadUrl": "http://api.wunderdocs.io/documents/d531d102-ebeb-48b2-8caf-08e23bd2942f/download"
}

Handling the response

By default the reponse will return a JSON object with the following properties:

Properties

  • Name
    uid
    Type
    string
    Description

    The unique identifier of the document.

  • Name
    name
    Type
    string
    Description

    The name of the document.

  • Name
    templateName
    Type
    string
    Description

    The name of the template.

  • Name
    downloadUrl
    Type
    string
    Description

    The download url of the document.

Response

{
  "uid": "d531d102-ebeb-48b2-8caf-08e23bd2942f",
  "name": "My Document",
  "templateName": "Untitled (1)",
  "downloadUrl": "http://api.wunderdocs.io/documents/d531d102-ebeb-48b2-8caf-08e23bd2942f/download"
}

Since the downloadUrl will return a buffer, you can use the link to create follow up actions. For example, you can use the link to send the document via email or to upload it to a cloud storage.

You can use tools like zapier to automate your workflow. Just inlude the downloadUrl in your zapier workflow and you can use it as a trigger for your next action.

Furthermore you can easyly integrate the download url in your application. Just use the url as a link in your application and the document will be downloaded or attached to the email.

Attach Document to Mail

  const nodemailer = require('nodemailer');

  const transporter = nodemailer.createTransport();

  await transporter.sendMail({
      from: 'your_email@gmail.com',
      to: 'recipient@example.com', 
      subject: 'Document Attachment', 
      text: 'Please find attached document.', 
      attachments: [
          {
              filename: 'document.pdf',
              path: response.data.downloadUrl
          }
      ]
  });

However, if you need to stream the response directly into your application to work further with it, you can use the query parameter responseType to return it as a binary instead of a structured json. This will return the document as a buffer.

Optional attributes

  • Name
    responseType
    Type
    string
    Description

    The response type of the request. Can be either json or binary. Default is json.

Request

POST
/v1/templates/:uid?responseType=binary
curl -X POST  https://api.wunderdocs.io/v1/templates/WAz8eIbvDR60rouK?responseType=binary \
  -H "X-API-KEY: {api-key}" \
  -H "Content-Type: application/json" \
  -d '{ "title": "My new Document", "data": {"firstName": "John", "lastName": "Doe", "isMarried": true, "age": 42}}'