Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active March 18, 2025 23:38
HTML to JPEG in Java
public class main
{
public static void main(String[] args)
{
// Define path for the working files.
String dir = "data/";
try {
// Invoke the HTMLDocument method to load the source HTML file.
com.aspose.html.HTMLDocument htmlDocument = new com.aspose.html.HTMLDocument(dir+"sample.html");
// Create an object of the ImageSaveOptions class with the JPEG image format.
com.aspose.html.saving.ImageSaveOptions options = new com.aspose.html.saving.ImageSaveOptions(com.aspose.html.rendering.image.ImageFormat.Jpeg);
// Invoke the setBackgroundColor function to make the background color of the JPEG/JPG image white.
options.setBackgroundColor(Color.getWhite());
// Call the convertHTML method to convert HTML to JPEG.
com.aspose.html.converters.Converter.convertHTML(htmlDocument, options, dir+"HTMLtoJPEG_Output.jpeg");
System.err.println("HTML to JPEG Conversion completed.");
} catch (Exception e) {
// TODO: handle exception
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment