Examples
HTML to PDF with Java and Puppeteer
Heads up before you start
Puppeteer has no official Java port. The closest equivalent is jvppeteer — an actively maintained Java library with a Puppeteer-style API that drives Chrome over the same DevTools Protocol. These examples use jvppeteer.
Add jvppeteer to your Maven project
<dependency>
<groupId>io.github.fanyong920</groupId>
<artifactId>jvppeteer</artifactId>
<version>3.6.5</version>
</dependency> jvppeteer drives a Chrome you already have installed in a standard location, or call Puppeteer.downloadBrowser() once to download a Chrome for Testing build. Check Maven Central for the latest version.
Convert HTML to PDF and save to file
import com.ruiyun.jvppeteer.api.core.Browser;
import com.ruiyun.jvppeteer.api.core.Page;
import com.ruiyun.jvppeteer.cdp.core.Puppeteer;
public class Screenshot {
public static void main(String[] args) throws Exception {
try (Browser browser = Puppeteer.launch()) {
Page page = browser.newPage();
page.setContent("<html><body><h1>Hello World</h1></body></html>");
page.pdf("html.pdf");
}
}
} Find out how to change the size of the viewport with Java and Puppeteer.
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.