Class GeneratePDF
java.lang.Object
net.serenitybdd.screenplay.playwright.interactions.GeneratePDF
- All Implemented Interfaces:
net.serenitybdd.core.SkipNested,net.serenitybdd.screenplay.Performable
Generate a PDF document from the current page.
Note: PDF generation is only supported in Chromium headless mode.
Sample usage:
// Generate PDF with default settings
actor.attemptsTo(GeneratePDF.andSaveAs("report.pdf"));
// Generate PDF with custom path
actor.attemptsTo(GeneratePDF.andSaveTo(Paths.get("output/invoice.pdf")));
// Generate PDF with options
actor.attemptsTo(
GeneratePDF.andSaveAs("document.pdf")
.withFormat("A4")
.withMargins("1cm", "1cm", "1cm", "1cm")
.inLandscape()
.withHeaderTemplate("<div style='font-size:10px'>Header</div>")
.withFooterTemplate("<div style='font-size:10px'>Page <span class='pageNumber'></span></div>")
);
// Print background graphics
actor.attemptsTo(
GeneratePDF.andSaveAs("styled-report.pdf")
.withBackground()
);
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic GeneratePDFGenerate PDF and save with the specified filename in the default output directory.static GeneratePDFGenerate PDF and save to the specified path.static GeneratePDFGenerate PDF and save to the specified path.Get the output path for the PDF.Generate PDF in landscape orientation.Generate PDF in portrait orientation (default).<T extends net.serenitybdd.screenplay.Actor>
voidperformAs(T actor) Include background graphics in the PDF.withDimensions(String width, String height) Set custom paper dimensions.withFooterTemplate(String template) Set the footer template.withFormat(String format) Set the paper format (e.g., "Letter", "A4", "A3", "Legal", "Tabloid").withHeaderTemplate(String template) Set the header template.withMargin(String margin) Set all margins to the same value.withMargins(String top, String right, String bottom, String left) Set individual margins.withPageRanges(String ranges) Set page ranges to print (e.g., "1-5, 8, 11-13").withScale(double scale) Set the scale of the webpage rendering.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface net.serenitybdd.screenplay.Performable
then
-
Method Details
-
andSaveAs
Generate PDF and save with the specified filename in the default output directory.- Parameters:
filename- The filename (e.g., "report.pdf")
-
andSaveTo
Generate PDF and save to the specified path.- Parameters:
path- The full path for the PDF file
-
andSaveTo
Generate PDF and save to the specified path.- Parameters:
path- The full path for the PDF file
-
withFormat
Set the paper format (e.g., "Letter", "A4", "A3", "Legal", "Tabloid").- Parameters:
format- The paper format
-
withDimensions
Set custom paper dimensions.- Parameters:
width- Paper width (e.g., "8.5in", "21cm")height- Paper height (e.g., "11in", "29.7cm")
-
withMargin
Set all margins to the same value.- Parameters:
margin- Margin value (e.g., "1cm", "0.5in")
-
withMargins
Set individual margins.- Parameters:
top- Top marginright- Right marginbottom- Bottom marginleft- Left margin
-
inLandscape
Generate PDF in landscape orientation. -
inPortrait
Generate PDF in portrait orientation (default). -
withBackground
Include background graphics in the PDF. -
withHeaderTemplate
Set the header template. Can include classes: date, title, url, pageNumber, totalPages.- Parameters:
template- HTML template for the header
-
withPageRanges
Set page ranges to print (e.g., "1-5, 8, 11-13").- Parameters:
ranges- Page range specification
-
withScale
Set the scale of the webpage rendering.- Parameters:
scale- Scale factor (default is 1, range 0.1-2)
-
performAs
public <T extends net.serenitybdd.screenplay.Actor> void performAs(T actor) - Specified by:
performAsin interfacenet.serenitybdd.screenplay.Performable
-
getOutputPath
Get the output path for the PDF.
-