Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save conholdate-com-kb/7875c8d3da5f357a3f5c95809aa52365 to your computer and use it in GitHub Desktop.
Save conholdate-com-kb/7875c8d3da5f357a3f5c95809aa52365 to your computer and use it in GitHub Desktop.
How to Convert HTML to JPG in Java. Get more information here: https://kb.conholdate.com/total/java/how-to-convert-html-to-jpg-in-java/
import com.aspose.html.HTMLDocument;
import com.aspose.html.License;
import com.aspose.html.converters.Converter;
import com.aspose.html.drawing.Resolution;
import com.aspose.html.drawing.UnitType;
import com.aspose.html.rendering.image.ImageFormat;
import com.aspose.html.saving.ImageSaveOptions;
public class ConvertHtmlToJpgInJava {
public static void main2(String[] args) throws Exception {//main function to convert HTML to JPG
// Instantiate the license to avoid water mark in the converted JPG
License licenseHtmlToJpg = new License();
licenseHtmlToJpg.setLicense("Aspose.html.lic");
// Load an existing HTML file to convert to JPG
HTMLDocument documentHtmlToJpg = new HTMLDocument("sample.html");
try {
// Create ImageSaveOptions class object and initialize it with the JPG format
ImageSaveOptions jpgImageOptions = new ImageSaveOptions(ImageFormat.Jpeg);
Resolution resolution = new Resolution(200, UnitType.DPCM);
jpgImageOptions.setHorizontalResolution(resolution);
jpgImageOptions.setVerticalResolution(resolution);
// Export HTML to JPG using the Converter.convertHTML() function
Converter.convertHTML(documentHtmlToJpg, jpgImageOptions, "outputHtmlImage.jpg");
}
finally {
if (documentHtmlToJpg != null) {
documentHtmlToJpg.dispose();
}
}
System.out.println("Done");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment