NODE.JS · DEVELOPER GUIDE

# HTML to PDF in Node.js with Puppeteer

Puppeteer gives Node.js applications control over Chromium's print engine. Production systems need more than a call to page.pdf().

## A minimal example

```
import puppeteer from 'puppeteer';

const browser = await puppeteer.launch({ headless: true });
try {
  const page = await browser.newPage();
  await page.setJavaScriptEnabled(false);
  await page.setContent(html, { waitUntil: 'load', timeout: 10000 });
  const pdf = await page.pdf({
    format: 'A4',
    printBackground: true,
    preferCSSPageSize: true
  });
  return pdf;
} finally {
  await browser.close();
}
```

This is a minimal demonstration, not a safe production endpoint. For untrusted markup, add request interception or an outbound network policy before loading content, and run the browser inside an appropriately isolated container.

## Use print CSS

```
@page { size: A4; margin: 18mm 14mm; }
@media print {
  .no-print { display: none !important; }
  .page-break { break-before: page; }
  tr { break-inside: avoid; }
}
```

## Manage browser lifecycle

Launching a new Chromium process per request is simple but expensive. A browser pool can improve latency but introduces lifecycle and isolation concerns. Recycle workers after a bounded number of jobs, cap parallel pages and monitor memory. Never let a browser outlive a customer request indefinitely.

## Control external resources

Images, fonts and stylesheets loaded from URLs make output less deterministic and create a network boundary. Prefer validated, bundled assets. If remote assets are essential, fetch them in a separate controlled service and validate origin, size, content type and redirects.

## When should you use a PDF API?

Self-host Puppeteer when PDF rendering is core to your product and your team can own browser upgrades, security controls and scaling. A hosted API can be simpler when you need a stable document endpoint. Compare retention, data region, access controls, quotas, uptime and total cost.

Vervant is being built as a focused HTML-to-PDF API for SaaS backends. Public hosted access is not open yet. Read [the rendering security guide](/guides/secure-html-to-pdf.html).
