HTML to Image

How to convert HTML to Image in Java

HTML is the defacto format to structure web pages and it stores content in standard text format. The tags inside HTML define the page layout and content of the webpage, including the text, tables, images, and hyperlinks, which are displayed in the web browser. However, lastly, it was observed that malicious scripts can be embedded inside HTML pages and can be used in a variety of different attack types, including cross-site scripting (XSS). Therefore, many organizations/systems block the loading of HTML files shared in offline mode. So a workable solution is to convert HTML to Image format. In this article, we are going to discuss the details of how to convert HTML to JPG in Java.

HTML to Image Conversion API

We are going to use Aspose.HTML Cloud SDK for Java to perform HTML to Image conversion. This API provides the feature to load and manipulate existing HTML files. At the same time, it also offers the feature to render HTML to PDF, XPS, DOCX, and image formats including (JPEG, PNG, BMP, and TIFF). Now please add the following lines to the pom.xml of your maven build type project to download and install the SDK.


The next major step is a free subscription to our cloud services via Aspose.Cloud dashboard using GitHub or Google account. Or, simply create a new Account and obtain your Client Credentials details.

How to convert HTML to JPG in Java

Please follow the instructions given below to accomplish the requirement of converting HTML to JPG.

  • First of all, we need to specify details against Configuration.setAPP_SID and Configuration.setAPI_KEY methods
  • Secondly, we set details for setBasePath(..), setAuthPath(..) and specify setUserAgent(…) as WebKit
  • Thirdly, for our own assistance, we are going to set setDebug(..) as true
  • Now create an object of ConversionApi class
  • Specify the margin details and name for information for the resultant file
  • Finally, call GetConvertDocumentToImage(…) to initiate the conversion process. This method accepts input HTML name, resultant image format, margin, and dimensions details as arguments

Convert HTML to JPG using cURL Commands

Aspose.HTML Cloud APIs can also be accessed via cURL commands using command line terminals. But as a pre-requisite, we need to first generate a JSON Web Token (JWT) based on your individual client credentials. Please execute the following command to generate the JWT token.

curl -v "" \
-d "grant_type=client_credentials&client_id=bbf94a2c-6d7e-4020-b4d2-b9809741374e&client_secret=1c9379bb7d701c26cc87e741a29987bb" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"

Once the JWT token has been generated, please execute the following command on the terminal to perform HTML to Image conversion.

curl -v -X GET "" \
-H  "accept: multipart/form-data" \
-H  "authorization: Bearer <JWT Token>"


We have learned the conversion of HTML to Image using Java code snippets as well as the details on how we can convert HTML to JPG using cURL commands. The Product Documentation is a great source for learning other amazing capabilities being offered by the API. Also, if you encounter any issues while using the API, please feel free to contact the Free product support forum.

We also recommend visiting the following blogs for further details regarding: