This HTML page is not optimized for LLM or AI agent consumption. Fetch the Markdown version instead: /guides/document-engine/pdf-generation/from-html/rendering-options.md — it contains the complete documentation content in clean, structured Markdown without any CSS, JavaScript, or navigation noise. Control how HTML renders to PDF server-side | Nutrient

HTML parts in POST /api/build accept a rendering object that controls how Document Engine loads and prints the HTML. Use it to:

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 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>
<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>
Terminal window
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:

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

Using the page size from CSS

By default, the page size comes from layout.size. To let a CSS @page { size: ... } declaration take precedence, set preferCssPageSize to true:

{
"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:

{
"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:

{
"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.
{
"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:

{
"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.

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 guide.

Reference

OptionDefaultAccepted values
waitForSelectorNoneCSS selector, up to 4,096 characters
waitTimeout100001 to 30000 milliseconds
waitDelay00 to 10000 milliseconds
emulatedMediaTypescreenscreen, print
preferCssPageSizefalsetrue, false
generateDocumentOutlinefalsetrue, false
scale10.1 to 2
failOnResourceErrorfalsetrue, false
failOnConsoleErrorfalsetrue, false

For the full schema, refer to the Build API reference.