docstudio/pdf API reference

Every public export of docstudio/pdf in DocStudio 0.2.0: functions, constants, types and interfaces with signatures.

Import from docstudio/pdf.

Never throws. Failures are returned as diagnostics.

Functions

createPdfClient

Creates a browser-safe client that posts a resolved tree to a PDF service and returns the PDF bytes.

createPdfClient(options: PdfClientOptions): PdfClient

Errors. render never throws. Failures are returned as { ok: false, status, diagnostics } and remote error text never reaches the caller.

import { compile } from 'docstudio/core';
import { createPdfClient, type FetchLike } from 'docstudio/pdf';

export async function run(fetch: FetchLike): Promise<{ ok: boolean; pages: number | undefined }> {
  const compiled = compile({
    template: { schemaVersion: '1.0.0', body: [{ id: 'hello', type: 'text', content: ['Hello'] }] },
    data: {},
  });
  if (compiled.tree === undefined) throw new Error('did not compile');
  const client = createPdfClient({
    endpoint: 'https://docs.example.com/api/pdf',
    fetch,
    headers: { authorization: 'Bearer token' },
    timeoutMs: 30000,
  });
  const result = await client.render({ rdt: compiled.tree, options: { title: 'Hello' } });
  return result.ok ? { ok: true, pages: result.pages } : { ok: false, pages: undefined };
}

printHtml

Prints an HTML document through the browser print dialog in a hidden frame and removes the frame afterwards.

printHtml(html: string, options?: PrintOptions): Promise<void>

Errors. Rejects when there is no browser document or when printing throws.

import { printHtml, type PrintOptions } from 'docstudio/pdf';
import { renderDocument } from 'docstudio';

export async function printReport(
  template: unknown,
  data: unknown,
  options?: PrintOptions,
): Promise<void> {
  const result = renderDocument(template, data, { target: 'print' });
  if (!result.ok) {
    throw new Error(result.diagnostics.map((item) => item.message).join('; '));
  }
  await printHtml(result.html, options);
}

validatePdfRequest

Validates a PDF request body and returns the typed request with diagnostics.

validatePdfRequest(input: unknown): RequestValidation

Constants

MAX_TIMEOUT_MS

The largest per-request timeout a PDF request may ask for.

const MAX_TIMEOUT_MS: 300000

MIN_TIMEOUT_MS

The smallest per-request timeout a PDF request may ask for.

const MIN_TIMEOUT_MS: 1000

PDF_DEPENDS_ON

Declares that pdf depends only on core.

const PDF_DEPENDS_ON: "ds:core"

PDF_MODULE_ID

The identifier of the pdf module.

const PDF_MODULE_ID: "ds:pdf"

Types

FetchLike

export type FetchLike = (url: string, init: FetchInit) => Promise<ResponseLike>;

PdfResult

export type PdfResult =
  | { readonly ok: true; readonly pdf: Uint8Array; readonly pages: number | undefined }
  | {
      readonly ok: false;
      readonly status: number | undefined;
      readonly diagnostics: readonly Diagnostic[];
    };

Interfaces

FetchInit

export interface FetchInit {
  readonly method: 'POST';
  readonly headers: Readonly<Record<string, string>>;
  readonly body: string;
  readonly signal?: unknown;
}

PdfClient

export interface PdfClient {
  render(request: unknown): Promise<PdfResult>;
}

PdfClientOptions

export interface PdfClientOptions {
  readonly endpoint: string;
  readonly fetch?: FetchLike;
  readonly headers?: Readonly<Record<string, string>>;
  readonly timeoutMs?: number;
  readonly maxRequestChars?: number;
  readonly maxResponseBytes?: number;
}

PdfJobOptions

export interface PdfJobOptions {
  readonly title?: string;
  readonly lang?: string;
  readonly timeoutMs?: number;
}

PdfRequest

export interface PdfRequest {
  readonly rdt: unknown;
  readonly options?: PdfJobOptions;
}

PrintDocument

export interface PrintDocument {
  createElement(tag: 'iframe'): PrintFrame;
  readonly body: { append(node: PrintFrame): void };
}

PrintFrame

export interface PrintFrame {
  readonly style: { cssText: string };
  readonly contentWindow: PrintWindow | null;
  srcdoc: string;
  setAttribute(name: string, value: string): void;
  addEventListener(name: string, listener: () => void): void;
  remove(): void;
}

PrintOptions

export interface PrintOptions {
  readonly document?: PrintDocument;
  readonly timeoutMs?: number;
}

PrintWindow

export interface PrintWindow {
  focus(): void;
  print(): void;
  addEventListener(name: string, listener: () => void): void;
}

RequestValidation

export interface RequestValidation {
  readonly request: PdfRequest | undefined;
  readonly diagnostics: readonly Diagnostic[];
}

ResponseLike

export interface ResponseLike {
  readonly ok: boolean;
  readonly status: number;
  readonly headers: { get(name: string): string | null };
  arrayBuffer(): Promise<ArrayBuffer>;
  text(): Promise<string>;
}