PDF client
The browser-safe client and the wire contract. Rendering and PDF in the DocStudio developer reference.
Public from docstudio/pdf: createPdfClient, validatePdfRequest, MIN_TIMEOUT_MS, MAX_TIMEOUT_MS and the types PdfRequest, PdfJobOptions, PdfResult, PdfClient, PdfClientOptions, FetchLike, ResponseLike, RequestValidation. The module is browser safe: it imports only core, uses no Node built-ins and no Playwright, and performs network access only through a fetch function: the one the host injects, or the global fetch when the option is omitted.
Wire contract
The client sends POST <endpoint> with content-type: application/json and accept: application/pdf. The body is { "rdt": <resolved document tree>, "options"?: { "title"?, "lang"?, "timeoutMs"? } }. The service renders the tree with the renderer and returns:
200withapplication/pdfbytes and an optionalx-docstudio-pagesheader with the page count;- an error status (
400invalid request,413too large,429queue full,503unavailable,504timeout,500render failure) with a JSON body{ "ok": false, "diagnostics": [ ... ] }.
The tree is the output of compile (rdt-and-compile.md); compilation happens on the host or in the browser, rendering on the service. Assets and fonts are resolved on the service from identifiers; they are never sent by the client.
validatePdfRequest
validatePdfRequest(input) returns { request, diagnostics } and never throws. The request must be an object with only the fields rdt and options; rdt must be a version 1 tree; options may contain only title (string up to 256 characters), lang (a language tag) and timeoutMs (an integer from 1,000 to 300,000). Unknown fields and options, including __proto__, are errors (INVALID_PDF_REQUEST) with a JSON Pointer. The service uses the same function.
createPdfClient
const client = createPdfClient({ endpoint, fetch?, headers?, timeoutMs?, maxRequestChars?, maxResponseBytes? });
const result = await client.render({ rdt, options });render validates the request first, then posts it. It never throws. The result is { ok: true, pdf, pages } or { ok: false, status, diagnostics }.
- Timeout: default 60 s, overridden by
options.timeoutMs; the request is aborted through anAbortControllerwhen one exists, and a timeout isPDF_TRANSPORT_FAILED. - Size: requests over
maxRequestChars(default 20 million characters) and responses overmaxResponseBytes(default 200 MB) are refused. - A success body must start with
%PDF-. - Error bodies are parsed for diagnostics; only entries with a known code, a valid severity and string message and path are kept (up to 50, each truncated). Anything else becomes a single
PDF_SERVER_ERRORthat names the status only, so remote text never reaches the caller. - Transport errors do not echo the underlying message.
- Extra headers (for example authorization) are kept only when the name and value are plain visible ASCII;
content-typecannot be overridden.