Use DocStudio in a React app

Embed the DocStudio designer in React, render a live document preview from state with renderDocument, and print it with printHtml.

DocStudio has two React-relevant parts: the Designer component, and the framework-independent renderer that you can call from any component.

Embed the designer

TemplateEditor.tsx
import { Designer, type DocumentPayload } from 'docstudio/designer';

export function TemplateEditor({
  initial,
  sample,
  onSave,
}: {
  initial: unknown;
  sample: unknown;
  onSave: (payload: DocumentPayload) => Promise<void>;
}): React.JSX.Element {
  return (
    <div style={{ height: '100vh' }}>
      <Designer defaultTemplate={initial} sampleData={sample} onSubmit={onSave} initialFit="page" />
    </div>
  );
}

defaultTemplate seeds the editor. Use onChange(template, patch) to follow edits, and onSampleDataChange to follow the sample data. Install the peers first: npm install react react-dom @dnd-kit/core.

A live preview without the designer

renderDocument is synchronous and runs in the browser, so a preview is a memo:

Preview.tsx
import { useMemo } from 'react';
import { renderDocument } from 'docstudio';

export function Preview({
  template,
  data,
}: {
  template: unknown;
  data: unknown;
}): React.JSX.Element {
  const result = useMemo(
    () => renderDocument(template, data, { target: 'screen' }),
    [template, data],
  );
  if (!result.ok) {
    return (
      <ul>
        {result.diagnostics.map((item) => (
          <li key={`${item.path}${item.code}`}>{item.message}</li>
        ))}
      </ul>
    );
  }
  return (
    <iframe
      title="Document preview"
      sandbox=""
      srcDoc={result.html}
      style={{ width: '100%', height: 800, border: 0 }}
    />
  );
}

Render into a sandboxed iframe with srcDoc so the document's CSS cannot affect your page, and the document cannot run scripts.

Print

import { printHtml } from 'docstudio/pdf';

async function print(template: unknown, data: unknown): Promise<void> {
  const result = renderDocument(template, data, { target: 'print' });
  if (result.ok) await printHtml(result.html);
}

Notes

  • Keep the template and data in your own state or store. The designer does not save anything by itself.
  • Hosts that need history can use the headless editor from docstudio/designer, which applies validated commands with undo.
  • See the Next.js guide for server components.

Last updated 2026-12-03