PDF service

Chromium service, isolation and limits. Rendering and PDF in the DocStudio developer reference.

Public from docstudio/server (Node only): createPdfService, createPdfHandler, createPdfHttpServer, launchPlaywright, createNodeNetwork, fetchRemoteImage, prepareAssets, isPrivateAddress, DEFAULT_SERVICE_LIMITS, DOCUMENT_CSP and the structural types. The export resolves only under the node condition; browsers get null. Playwright is an optional peer dependency and Chromium is never bundled. Decisions: ADR 0003 and 0004.

Creating a service

const service = createPdfService({
  launchBrowser: () => launchPlaywright(),
  assets, // optional AssetResolver
  fonts, // optional FontRegistry
  limits, // optional overrides
  log, // optional callback with job metadata only
});
const result = await service.render({ rdt, options });
await service.close();

launchBrowser returns anything that has the small BrowserLike surface (newContext, close, isConnected); launchPlaywright() returns a Playwright Chromium. Injecting the launcher keeps the service testable without a browser, lets hosts configure launch flags, and keeps Playwright types out of the published declarations. In containers that cannot use the Chromium sandbox pass launchPlaywright({ args: ['--no-sandbox'] }) and run as a non-root user with a restricted profile.

render takes the wire request from pdf.md, never throws, and returns { ok: true, pdf, pages } or { ok: false, status, diagnostics } with an HTTP-style status: 400 invalid request or tree, 413 too large, 429 queue full, 500 render failure, 503 shutting down, 504 timeout.

Isolation and safety

  • One long-lived browser, one context per job. Each job gets a fresh browser context with downloads off and service workers blocked; the context is always closed, including after errors and timeouts.
  • Request interception. Inside the context every request is aborted except the single document request, which is fulfilled by the service itself from the rendered HTML. Nothing is loaded from the network or the file system by the page. Images and fonts reach the page only as data: URIs.
  • Content security policy. The document is served with default-src 'none'; img-src data:; font-src data:; style-src 'unsafe-inline'; script-src 'none'; base-uri 'none'; form-action 'none', delivered as a real response header (a meta tag is not enforced for setContent, as found in Phase 0).
  • Assets. Image identifiers resolve through the host AssetResolver. A remote https image is fetched by the service only when the resolver allows the address, and only after fetchRemoteImage approves it: https only, default port only, no credentials, no address literals other than a strict public IPv4, the host name resolved once and every resolved address required to be public (loopback, private, link-local, carrier-grade NAT, documentation, multicast, reserved, IPv4-mapped, NAT64 and 6to4 forms are all blocked), the connection pinned to the checked address with the original name for TLS, no redirects, a supported image content type and a size cap (default 5 MB), a 10 second timeout, at most 50 remote images per document. Fetched bytes are validated again by the renderer (signature must match the type) and embedded as data URIs.
  • No files, no payload logging. The service writes no files and its log callback receives only { id, status, ms, bytes, pages }. Playwright's temporary Chromium profile lives in the operating system temporary directory and is removed when the browser closes. Error responses never include the underlying browser message, document content or paths.
  • Fonts. Faces come from the host FontRegistry, are embedded as data URIs, and header and footer families are preloaded in the main document (ADR 0003). The main document waits for document.fonts.ready before printing.

Limits and lifecycle

Limit Default Behaviour
concurrency 2 Jobs running at once
queueSize 16 Jobs waiting; more are refused with PDF_QUEUE_FULL (429)
jobTimeoutMs 60,000 Per job, including navigation and printing; options.timeoutMs can only lower it. A timed-out job returns PDF_TIMEOUT and the browser is recycled
maxRequestChars 20 million Larger requests get 413
maxPdfBytes 200 MB Larger output gets PDF_TOO_LARGE
recycleAfterJobs 200 The browser is closed when idle after this many jobs and relaunched on demand
maxRemoteImageBytes 5 MB Per remote image

A crashed page or a disconnected browser fails the job with PDF_RENDER_FAILED and the next job launches a new browser. close() refuses queued jobs with PDF_UNAVAILABLE, waits for running jobs, then closes the browser; later requests get 503.

HTTP

createPdfHandler(service, { maxBodyBytes?, authorize? }) returns a Node request listener and createPdfHttpServer wraps it in an http.Server (typed structurally, so the declarations need no Node type packages). Bundlers must treat playwright as external when bundling the server entry. It accepts only POST with application/json (405 and 415 otherwise), caps the body (413), answers 400 for bad JSON, calls service.render, and writes either the PDF (application/pdf, x-docstudio-pages) or a JSON { ok: false, diagnostics } body, always with cache-control: no-store and x-content-type-options: nosniff. authorize(headers) returning false gives 401; authentication and TLS are otherwise the host's responsibility, as is rate limiting per client.