Examples

Screenshot the viewport with Java and Playwright

Add Playwright to your Maven project

<dependency>
  <groupId>com.microsoft.playwright</groupId>
  <artifactId>playwright</artifactId>
  <version>1.54.0</version>
</dependency>

Playwright downloads its browsers automatically on first run. Check Maven Central for the latest version.

Take a screenshot of the viewport and save to file

import com.microsoft.playwright.*;
import java.nio.file.Paths;

public class Screenshot {
  public static void main(String[] args) {
    try (Playwright playwright = Playwright.create()) {
      Browser browser = playwright.chromium().launch();
      Page page = browser.newPage();
      page.navigate("https://www.htmltoimage.com/demo");
      page.screenshot(new Page.ScreenshotOptions().setPath(Paths.get("viewport.png")));
    }
  }
}

Find out how to change the size of the viewport with Java and Playwright.

That was 13 lines of code — plus a browser to run in production

Keeping headless browsers fed, patched and scaled is a job in itself. We built Urlbox so the same screenshot is one API call:

curl "https://api.urlbox.com/v1/YOUR_API_KEY/png?url=https://example.com"

Try the Urlbox screenshot API — free trial, no browser babysitting.