HTML rendering options
HTML parts in POST /api/build accept a rendering object that controls how Document Engine loads and prints the HTML. Use it to:
- Wait for content that JavaScript renders, such as charts.
- Apply print styles.
- Use the page size declared in CSS.
- Turn headings into bookmarks.
- Scale the page content.
- Fail the request when a resource or script breaks 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 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>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.pdfDocument 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.
waitTimeoutlimits the wait, in milliseconds. It defaults to10000and accepts up to30000.waitDelayadds a fixed pause before printing, in milliseconds. It defaults to0and accepts up to10000. PreferwaitForSelector, 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:
failOnResourceErrorfails 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.failOnConsoleErrorfails the request when a script throws an uncaught exception or callsconsole.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.
Using the options with the header and footer elements
The pspdfkit-header and pspdfkit-footer elements limit some of these options:
scalevalues other than1andpreferCssPageSizecan’t be used with them. The request fails with a400error.- The document outline from
generateDocumentOutlineisn’t kept.
CSS @page margin boxes don’t have these limits. Refer to the page header and footer 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.