Overview
The PrintOptions class provides comprehensive configuration for printing a web page to PDF via the W3C WebDriver print command, including orientation, scale, margins, page size, background rendering, and page ranges.
Description
PrintOptions is a mutable configuration object that assembles all parameters needed for the W3C WebDriver print command. It supports portrait and landscape orientations, scale factors between 0.1 and 2.0, page size and margin configuration via PageSize and PageMargin objects, background printing control, shrink-to-fit behavior, and optional page range selection. The class provides a toMap() method to serialize all settings into the wire protocol format. The Orientation enum is nested within the class.
Usage
Use PrintOptions when you need to print the current page to a PDF using the PrintsPage interface. Configure orientation, scale, page size, margins, background printing, and specific page ranges before passing the options to the driver's print method.
Code Reference
Source Location
Signature
public class PrintOptions {
public enum Orientation {
PORTRAIT("portrait"),
LANDSCAPE("landscape");
}
public Orientation getOrientation();
public void setOrientation(Orientation orientation);
public String @Nullable [] getPageRanges();
public void setPageRanges(String firstRange, String... ranges);
public void setPageRanges(List<String> ranges);
public void setBackground(boolean background);
public boolean getBackground();
public void setScale(double scale);
public double getScale();
public boolean getShrinkToFit();
public void setShrinkToFit(boolean value);
public void setPageSize(PageSize pageSize);
public void setPageMargin(PageMargin margin);
public PageSize getPageSize();
public PageMargin getPageMargin();
public Map<String, Object> toMap();
}
Import
import org.openqa.selenium.print.PrintOptions;
I/O Contract
Orientation Enum
| Enum Value |
Wire Format |
Description
|
PORTRAIT |
"portrait" |
Portrait page orientation (default)
|
LANDSCAPE |
"landscape" |
Landscape page orientation
|
Properties
| Property |
Type |
Default |
Constraints |
Description
|
orientation |
Orientation |
PORTRAIT |
Non-null |
Page orientation
|
scale |
double |
1.0 |
0.1 to 2.0 |
Zoom/scale factor; throws IllegalArgumentException if out of range
|
background |
boolean |
false |
-- |
Whether to print background colors and images
|
shrinkToFit |
boolean |
true |
-- |
Whether to shrink content to fit page width
|
pageSize |
PageSize |
ISO A4 |
Non-null |
Page width and height dimensions
|
pageMargin |
PageMargin |
1.0 cm all sides |
Non-null |
Page margins
|
pageRanges |
String[] |
null |
-- |
Optional page ranges to include (e.g., "1-3", "5")
|
Serialization (toMap)
| Map Key |
Type |
Description
|
page |
Map<String, Object> |
Page size as {height, width}
|
orientation |
String |
"portrait" or "landscape"
|
scale |
double |
Scale factor
|
shrinkToFit |
boolean |
Shrink-to-fit flag
|
background |
boolean |
Background printing flag
|
pageRanges |
String[] |
Page ranges (only included if set)
|
margin |
Map<String, Object> |
Margins as {top, bottom, left, right}
|
Usage Examples
// Basic print with defaults
PrintOptions options = new PrintOptions();
Pdf pdf = ((PrintsPage) driver).print(options);
// Landscape with custom scale
PrintOptions landscapeOptions = new PrintOptions();
landscapeOptions.setOrientation(PrintOptions.Orientation.LANDSCAPE);
landscapeOptions.setScale(0.5);
// Full configuration example
PrintOptions fullOptions = new PrintOptions();
fullOptions.setOrientation(PrintOptions.Orientation.PORTRAIT);
fullOptions.setScale(1.0);
fullOptions.setBackground(true);
fullOptions.setShrinkToFit(true);
fullOptions.setPageSize(PageSize.US_LETTER);
fullOptions.setPageMargin(new PageMargin(2.0, 2.0, 1.5, 1.5));
fullOptions.setPageRanges("1-3", "5", "7-10");
// Print specific pages using a List
fullOptions.setPageRanges(List.of("1", "3-5"));
// Execute print
Pdf result = ((PrintsPage) driver).print(fullOptions);
String base64EncodedPdf = result.getContent();
Related Pages
Page Connections
Double-click a node to navigate. Hold to expand connections.