How to generate dynamic PDFs in React
Three ways to produce PDFs from a React app with DocStudio: live preview from state, browser printing with printHtml, and a server PDF endpoint, with the trade-offs of each.
"Generate a PDF in React" hides three different problems: showing the document, letting the user print or save it, and producing a file on a server. They have different answers. This article builds all three on one template and says when each is the right choice.
One template, one renderer
DocStudio renders a JSON template and your data into HTML and CSS. Everything below starts from the same call:
import { renderDocument } from 'docstudio';
const result = renderDocument(template, data, { target: 'print' });It returns { ok, html, diagnostics }. If ok is false, the diagnostics say which part of the template or data is wrong, so a React component can show them instead of crashing.
1. Show the document in the page
A preview is a memoized render shown in a sandboxed frame:
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 <p>The document has {result.diagnostics.length} problems.</p>;
return (
<iframe
title="Document preview"
sandbox=""
srcDoc={result.html}
style={{ width: '100%', height: 800 }}
/>
);
}The screen target draws a paper sheet. The frame isolates the document's CSS from your app and, with an empty sandbox, keeps it from running scripts.
2. Let the user print or save
printHtml from docstudio/pdf prints the document in a hidden frame, so only the document is printed and not your app:
import { printHtml } from 'docstudio/pdf';
async function onPrint(): Promise<void> {
const result = renderDocument(template, data, { target: 'print' });
if (result.ok) await printHtml(result.html);
}This needs no server. In the browser's print dialog the user can choose Save as PDF. The trade-off is control: you cannot name the file, run it in the background or guarantee that every browser paginates identically.
3. Produce a file on the server
When you need a file for an email, a download link or a batch job, render in Chromium on a server:
import { compile } from 'docstudio/core';
import { createPdfService, launchPlaywright } from 'docstudio/server';
const service = createPdfService({ launchBrowser: () => launchPlaywright() });
export async function toPdf(template: unknown, data: unknown): Promise<Uint8Array> {
const compiled = compile({ template, data });
if (compiled.tree === undefined) throw new Error('template or data problem');
const result = await service.render({ rdt: compiled.tree });
if (!result.ok) throw new Error('render failed');
return result.pdf;
}This needs Node, Playwright and a Chromium install. The browser then calls your authenticated endpoint, for example with createPdfClient.
Choosing
| Need | Use |
|---|---|
| Show the document in the app | The preview |
| The user prints or saves once | printHtml |
| A file, an attachment or a background job | The server |
Most apps start with the first two and add the server when a requirement needs a file. Because all three use the same template and renderer, nothing is rewritten when you add the third.
Where to go next
Related: the matching feature page and the quick start.