jvppeteer

repository·master·Indexed 21 days ago

https://github.com/fanyong920/jvppeteer

A Java API for controlling Chrome and Firefox browsers, providing an interface similar to Puppeteer for Node.js. It supports tasks such as PDF generation, web scraping, UI testing, and performance analysis. It works with Chrome for Testing and Firefox via WebDriver-bidi.

Tokens
1.7K
Snippets
4
Records
4
Agent score
24%

What's inside jvppeteer

  1. Browser Compatibility and Setup

    master

    Jvppeteer controls Chrome (via DevTools) and Firefox (via WebDriver-bidi).

    Important Notes:

    • Chrome: Since version 2.0.0, Jvppeteer works with Chrome for Testing. If you set headless to true, Jvppeteer automatically matches the new headless mode or the chrome-headless-shell.
    • Firefox: Controlled via WebDriver-bidi. Note that not all functions are supported; unsupported operations will throw UnsupportedOperationException.
    • Version Matching: It is highly recommended to use the browser version specifically bound to your Jvppeteer version to ensure stability.
    | Jvppeteer Version | Bound Browser Version |
    |:-----------------|:----------------------|
    | 3.6.3-3.6.5      | Chrome for Testing 149.0.7798.0 & Firefox stable_149.0 |
    | 3.5.0-3.6.2      | Chrome for Testing 142.0.7444.175 & Firefox stable_145.0.1 |
    | 3.4.0-3.4.2      | Chrome for Testing 137.0.7151.119 & Firefox stable_139.0.4 |
    | 3.3.6-3.3.9      | Chrome for Testing 135.0.7049.42 & Firefox stable_137.0 |
    | 3.3.0-3.3.5      | Chrome for Testing 133.0.6943.98 & Firefox stable_135.0 |
    | 3.0.0-3.2.0      | Chrome for Testing 131.0.6778.87 & Firefox stable_133.0 |
    | 2.2.0-2.2.5      | Chrome for Testing 130.0.6723.58 |
    | 2.0.0-2.1.2      | Chrome for Testing 128.0.6613.137 |
    | 1.1.x            | Chromium 722234 |
  2. Install Jvppeteer via Maven or Gradle

    master

    To use Jvppeteer in your Java project, add the dependency to your build configuration file. The library uses SLF4J for logging; to debug, set your logging level to Debug. If you are not using a dependency manager, you can download the latest standalone JAR from the GitHub releases page.

    ### Maven
    ```xml
    <dependency>
      <groupId>io.github.fanyong920</groupId>
      <artifactId>jvppeteer</artifactId>
      <version>3.6.5</version>
    </dependency>

    Gradle

    // Add mavenCentral() to your repositories
    compile "io.github.fanyong920:jvppeteer:3.6.5"
  3. Generate a PDF from a page

    master

    Use Page.pdf() to generate a PDF.

    Requirements:

    • headless must be set to true in LaunchOptions.
    • You must add the --no-sandbox argument to LaunchOptions for PDF generation to work.

    Common PDFOptions include:

    • setPath(String): Destination file path.
    • setOutline(boolean): Generate an outline.
    • setFormat(PaperFormats): Set paper size (e.g., PaperFormats.a4).
    • setPrintBackground(boolean): Print background graphics.
    • setScale(double): Set the scale factor.
    // pdf must have headless = true
    launchOptions.setHeadless(true);
    ArrayList<String> args = new ArrayList<>();
    args.add("--no-sandbox"); // Required for PDF
    launchOptions.setArgs(args);
    
    Browser cdpBrowser = Puppeteer.launch(launchOptions);
    Page page = cdpBrowser.newPage();
    
    GoToOptions goToOptions = new GoToOptions();
    goToOptions.setWaitUntil(Collections.singletonList(PuppeteerLifeCycle.NETWORKIDLE));
    page.goTo("https://www.baidu.com/", goToOptions);
    
    PDFOptions pdfOptions = new PDFOptions();
    pdfOptions.setPath("baidu.pdf");
    pdfOptions.setOutline(true);
    pdfOptions.setFormat(PaperFormats.a4);
    pdfOptions.setPrintBackground(true);
    pdfOptions.setPreferCSSPageSize(false);
    pdfOptions.setScale(1.1);
    
    page.pdf(pdfOptions);
    cdpBrowser.close();
  4. Capture screenshots of a page

    master

    Use Page.screenshot() to capture images of the current page. You can capture full pages, specify image types (JPEG, WEBP, PNG), and adjust quality for compressed formats.

    Key ScreenshotOptions:

    • setPath(String): Destination file path.
    • setFullPage(boolean): Capture the entire scrollable page.
    • setCaptureBeyondViewport(boolean): Capture content beyond the current viewport.
    • setType(ImageType): Set the image format (e.g., ImageType.JPEG, ImageType.WEBP).
    • setQuality(double): Set quality for JPEG/WEBP (0-100).
    • setOmitBackground(boolean): Omit the background.
    // Basic screenshot
    ScreenshotOptions screenshotOptions = new ScreenshotOptions();
    screenshotOptions.setPath("baidu.png");
    screenshotOptions.setFullPage(true);
    page.screenshot(screenshotOptions);
    
    // JPEG with quality
    ScreenshotOptions jpegOptions = new ScreenshotOptions();
    jpegOptions.setPath("baidu.png");
    jpegOptions.setType(ImageType.JPEG);
    jpegOptions.setQuality(80.00);
    jpegOptions.setFullPage(true);
    page.screenshot(jpegOptions);
    
    // WEBP screenshot
    ScreenshotOptions webpOptions = new ScreenshotOptions();
    webpOptions.setPath("baidu.jpeg");
    webpOptions.setType(ImageType.WEBP);
    webpOptions.setQuality(80.00);
    webpOptions.setFullPage(true);
    page.screenshot(webpOptions);