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:

  • 200 with application/pdf bytes and an optional x-docstudio-pages header with the page count;
  • an error status (400 invalid request, 413 too large, 429 queue full, 503 unavailable, 504 timeout, 500 render 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 an AbortController when one exists, and a timeout is PDF_TRANSPORT_FAILED.
  • Size: requests over maxRequestChars (default 20 million characters) and responses over maxResponseBytes (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_ERROR that 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-type cannot be overridden.