java.lang.Object
net.serenitybdd.screenplay.playwright.interactions.GeneratePDF
All Implemented Interfaces:
net.serenitybdd.core.SkipNested, net.serenitybdd.screenplay.Performable

public class GeneratePDF extends Object implements 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 Details

    • andSaveAs

      public static GeneratePDF andSaveAs(String filename)
      Generate PDF and save with the specified filename in the default output directory.
      Parameters:
      filename - The filename (e.g., "report.pdf")
    • andSaveTo

      public static GeneratePDF andSaveTo(Path path)
      Generate PDF and save to the specified path.
      Parameters:
      path - The full path for the PDF file
    • andSaveTo

      public static GeneratePDF andSaveTo(String path)
      Generate PDF and save to the specified path.
      Parameters:
      path - The full path for the PDF file
    • withFormat

      public GeneratePDF withFormat(String format)
      Set the paper format (e.g., "Letter", "A4", "A3", "Legal", "Tabloid").
      Parameters:
      format - The paper format
    • withDimensions

      public GeneratePDF withDimensions(String width, String height)
      Set custom paper dimensions.
      Parameters:
      width - Paper width (e.g., "8.5in", "21cm")
      height - Paper height (e.g., "11in", "29.7cm")
    • withMargin

      public GeneratePDF withMargin(String margin)
      Set all margins to the same value.
      Parameters:
      margin - Margin value (e.g., "1cm", "0.5in")
    • withMargins

      public GeneratePDF withMargins(String top, String right, String bottom, String left)
      Set individual margins.
      Parameters:
      top - Top margin
      right - Right margin
      bottom - Bottom margin
      left - Left margin
    • inLandscape

      public GeneratePDF inLandscape()
      Generate PDF in landscape orientation.
    • inPortrait

      public GeneratePDF inPortrait()
      Generate PDF in portrait orientation (default).
    • withBackground

      public GeneratePDF withBackground()
      Include background graphics in the PDF.
    • withHeaderTemplate

      public GeneratePDF withHeaderTemplate(String template)
      Set the header template. Can include classes: date, title, url, pageNumber, totalPages.
      Parameters:
      template - HTML template for the header
    • withFooterTemplate

      public GeneratePDF withFooterTemplate(String template)
      Set the footer template. Can include classes: date, title, url, pageNumber, totalPages.
      Parameters:
      template - HTML template for the footer
    • withPageRanges

      public GeneratePDF withPageRanges(String ranges)
      Set page ranges to print (e.g., "1-5, 8, 11-13").
      Parameters:
      ranges - Page range specification
    • withScale

      public GeneratePDF withScale(double scale)
      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:
      performAs in interface net.serenitybdd.screenplay.Performable
    • getOutputPath

      public Path getOutputPath()
      Get the output path for the PDF.