Skip to content

Instantly share code, notes, and snippets.

@blog-aspose-cloud
Last active August 10, 2022 00:13
Show Gist options
  • Save blog-aspose-cloud/deffb7f7592ebc843b1fae6e5648ba4c to your computer and use it in GitHub Desktop.
Save blog-aspose-cloud/deffb7f7592ebc843b1fae6e5648ba4c to your computer and use it in GitHub Desktop.
Convert HTML to Image using Java Cloud SDK

Convert HTML to Image in Java


This Gist provides the details on how to convert HTML to Image using Aspose.HTML Cloud SDK for Java.
In following code snippet, we are going to first read the input HTML from cloud storage and then going to call GetConvertDocumentToImage(...) method to initiate the HTML to JPG conversion. During the conversion operation, we get an option to specify the format for reusltant image i.e. (JPG, PNG, BMP, TIFF etc). The resultant Image can be saved to local drive using custom java routine. For complete details, please visit Convert HTML to JPG in Java.

Convert HTML to Image

Important Links

Product Page | Docs | Live Demo | Swagger UI | Code Samples | Source Code | New Releases | Blog | Free Support | Free Trial

Convert HTML to Image using Java Cloud SDK
// for more examples, please visit https://github.com/aspose-html-cloud/aspose-html-cloud-java
try
{
// Get ClientID and ClientSecret from https://dashboard.aspose.cloud/
String clientId = "bbf94a2c-6d7e-4020-b4d2-b9809741374e";
String clientSecret = "1c9379bb7d701c26cc87e741a29987bb";
// details for Api invocation
com.aspose.html.Configuration.setAPP_SID(clientId);
com.aspose.html.Configuration.setAPI_KEY(clientSecret);
com.aspose.html.Configuration.setBasePath("https://api.aspose.cloud/v3.0");
com.aspose.html.Configuration.setAuthPath("https://api.aspose.cloud/connect/token");
com.aspose.html.Configuration.setUserAgent("WebKit");
com.aspose.html.Configuration.setDebug(true);
// Create an object of Aspose.HTML Cloud API
com.aspose.html.api.ConversionApi htmlApi = new ApiClient().createService(ConversionApi.class);
// The html document from cloud storage
String name = "list.html";
// resultant image format
String outFormat = "PNG";
Integer width = 800; // Resulting image width.
Integer height = 1000; // Resulting image height.
Integer leftMargin = 10; // Left resulting image margin.
Integer rightMargin = 10; // Right resulting image margin.
Integer topMargin = 10; // Top resulting image margin.
Integer bottomMargin = 10; // Bottom resulting image margin.
Integer resolution = 300; // Resolution of resulting image.
String folder = null; // The folder in the storage. Should exist.
String storage = "Internal"; // Name of the storage. null
// Invoke the API for HTMl to JPG conversion
retrofit2.Call<okhttp3.ResponseBody> call = htmlApi.GetConvertDocumentToImage(name, outFormat, width, height, leftMargin, rightMargin, topMargin, bottomMargin, resolution, folder, storage);
// (optional custom method to save resultant JPG to local drive)
checkAndSave(call, "resultantFile.png");
System.out.println("HTML to JPG conversion sucessfull !");
}catch(Exception ex)
{
System.out.println(ex);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment