Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Microsoft Playwright CrPdf

From Leeroopedia
Revision as of 11:36, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Microsoft_Playwright_CrPdf.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Knowledge Sources
Domains Chromium, PDF
Last Updated 2026-02-12 00:00 GMT

Overview

Concrete tool for generating PDF documents from Chromium pages via the Chrome DevTools Protocol provided by the Playwright library.

Description

The `CrPdf` module provides a `pdfToBuffer` function that generates PDF content from a Chromium page using CDP's `Page.printToPDF` command. It handles paper format resolution (letter, A4, legal, tabloid, etc.), unit conversion from CSS units (px, in, cm, mm) to inches, margin calculation, and print options including landscape orientation, background graphics, header/footer templates with class-based substitution (date, title, url, pageNumber, totalPages), page ranges, scale, and tagged/outline PDF options. The result is read from a CDP IO stream via `readProtocolStream`.

Usage

Use CrPdf when generating PDF output from Chromium pages via `page.pdf()`, which is only supported in headless Chromium.

Code Reference

Source Location

Signature

export async function pdfToBuffer(client: CRSession, params: channels.PagePdfParams): Promise<Buffer>;

Import

import { pdfToBuffer } from '../server/chromium/crPdf';

I/O Contract

Inputs

Name Type Required Description
client CRSession Yes CDP session for the target page
params channels.PagePdfParams Yes PDF generation parameters (format, margins, scale, headerTemplate, etc.)

Outputs

Name Type Description
buffer Buffer The generated PDF content as a Node.js Buffer

Usage Examples

import { pdfToBuffer } from '../server/chromium/crPdf';

const pdfBuffer = await pdfToBuffer(crSession, {
  format: 'Letter',
  printBackground: true,
  margin: { top: '0.5in', bottom: '0.5in', left: '0.5in', right: '0.5in' },
  headerTemplate: '<span class="title"></span>',
  footerTemplate: '<span class="pageNumber"></span>/<span class="totalPages"></span>',
});
await fs.promises.writeFile('output.pdf', pdfBuffer);

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment