Use DocStudio in Next.js

Use the DocStudio designer as a client component in the Next.js App Router, render documents in route handlers, and generate PDFs on the Node runtime.

The designer is a client component

The designer needs a browser. Put it in a client component and load it without server rendering:

app/editor/editor.tsx
'use client';

import dynamic from 'next/dynamic';

const Designer = dynamic(() => import('docstudio/designer').then((module) => module.Designer), {
  ssr: false,
});

export function Editor({
  template,
  data,
}: {
  template: unknown;
  data: unknown;
}): React.JSX.Element {
  return (
    <div style={{ height: '100vh' }}>
      <Designer
        defaultTemplate={template}
        sampleData={data}
        onSubmit={async (payload) => {
          await fetch('/api/templates', { method: 'POST', body: JSON.stringify(payload) });
        }}
      />
    </div>
  );
}

Use it from a server component page, passing the stored template and sample data as props.

Render HTML in a route handler

renderDocument needs no browser, so it runs in a route handler or a server component:

app/api/report/route.ts
import { renderDocument } from 'docstudio';
import { loadTemplate, loadReportData } from '@/lib/reports';

export async function GET(): Promise<Response> {
  const result = renderDocument(await loadTemplate(), await loadReportData(), { target: 'print' });
  if (!result.ok) return Response.json({ diagnostics: result.diagnostics }, { status: 422 });
  return new Response(result.html, { headers: { 'content-type': 'text/html; charset=utf-8' } });
}

PDF on the server

docstudio/server needs Node, Playwright and an installed Chromium, so use the Node runtime and a host that can run Chromium:

app/api/report.pdf/route.ts
import { compile } from 'docstudio/core';
import { createPdfService, launchPlaywright } from 'docstudio/server';

export const runtime = 'nodejs';

const service = createPdfService({ launchBrowser: () => launchPlaywright() });

export async function GET(): Promise<Response> {
  const compiled = compile({ template: await loadTemplate(), data: await loadReportData() });
  if (compiled.tree === undefined) return Response.json(compiled.diagnostics, { status: 422 });
  const result = await service.render({ rdt: compiled.tree, options: { title: 'Report' } });
  if (!result.ok) return Response.json(result.diagnostics, { status: result.status });
  return new Response(result.pdf, { headers: { 'content-type': 'application/pdf' } });
}

Create one service per process and close it on shutdown. Whether your hosting platform can run Chromium is a property of the platform, so check it before you rely on this route. If it cannot, use browser printing or run the PDF service as a separate process and call it with createPdfClient.

Bundling

Import docstudio/server only in server code. Its package map has no browser entry, so a client bundle that imports it fails instead of silently including Node code.

Last updated 2026-12-03