Get a Free Trial

Take Website Screenshots or Turn HTML to Images in Java

DownloadTechnical Information
Java API

Take perfect screenshots of websites or convert HTML to images with GrabzIt's Java API. Before starting, remember to call URLToImage, HTMLToImage or FileToImage methods first. Then, use Save or SaveTo method to capture the screenshot.

Basic Options

You need just one parameter to take a screenshot of a web page or convert HTML into an image file. As shown in the following example.

grabzIt.URLToImage("https://www.tesla.com");
//Then call the Save or SaveTo method
grabzIt.HTMLToImage("<html><body><h1>Hello World!</h1></body></html>");
//Then call the Save or SaveTo method
grabzIt.FileToImage("example.html");
//Then call the Save or SaveTo method

Image Screenshot Formats

GrabzIt's Java API can capture images in various formats. Like JPG, PNG, WEBP, TIFF, and BMP with different colour depths.

The default format for image screenshots is JPG. However the quality of a JPEG image may not be good enough for some apps. PNG format is best for image screenshots because it balances quality and file size well in these situations. The below example demonstrates taking a screenshot image using the PNG format.

GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

ImageOptions options = new ImageOptions();
options.setFormat(ImageFormat.PNG);

grabzIt.URLToImage("https://www.tesla.com", options);
//Then call the Save or SaveTo method
grabzIt.SaveTo("result.png");
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

ImageOptions options = new ImageOptions();
options.setFormat(ImageFormat.PNG);

grabzIt.HTMLToImage("<html><body><h1>Hello World!</h1></body></html>", options);
//Then call the Save or SaveTo method
grabzIt.SaveTo("result.png");
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

ImageOptions options = new ImageOptions();
options.setFormat(ImageFormat.PNG);

grabzIt.FileToImage("example.html", options);
//Then call the Save or SaveTo method
grabzIt.SaveTo("result.png");

Browser Size

The browser size is the size of the window when taking a screenshot. Usually, you don't need to change this because the default size works for most tasks. To set the browser size just pass a value to the setBrowserWidth and setBrowserHeight methods of the ImageOptions class.

Change Image Size

Changing the size of an image is easy, doing it without distorting the image is a bit harder. To make the whole process simpler we recommend you use this simple image dimension calculator.

To increase the image width and height to a size larger than the browser width and height which, by default is 1366 by 1170 pixels. You must also increase the browser width and height to match.

Custom Identifier

You can pass a custom identifier to the image methods as shown below. Your GrabzIt Java handler then returns this value. For example, this special name could be a database ID, linking a screenshot to a specific database entry.

GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

ImageOptions options = new ImageOptions();
options.setCustomId("123456");

grabzIt.URLToImage("https://www.tesla.com", options);
//Then call the Save method
grabzIt.Save("http://www.example.com/handler");
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

ImageOptions options = new ImageOptions();
options.setCustomId("123456");

grabzIt.HTMLToImage("<html><body><h1>Hello World!</h1></body></html>", options);
//Then call the Save method
grabzIt.Save("http://www.example.com/handler");
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

ImageOptions options = new ImageOptions();
options.setCustomId("123456");

grabzIt.FileToImage("example.html", options);
//Then call the Save method
grabzIt.Save("http://www.example.com/handler");

Full Length Screenshot

GrabzIt lets you capture a whole webpage screenshot by using the setBrowserHeight method and entering -1. To ensure the image matches the size of the browser this pass a -1 to the setHeight and setWidth methods.

GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

ImageOptions options = new ImageOptions();
options.setBrowserHeight(-1);
options.setWidth(-1);
options.setHeight(-1);

grabzIt.URLToImage("https://www.tesla.com", options);
//Then call the Save or SaveTo method
grabzIt.SaveTo("result.jpg");
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

ImageOptions options = new ImageOptions();
options.setBrowserHeight(-1);
options.setWidth(-1);
options.setHeight(-1);

grabzIt.HTMLToImage("<html><body><h1>Hello World!</h1></body></html>", options);
//Then call the Save or SaveTo method
grabzIt.SaveTo("result.jpg");
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

ImageOptions options = new ImageOptions();
options.setBrowserHeight(-1);
options.setWidth(-1);
options.setHeight(-1);

grabzIt.FileToImage("example.html", options);
//Then call the Save or SaveTo method
grabzIt.SaveTo("result.jpg");

You can also return uncropped thumbnails, but be aware that this can create large images. To do this pass a -1 to the setHeight and/or setWidth methods. If you pass a dimension of -1. The image will not be cropped.

GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

ImageOptions options = new ImageOptions();
options.setWidth(-1);
options.setHeight(-1);

grabzIt.URLToImage("https://www.tesla.com", options);
//Then call the Save or SaveTo method
grabzIt.SaveTo("result.jpg");
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

ImageOptions options = new ImageOptions();
options.setWidth(-1);
options.setHeight(-1);

grabzIt.HTMLToImage("<html><body><h1>Hello World!</h1></body></html>", options);
//Then call the Save or SaveTo method
grabzIt.SaveTo("result.jpg");
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

ImageOptions options = new ImageOptions();
options.setWidth(-1);
options.setHeight(-1);

grabzIt.FileToImage("example.html", options);
//Then call the Save or SaveTo method
grabzIt.SaveTo("result.jpg");
Note there is no full length browser width!

Using these values means that you can create a screenshot that is a full scale version of the entire web page!

Take a Screenshot of a Page Element

GrabzIt allows you to take a screenshot of any HTML element. As long as it has an id or class attribute, such as a div or span tag, and capture all its content. Specify the HTML element you want to screenshot as a CSS selector.

...
<div id="features">
	<img src="http://www.example.com/football.jpg"/><h3>Local Team Loses</h3>
</div>
...

The code captures the div with the id "features" and outputs it as a 250 x 250px JPEG image in the example below.

GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

// The 250 parameters indicates that image should be sized to 250 x 250 px
ImageOptions options = new ImageOptions();
options.setWidth(250);
options.setHeight(250);
options.setFormat(ImageFormat.JPG);
options.setTargetElement("#features");

grabzIt.URLToImage("http://www.bbc.co.uk/news", options);
//Then call the Save or SaveTo method
grabzIt.SaveTo("result.jpg");

The next example again captures the "features" div but this time outputs a JPEG image that is exactly the same size of the div.

GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

// The -1 indicates that image should not be cropped
ImageOptions options = new ImageOptions();
options.setWidth(-1);
options.setHeight(-1);
options.setBrowserHeight(-1);
options.setFormat(ImageFormat.JPG);
options.setTargetElement("#features");

grabzIt.URLToImage("http://www.bbc.co.uk/news", options);
//Then call the Save or SaveTo method
grabzIt.SaveTo("result.jpg");