---
title: "Control how HTML renders to PDF server-side | Nutrient"
canonical_url: "https://www.nutrient.io/guides/document-engine/pdf-generation/from-html/rendering-options/"
md_url: "https://www.nutrient.io/guides/document-engine/pdf-generation/from-html/rendering-options.md"
last_updated: "2026-12-01T00:00:00.000Z"
description: "Wait for JavaScript content, apply print styles, add bookmarks, and fail on broken resources when Document Engine generates a PDF from HTML."
---

# HTML rendering options

HTML parts in [`POST /api/build`](https://www.nutrient.io/api/reference/document-engine/upstream/#tag/Build-API/Instructions-Schema) accept a `rendering` object that controls how Document Engine loads and prints the HTML. Use it to:

- [Wait for content that JavaScript renders](#waiting-for-javascript-content), such as charts.

- [Apply print styles](#applying-print-styles).

- [Use the page size declared in CSS](#using-the-page-size-from-css).

- [Turn headings into bookmarks](#adding-bookmarks-from-headings).

- [Scale the page content](#scaling-the-page-content).

- [Fail the request when a resource or script breaks](#failing-on-broken-resources-and-script-errors) instead of returning an incomplete PDF.

The `rendering` object is available from Document Engine 1.21. Requests without it render as before.

For running headers, footers, and page numbers, refer to the [page header and footer](https://www.nutrient.io/guides/document-engine/pdf-generation/from-html/page-header-footer.md) guide.

## Waiting for JavaScript content

Document Engine prints the page once the `load` event fires. Content that a script draws after that, such as a chart, would print in its loading state. To print it once it’s ready, have the page’s script add a marker when it’s done, and wait for that marker with `waitForSelector`:

```html
<html>
  <body>
    <h1>Summary</h1>
    <p id="chart">Loading…</p>
    <script>
      setTimeout(() => {
        document.getElementById("chart").textContent = "Chart drawn";
        document.body.dataset.pdfReady = "true";
      }, 600);
    </script>
  </body>
</html>
```

```shell
curl -X POST http://localhost:5000/api/build \
  -F page.html=@/path/to/page.html \
  -F instructions='{
  "parts": [
    {
      "html": "page.html",
      "rendering": {
        "waitForSelector": "[data-pdf-ready=\"true\"]"
      }
    }
  ]
}' \
  --output result.pdf
```

Document Engine checks the selector every 100 milliseconds after the page loads and prints once an element matches it. If the selector is invalid, or nothing matches within `waitTimeout`, the request fails with a `400` error instead of returning an incomplete PDF.

- `waitTimeout` limits the wait, in milliseconds. It defaults to `10000` and accepts up to `30000`.

- `waitDelay` adds a fixed pause before printing, in milliseconds. It defaults to `0` and accepts up to `10000`. Prefer `waitForSelector`, which doesn’t wait longer than needed.

Document Engine loads the HTML at least twice, and three times when it uses the `pspdfkit-header` or `pspdfkit-footer` elements. The wait applies to each load, so a `waitDelay` of `10000` adds at least 20 seconds. All loads share the request’s overall time limit, and a wait is cut short when that limit is close.

Document Engine doesn’t accept `waitForExpression`, which other HTML-to-PDF tools use to run a JavaScript expression in the page. A request that sets it fails with a `400` error. Use `waitForSelector` with a marker instead.

## Applying print styles

Pages render with `screen` media by default. To apply your `@media print` rules, set `emulatedMediaType` to `print`:

```json
{
  "parts": [
    {
      "html": "page.html",
      "rendering": {
        "emulatedMediaType": "print"
      }
    }
  ]
}
```

## Using the page size from CSS

By default, the page size comes from [`layout.size`](https://www.nutrient.io/guides/document-engine/pdf-generation/from-html/schema/#page-layout). To let a CSS `@page { size: ... }` declaration take precedence, set `preferCssPageSize` to `true`:

```json
{
  "parts": [
    {
      "html": "page.html",
      "rendering": {
        "preferCssPageSize": true
      }
    }
  ]
}
```

## Adding bookmarks from headings

To turn the headings in the HTML into PDF bookmarks, set `generateDocumentOutline` to `true`:

```json
{
  "parts": [
    {
      "html": "page.html",
      "rendering": {
        "generateDocumentOutline": true
      }
    }
  ]
}
```

## Scaling the page content

`scale` scales the rendered page content. It defaults to `1` and accepts values from `0.1` to `2`:

```json
{
  "parts": [
    {
      "html": "page.html",
      "rendering": {
        "scale": 0.8
      }
    }
  ]
}
```

## Failing on broken resources and script errors

By default, an HTML part produces a PDF even when an image returns `404`, a stylesheet is blocked, or a script throws, and the PDF is missing that content. Two options make those problems fail the request instead:

- `failOnResourceError` fails the request when a resource the page loads — such as an image, stylesheet, font, or script — responds with an HTTP status of 400 or above, fails to load, or is blocked, for example because remote assets are disabled.

- `failOnConsoleError` fails the request when a script throws an uncaught exception or calls `console.error`.

```json
{
  "parts": [
    {
      "html": "page.html",
      "assets": ["logo.png"],
      "rendering": {
        "failOnResourceError": true,
        "failOnConsoleError": true
      }
    }
  ]
}
```

The request fails with a `400` error, and `failingPaths` lists each resource URL with its reason, or each error message. For example, a font the HTML references but the request doesn’t upload is listed like this:

```json
{
  "path": "$.instructions.parts[0].rendering.failOnResourceError",
  "details": "OpenSans-Regular.ttf failed to load: net::ERR_FILE_NOT_FOUND"
}
```

At most 20 errors are listed, so a page with more errors can fail again after you fix those.

Both options are off by default.

## Using the options with the header and footer elements

The `pspdfkit-header` and `pspdfkit-footer` elements limit some of these options:

- `scale` values other than `1` and `preferCssPageSize` can’t be used with them. The request fails with a `400` error.

- The document outline from `generateDocumentOutline` isn’t kept.

CSS `@page` margin boxes don’t have these limits. Refer to the [page header and footer](https://www.nutrient.io/guides/document-engine/pdf-generation/from-html/page-header-footer.md) guide.

## Reference

| Option                    | Default  | Accepted values                      |
| ------------------------- | -------- | ------------------------------------ |
| `waitForSelector`         | None     | CSS selector, up to 4,096 characters |
| `waitTimeout`             | `10000`  | `1` to `30000` milliseconds          |
| `waitDelay`               | `0`      | `0` to `10000` milliseconds          |
| `emulatedMediaType`       | `screen` | `screen`, `print`                    |
| `preferCssPageSize`       | `false`  | `true`, `false`                      |
| `generateDocumentOutline` | `false`  | `true`, `false`                      |
| `scale`                   | `1`      | `0.1` to `2`                         |
| `failOnResourceError`     | `false`  | `true`, `false`                      |
| `failOnConsoleError`      | `false`  | `true`, `false`                      |

For the full schema, refer to the [Build API reference](https://www.nutrient.io/api/reference/document-engine/upstream/#tag/Build-API/Instructions-Schema).
---

## Related pages

- [Generate a blank PDF](/guides/document-engine/pdf-generation/from-html/blank-pdf.md)
- [Edit a generated PDF](/guides/document-engine/pdf-generation/from-html/edit-a-generated-pdf.md)
- [Generate fillable PDF forms from HTML](/guides/document-engine/pdf-generation/from-html/fillable-pdf-forms.md)
- [Create PDFs from scratch](/guides/document-engine/pdf-generation/from-html/from-scratch.md)
- [Java](/guides/document-engine/pdf-generation/from-html/java.md)
- [JavaScript](/guides/document-engine/pdf-generation/from-html/javascript.md)
- [Customize the page header and footer](/guides/document-engine/pdf-generation/from-html/page-header-footer.md)
- [PHP](/guides/document-engine/pdf-generation/from-html/php.md)
- [Python](/guides/document-engine/pdf-generation/from-html/python.md)
- [HTML-to-PDF conversion server sample code](/guides/document-engine/pdf-generation/from-html/sample-code.md)
- [HTML-to-PDF generation schema](/guides/document-engine/pdf-generation/from-html/schema.md)
- [HTML template design for generating PDFs](/guides/document-engine/pdf-generation/from-html/template-design.md)

