Examples
How to convert HTML to a base64 image with Playwright and Java
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.
Get the base64 encoded image of some HTML and print to screen
import com.microsoft.playwright.*;
import java.util.Base64;
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>");
byte[] image = page.screenshot();
System.out.println(Base64.getEncoder().encodeToString(image));
}
}
} Find out how to generate a PNG from HTML with Java and Playwright.
That was 14 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.