Print from the browser

Print a rendered DocStudio document with printHtml, which opens the browser print dialog in a hidden frame without any server or PDF service.

Browser printing needs no server, no Playwright and no Chromium install. The browser's print engine does the work, and the user can choose Save as PDF in the print dialog.

printHtml

printHtml is exported from the browser-safe docstudio/pdf entry:

print.ts
import { renderDocument } from 'docstudio';
import { printHtml } from 'docstudio/pdf';

export async function printReport(template: unknown, data: unknown): 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);
}

It renders the HTML into a sandboxed hidden frame, opens the print dialog for that document only, resolves when the dialog closes and removes the frame.

Use the print target

Render with target: 'print'. That target writes the page size and margins from your template as @page rules, so the browser prints the paper size you designed.

What to expect

  • Multi-page documents work. Header, footer and {n} of {total} page numbers are real in print output.
  • The result depends on the browser's print engine. Margins, background printing and fonts can differ between browsers.
  • Fonts must be available to the page. Pass a font registry to renderDocument for fonts that are not installed on the user's machine.
  • It is a client-side feature. For files generated on a server, use the PDF service.

From the designer

The designer has a built-in Print action that does exactly this for the current template and data. See designer setup.

Last updated 2026-12-03