PDF generation and printing

Two output paths from the same HTML: print in the browser with no server, or generate PDF files on a Node server with Chromium.

The designer in View mode showing the rendered document next to the Print and Submit actions
The designer in View mode showing the rendered document next to the Print and Submit actions

DocStudio renders HTML and CSS first. PDF comes from printing that HTML, and there are two ways to do it.

Browser-native printing

printHtml(html) from docstudio/pdf prints a rendered document through the browser's own print engine. It renders the HTML in a sandboxed hidden frame, opens the print dialog for that document only and removes the frame afterwards.

import { renderDocument } from 'docstudio';
import { printHtml } from 'docstudio/pdf';

const result = renderDocument(template, data, { target: 'print' });
if (result.ok) await printHtml(result.html);

Requirements: a browser. No server, no Playwright, no Chromium install. The user can choose Save as PDF in the dialog.

Server-side PDF

createPdfService from docstudio/server renders the compiled tree in Chromium through Playwright and returns the PDF bytes and the page count.

import { createPdfService, launchPlaywright } from 'docstudio/server';

const service = createPdfService({ launchBrowser: () => launchPlaywright() });
const result = await service.render({ rdt: compiled.tree, options: { title: 'Report' } });
if (result.ok) console.log(result.pdf.length, result.pages);

Requirements: Node 20.19 or later, playwright 1.50 or later and a Chromium installed with npx playwright install chromium. Chromium is never bundled. From a browser you can ask your own endpoint for a PDF with createPdfClient.

Which one

Browser printing Server PDF
Needs a server No Yes, with Chromium
Produces a file in your code No, the user saves it Yes
Background and batch jobs No Yes
Identical output across users Depends on each browser One engine
Fonts Must be available to the page or registered Registered with the service
Controls The user's print dialog Timeouts, concurrency and queue limits

What the service enforces

A restrictive content security policy, request interception that allows only data: and resolved assets, a per-job timeout, concurrency and queue limits, and no files written to disk. Logs carry job identifiers, sizes and timings, never document data.

Limitations

  • Arabic and Urdu text in a PDF has a lossy text layer, so extracted text is degraded.
  • Fonts are supplied by you. A missing font falls back to a default.
  • The designer canvas does not simulate page breaks. Use the Print view or a PDF.
  • Browser printing results depend on the browser's print engine.

See the guides for browser printing and server-side PDF, and the comparison article.

Related documentation