> For the complete documentation index, see [llms.txt](https://docs.appfarm.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.appfarm.io/library/action-nodes/generate-document.md).

# Generate document

**Generate Document** lets you generate a PDF document that can be saved to a database-connected File Data Source. The file is generated by a **Template String** written in HTML. The Template String can be set using a **Data Binding**, a **Text Editor** or a **Function**. If using a Function, parameters can be added using `${parameterName}`.

Generate Document is available in Apps, Services and Flows.

Generating a document can take at most 120 seconds. If it takes longer, the action node fails.

If you are writing your own HTML for the document it might be useful to preview the result. This can be done by storing the code in its own file with the file extension *.html* and then opening the document with your browser.

{% hint style="info" %}
Showroom: [Generate document example](https://showroom.appfarm.app/action-nodes/generate-document) and [Advanced PDF example](https://showroom.appfarm.app/use-cases/advanced-pdf), with their setup in Appfarm Create ([register](https://showroom.appfarm.app/sign-up) for access).
{% endhint %}

**You may also use the HTML templates found in the Template String setting of the Generate Document actions in Create**.

## Properties

| Property             | Description                                                                                                                                                                                                                             |
| -------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **Document type**    | The type of document to generate. `PDF` is the only option.                                                                                                                                                                             |
| **File name**        | The file name users see when they download the document, stored as the original file name. `.pdf` is added if missing. Defaults to `filename.pdf` when empty.                                                                           |
| **Orientation**      | `Portrait` (default) or `Landscape`.                                                                                                                                                                                                    |
| **Page format**      | Letter, Legal, Tabloid, Ledger, or A0 to A6. Defaults to A4 (210 x 297 mm).                                                                                                                                                             |
| **Margin top**       | The top page margin, in millimeters.                                                                                                                                                                                                    |
| **Margin bottom**    | The bottom page margin, in millimeters.                                                                                                                                                                                                 |
| **Margin left**      | The left page margin, in millimeters.                                                                                                                                                                                                   |
| **Margin right**     | The right page margin, in millimeters.                                                                                                                                                                                                  |
| **Template string**  | The HTML that becomes the document content. Images are supported. Insert `{{{PAGE_BREAK}}}` to force a page break.                                                                                                                      |
| **Optimize images**  | When enabled, images are embedded at a reduced, screen-friendly resolution, so large or many images don't slow down or time out PDF generation. Off by default, which embeds images at full resolution. Can be data bound to a Boolean. |
| **Data source**      | The data source, based on a file object class, to store the generated document in.                                                                                                                                                      |
| **Public**           | Make the file accessible to unauthenticated users. Links to public files do not expire, so don't use this for sensitive documents.                                                                                                      |
| **Set selected**     | Shown for many-cardinality data sources. Selects the generated file in the data source.                                                                                                                                                 |
| **Values on create** | Set values for object class properties on the generated file object.                                                                                                                                                                    |

### Short about HTML elements:

* `<html>…</html>`: The root element of the HTML document that all other elements are contained in.
* `<head>…</head>`: The container element for processing information and metadata in the document. This container can include elements, as, for example `<style>…</style>`, which specifies the CSS style for the document. `<title>…</title>` specifies the document title and `<meta>…</meta>` can be used to specify additional metadata for the document.
* `<body>…</body>`: The container element for the visible content of the document. There is a long list of elements that can be used to generate the body of the document. Some elements are listed below:\
  – `<div>…</div>`: a generic container element with no semantic meaning\
  – `<p>…</p>`: a paragraph\
  – `<h1>…</h1> - <h6>…</h6>`: section headings, where `h1` is the highest level heading, while `h6` is the lowest-level heading.\
  – `<ol>…</ol>` ordered list or `<ul>…</ul>` unordered list, with `<li>…</li>` as list elements.\
  – `<table>…</table>` a table with `<tr>…</tr>` table rows, `<th>…</th>` table header cells and `<td>…</td>` table cells.\
  – `<img>…</img>`: an image tag used to display images.

{% file src="/files/qhvWO8AZwUCT68pXSMgR" %}
Example of a simple HTML Template String for the Generate Document action node. This example is also found in Showroom.
{% endfile %}

### Background graphics

Printing of background colors and graphics from webpages is typically turned off by default. The same applies when using Generate Document. In order for background colors and graphics to be included in your PDF, you can add the following code to your document HTML. Note that even though the styling is applied to the `<body>` element, it will apply only to its descendants.

{% code collapsedlinecount="10" %}

```
<style>
  body {
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }
</style>
```

{% endcode %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.appfarm.io/library/action-nodes/generate-document.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
