Implementation:MarketSquare Robotframework browser Pdf Grpc Handlers
| Knowledge Sources | |
|---|---|
| Domains | Browser Automation, PDF Generation |
| Last Updated | 2026-02-12 05:40 GMT |
Overview
gRPC service handlers for saving pages as PDF documents and emulating media features such as color scheme, forced colors, reduced motion, and media type.
Description
This module exports two async functions that handle PDF generation and media emulation requests.
savePageAsPdf generates a PDF from the active page using Playwright's page.pdf() method. It extracts a comprehensive set of PDF options from the protobuf request including path, displayHeaderFooter, footerTemplate, headerTemplate, format, height, width, landscape, margin (parsed from JSON string), outline, pageRanges, preferCSSPageSize, printBackground, scale, and tagged. All options are passed through to Playwright's PDF API and the output file path is returned in the response.
emulateMedia configures media emulation on the active page by setting colorScheme, forcedColors, media, and reducedMotion properties. Each property supports three states: not_set (property is omitted from options), null (property is explicitly set to null to reset), or a specific value string. The assembled options object is passed to Playwright's page.emulateMedia() method.
Usage
Use savePageAsPdf when tests need to verify PDF output or generate PDF reports from web pages. Note that PDF generation is only supported in Chromium-based browsers. Use emulateMedia to test responsive designs under different media conditions such as dark mode (colorScheme: dark), print stylesheets (media: print), reduced motion preferences, or forced colors accessibility mode.
Code Reference
Source Location
- Repository: MarketSquare_Robotframework_browser
- File: node/playwright-wrapper/pdf.ts
- Lines: 1-124
Signature
export async function savePageAsPdf(
request: Request.Pdf,
state: PlaywrightState,
): Promise<Response.String>
export async function emulateMedia(
request: Request.EmulateMedia,
state: PlaywrightState,
): Promise<Response.Empty>
Import
import { savePageAsPdf, emulateMedia } from './pdf';
I/O Contract
| Function | Request Type | Response Type | Description |
|---|---|---|---|
| savePageAsPdf | Request.Pdf | Response.String (file path) | Generates a PDF from the active page with full layout options; returns the output file path |
| emulateMedia | Request.EmulateMedia | Response.Empty (JSON options string) | Configures media emulation settings on the active page |
| savePageAsPdf Options | |
|---|---|
| Option | Description |
| path | Output file path for the generated PDF |
| displayHeaderFooter | Whether to display header and footer |
| headerTemplate / footerTemplate | HTML templates for header and footer |
| format | Paper format (e.g., 'A4', 'Letter') |
| width / height | Custom paper dimensions |
| landscape | Paper orientation |
| margin | JSON object with top, right, bottom, left margins |
| pageRanges | Page ranges to include (e.g., '1-5') |
| scale | Scale of the webpage rendering (0.1 to 2.0) |
| printBackground | Whether to print background graphics |
| tagged | Whether to generate tagged (accessible) PDF |
| outline | Whether to embed document outline |
| preferCSSPageSize | Whether to prefer CSS-defined page size |
| emulateMedia Property States | ||
|---|---|---|
| Property | Value | Behavior |
| colorScheme / forcedColors / media / reducedMotion | not_set | Property omitted from options (no change) |
| colorScheme / forcedColors / media / reducedMotion | null | Property set to null (reset to default) |
| colorScheme / forcedColors / media / reducedMotion | specific value | Property set to the given value (e.g., 'dark', 'print') |
Usage Examples
*** Settings ***
Library Browser
*** Test Cases ***
Generate PDF Example
New Browser chromium headless=true
New Page https://example.com
Save Page As Pdf path=/tmp/output.pdf landscape=true format=A4 printBackground=true
Emulate Dark Mode Example
New Browser chromium headless=true
New Page https://example.com
Emulate Media colorScheme=dark reducedMotion=reduce