# Form Filling

> Fill the fields of a PDF form with your values and download the filled PDF.

A **Form Filling** writes your values into the Form Fields of a PDF: the interactive fields a person would type into, tick or pick from. Its Output is the filled PDF, downloaded from the Form Filling; the Document itself never changes.

<Mermaid
  chart="`
flowchart LR
  A[&#x22;Document<br/><small>e.g. application.pdf</small>&#x22;] --> B[&#x22;Form Filling<br/><small>+ your values</small>&#x22;]
  B --> C[&#x22;Filled PDF<br/><small>application.pdf</small>&#x22;]
`"
/>

* **Your values**: you say what goes into each Form Field, by its name. No language model reads or fills the Document.
* **Every field type**: text fields, checkboxes, radio groups, combo boxes and lists, single or multiple choice.
* **Fillable or flat**: the filled PDF keeps its form, or with `flatten` the values become part of the pages.

***

## 🏗️ Configuration

A Form Filling is a File Operation (see [Jobs](https://docs.axelered.com/jobs_guide)): it fills the original file and never parses it.

| Parameter | Type      | Default  | Description                                                                                         |
| :-------- | :-------- | :------- | :-------------------------------------------------------------------------------------------------- |
| `values`  | `object`  | required | The value of each Form Field to fill, by its full name, e.g. `applicant.address.zip`. At least one. |
| `flatten` | `boolean` | `false`  | Draw the Form Fields into the pages: the filled PDF can no longer be filled or edited.              |

A Form Filling takes no `perimeter`: it always runs inside Axelered's infrastructure, so the [Compliance Guard](https://docs.axelered.com/config_guide#-compliance-guard) never refuses it. It costs one Credit when `done`.

***

## ✍️ Values

A Form Field is named by its full name: the names of its parents and its own, joined by dots. Form Fields that share a name share one value, so filling the name fills every place it appears on the pages. The Form Fields you leave out keep the value they have in the Document: fill a form in several passes, or only the fields you know.

Each type of Form Field takes one type of value:

| Form Field                     | Value                                  | Example           |
| :----------------------------- | :------------------------------------- | :---------------- |
| Text field                     | A string                               | `"Ada Lovelace"`  |
| Checkbox                       | `true` to tick it, `false` to clear it | `true`            |
| Radio group                    | The name of one of its buttons         | `"large"`         |
| Combo box or list              | One of its exported values             | `"IT"`            |
| Editable combo box             | Any string, one of its values or not   | `"Lyon"`          |
| Multiple-choice list           | An array of its exported values        | `["red", "blue"]` |
| Push button or signature field | None: they cannot be filled            |                   |

Values are never converted: `1` is not `true`, and `20121` is not `"20121"`. A number, `null` or an object is no value of any Form Field, and is refused as an invalid request, `422 H_422`. A choice is picked by its exported value, which can differ from the text it displays: a country list may display `Italy` and export `IT`.

Every value is checked at submit, before anything is uploaded or created. A name no Form Field has, or a value of the right type that its Form Field does not take, such as a choice it does not offer, is refused with `422 invalid_form_values`, whose `detail` names every one of them.

### Knowing the Form Fields

To learn what a form takes, [read the Form Fields](https://docs.axelered.com/reference/documents/api_form_fields_documents__document_id__form_fields_get) of its Document. They come in the order of the form, each with the value it holds in the PDF and what it takes:

```bash
curl "https://idp.axelered.com/documents/{document_id}/form-fields" \
  -H "Authorization: Bearer YOUR_API_KEY"
```

```json
[
  {
    "name": "country",
    "kind": "choice",
    "value": "IT",
    "options": [
      { "value": "IT", "label": "Italy" },
      { "value": "FR", "label": "France" }
    ],
    "boxes": [{ "pageIndex": 0, "bbox": [0.1176, 0.2551, 0.4902, 0.2803] }]
  }
]
```

| Field     | Description                                                                                                                                            |
| :-------- | :----------------------------------------------------------------------------------------------------------------------------------------------------- |
| `name`    | The full name: the key of its value in `values`.                                                                                                       |
| `kind`    | `text`, `checkbox`, `radio`, `choice`, `editable_choice`, `multi_choice` or `signature`, as in the table above. A `signature` is listed, never filled. |
| `value`   | The value in the PDF, as a Form Filling writes it. When it holds none: `false` for a checkbox, `[]` for a multiple-choice list, `null` for the others. |
| `options` | The buttons of a radio group, or the options of a choice: the exported `value` to fill and the `label` the form displays.                              |
| `boxes`   | Where it appears: the index of its Page and its box as fractions of the Page as shown, from its top left corner, one for each place or button.         |

Reading is free and refuses what the Form Filling refuses, with the same errors (see [Supported Forms](#-supported-forms)). What it lists is what the Form Filling takes: fill the names it returns with values of their `kind`.

***

## 📄 Supported Forms

Form Filling fills interactive PDF forms (AcroForm). Any other file type is refused at submit with `400 unsupported_format`, a corrupt PDF with `400 unreadable_file`, and these PDFs with `422 unsupported_form`, whose `detail` says which case it is:

| PDF            | Why                                                                                  |
| :------------- | :----------------------------------------------------------------------------------- |
| No Form Fields | A flat or scanned form has nothing to fill: its blanks are only drawn on the page.   |
| XFA form       | An XML form that only some viewers show; only AcroForm forms are filled.             |
| Signed         | Filling it would invalidate its signatures. An empty signature field is fine.        |
| Encrypted      | Protected by a password, even only to restrict editing: remove the protection first. |

### Fonts

Each Form Field is drawn with its own font, the one set by the author of the form. A character missing from that font, such as a letter of another alphabet in a form made for Latin text, may show empty or broken. The filled PDF asks viewers to redraw its Form Fields, and most do with a font that has the character. A flattened PDF cannot be redrawn, and its choices are drawn in Helvetica: flatten only values the fonts of the form can show.

### Flatten

With `flatten`, the Form Fields become part of the pages: the filled PDF looks nearly the same, but has no form left, so no one can change its values. A page cannot scroll, so a combo box or a list is drawn as the displayed text of what it holds, `Italy` and not `IT`: a multiple-choice list shows its picked values, separated by commas.

***

## 🛠️ Fill a Form

For every field and parameter, see the [**Form Fillings reference**](https://docs.axelered.com/reference#form-fillings).

### [Submit a Form Filling](https://docs.axelered.com/reference/form-fillings/api_create_form_fillings_post)

```bash
curl -X POST "https://idp.axelered.com/form-fillings?wait=30" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -F "file=@/path/to/application.pdf" \
  -F 'config={ "values": { "name": "Ada Lovelace", "privacy": true, "size": "large", "colors": ["red", "blue"] } }'
```

```json
{
  "status": "done",
  "config": {
    "values": {
      "name": "Ada Lovelace",
      "privacy": true,
      "size": "large",
      "colors": ["red", "blue"]
    },
    "flatten": false
  },
  "credits": 1
}
```

### [Download the filled PDF](https://docs.axelered.com/reference/form-fillings/api_output_form_fillings__job_id__output_get)

```bash
curl -OJ "https://idp.axelered.com/form-fillings/{form_filling_id}/output" \
  -H "Authorization: Bearer YOUR_API_KEY"
```

The file is named like the Document. The download answers `425 output_not_ready` while the Form Filling runs, and `404` when it failed.

The filled PDF is not a Document: to parse it or run an Operation on it, upload it as a new Document.

### Fill from another Document

To fill a form with the data of another Document, such as an application from an ID card, compose an [Extraction](https://docs.axelered.com/extract) with a Form Filling. Give the Extraction an `outputSchema` whose properties are the names of the Form Fields, with the types of their values, then submit its `output.value` as the `values` of the Form Filling:

```json
{
  "type": "object",
  "properties": {
    "name": { "type": "string" },
    "privacy": { "type": "boolean" },
    "size": { "type": "string", "enum": ["small", "large"] }
  }
}
```

An `enum` of the exported values keeps the model to the choices the form takes. Leave out of `values` what the Extraction returned as `null`: no Form Field takes it. Each Job runs and is billed on its own.

### List, Read & Delete

* [**List Form Fillings**](https://docs.axelered.com/reference/form-fillings/api_list_form_fillings_get): newest first, optionally for one Document with `?documentId=`.
* [**Read Form Filling**](https://docs.axelered.com/reference/form-fillings/api_read_form_fillings__job_id__get): status, credits and configuration, with optional `wait`.
* [**Delete Form Filling**](https://docs.axelered.com/reference/form-fillings/api_delete_form_fillings__job_id__delete): hide a Form Filling. Its Document is kept.
