Examples

HTML to JPEG 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.

Convert HTML to JPG and save to file

import com.microsoft.playwright.*;
import com.microsoft.playwright.options.ScreenshotType;
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.setContent("<html><body><h1>Hello World</h1></body></html>");
      page.screenshot(new Page.ScreenshotOptions()
          .setPath(Paths.get("html.jpg"))
          .setType(ScreenshotType.JPEG));
    }
  }
}

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

That was 16 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.