Implementation:Microsoft Playwright BidiPdf
| Knowledge Sources | |
|---|---|
| Domains | BiDi, PDF |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
Concrete tool for generating PDF documents from web pages via the WebDriver BiDi protocol provided by the Playwright library.
Description
The `BidiPdf` module provides the `pdfToBuffer` function that generates PDF content from a browsing context using BiDi's `browsingContext.print` command. It handles paper format resolution (letter, A4, legal, etc.), unit conversion (px, in, cm, mm to inches), margin calculation, and print options such as landscape orientation, background graphics, header/footer templates, page ranges, and scale. The `convertPrintParameterToInches` helper converts CSS-style dimension strings into inches for the BiDi print parameters. The module defines standard page paper formats matching common print sizes.
Usage
Use BidiPdf when generating PDF output from pages in BiDi-connected browsers, typically via `page.pdf()` in Firefox or Chromium-via-BiDi mode.
Code Reference
Source Location
- Repository: Microsoft_Playwright
- File: packages/playwright-core/src/server/bidi/bidiPdf.ts
Signature
export async function pdfToBuffer(session: BidiSession, params: channels.PagePdfParams): Promise<Buffer>;
Import
import { pdfToBuffer } from '../server/bidi/bidiPdf';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| session | BidiSession | Yes | The BiDi session for the target browsing context |
| params | channels.PagePdfParams | Yes | PDF generation parameters (format, margins, scale, etc.) |
Outputs
| Name | Type | Description |
|---|---|---|
| buffer | Buffer | The generated PDF content as a Node.js Buffer |
Usage Examples
import { pdfToBuffer } from '../server/bidi/bidiPdf';
const pdfBuffer = await pdfToBuffer(bidiSession, {
format: 'A4',
landscape: false,
printBackground: true,
margin: { top: '1cm', bottom: '1cm', left: '1cm', right: '1cm' },
});
await fs.promises.writeFile('output.pdf', pdfBuffer);