Implementation:MarketSquare Robotframework browser Pdf Keywords
Appearance
| Knowledge Sources | |
|---|---|
| Domains | Browser Automation, PDF Export |
| Last Updated | 2026-02-12 05:40 GMT |
Overview
Provides Robot Framework keywords for saving browser pages as PDF files and emulating CSS media types for print-ready rendering.
Description
The Pdf class extends LibraryComponent and contains two keywords plus one internal helper:
- Save Page As PDF -- Exports the current page to a PDF file at the specified path. Supports extensive configuration including paper format, orientation, scale, margins, header/footer templates, page ranges, background graphics, tagged/accessible PDF, and document outline embedding. If the path is relative, the file is saved under ${OUTPUT_DIR}. This keyword is only supported in Chromium headless mode. Returns the absolute path to the saved PDF file.
- Emulate Media -- Changes the CSS media type and feature emulations for the current page. Supports colorScheme (light/dark), forcedColors (active/none), media (screen/print/null), and reducedMotion (reduce/no-preference). Typically called before Save Page As PDF to ensure the page renders in the correct format.
- _is_relative_to -- Internal helper method that checks whether a given path is relative to the output directory, with Python 3.8 compatibility.
Usage
Use Emulate Media with media=screen before Save Page As PDF to ensure proper rendering. Use Save Page As PDF when you need to generate PDF reports, capture page snapshots for documentation, or create printable versions of web content. This workflow requires a Chromium browser running in headless mode.
Code Reference
Source Location
- Repository: MarketSquare_Robotframework_browser
- File: Browser/keywords/pdf.py
- Lines: 1-190
Signature
class Pdf(LibraryComponent):
@keyword(tags=("Getter", "PageContent"))
def save_page_as_pdf(
self,
path: PathLike,
*,
displayHeaderFooter: bool = False,
footerTemplate: str = "",
format: PdfFormat = PdfFormat.Letter,
headerTemplate: str = "",
height: str = "0px",
landscape: bool = False,
margin: PdfMarging = PdfMargingDefault,
outline: bool = False,
pageRanges: str = "",
preferCSSPageSize: bool = False,
printBackground: bool = False,
scale: float = 1,
tagged: bool = False,
width: str = "0px",
) -> str: ...
@keyword(tags=("Setter", "PageContent"))
def emulate_media(
self,
colorScheme: ColorScheme | None = None,
forcedColors: ForcedColors | NotSet = NotSet.not_set,
media: Media | None = None,
reducedMotion: ReducedMotion | None = None,
) -> None: ...
Import
from Browser.keywords.pdf import Pdf
I/O Contract
Save Page As PDF
| Direction | Name | Type | Description |
|---|---|---|---|
| Input | path | PathLike | File path for the PDF; relative paths resolve under ${OUTPUT_DIR} |
| Input | displayHeaderFooter | bool | Display header and footer (default: False) |
| Input | footerTemplate | str | HTML template for the print footer |
| Input | format | PdfFormat | Paper format: Letter, Legal, Tabloid, Ledger, A0-A6 (default: Letter) |
| Input | headerTemplate | str | HTML template for the print header with classes: date, title, url, pageNumber, totalPages |
| Input | height | str | Paper height with units (px, in, cm, mm) (default: "0px") |
| Input | landscape | bool | Paper orientation (default: False for portrait) |
| Input | margin | PdfMarging | Dictionary with top, right, bottom, left margin values (default: all "0px") |
| Input | outline | bool | Embed document outline into PDF (default: False) |
| Input | pageRanges | str | Page ranges to print, e.g., "1-5, 8, 11-13" (default: "" for all pages) |
| Input | preferCSSPageSize | bool | Prioritize CSS @page size over format/width/height (default: False) |
| Input | printBackground | bool | Print background graphics (default: False) |
| Input | scale | float | Webpage rendering scale, between 0.1 and 2 (default: 1) |
| Input | tagged | bool | Generate tagged (accessible) PDF (default: False) |
| Input | width | str | Paper width with units (default: "0px") |
| Output | return | str | Absolute path to the saved PDF file |
Emulate Media
| Direction | Name | Type | Description |
|---|---|---|---|
| Input | colorScheme | ColorScheme or None | Emulate prefers-color-scheme: "light", "dark", or None to disable |
| Input | forcedColors | ForcedColors or NotSet | Emulate forced-colors: "active", "none", or not_set |
| Input | media | Media or None | CSS media type: "screen", "print", or None to disable |
| Input | reducedMotion | ReducedMotion or None | Emulate prefers-reduced-motion: "reduce", "no-preference", or None to disable |
Usage Examples
Robot Framework
*** Settings ***
Library Browser
*** Test Cases ***
Save Page As PDF With Default Settings
New Browser chromium headless=True
New Page https://example.com
Emulate Media media=screen
${pdf_path}= Save Page As Pdf page.pdf
Should Be Equal ${pdf_path} ${OUTPUT_DIR}${/}page.pdf
Save Page As PDF With Custom Options
New Browser chromium headless=True
New Page https://example.com/report
Emulate Media media=print colorScheme=light
${pdf_path}= Save Page As Pdf report.pdf
... landscape=True
... printBackground=True
... format=A4
... scale=0.8
... pageRanges=1-3
Related Pages
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment