HTML Converter – Convert HTML in Java

HTML is the standard language used to create documents for display in web browsers. As the web language, HTML has continuously evolved to meet the changing demands of displaying new types of information on web pages. HTML documents are the foundation of the World Wide Web and are used to create virtually all the content you see online. Converting HTML to other formats can be helpful in many ways. For example, by converting HTML to PDF, you can view content on a broader range of devices and platforms, make it easier to print the content while preserving the original formatting, or provide a way to keep the content for future reference.

One of the main features of Aspose.HTML is its conversion capability. Aspose.HTML for Java provides easy access to conversion methods through its Converter class. These methods are used for the most common scenarios of converting HTML to popular formats such as PDF, XPS, DOCX, JPG, PNG, BMP, TIFF, GIF, MHTML, and MD.

How to Convert HTML in Java

The articles in the “HTML Converter” chapter give information on the list of supported HTML conversions and how to perform them using the Converter class.

To convert HTML, you should take a few steps:

  1. Install Aspose.HTML for Java library.
  2. Initialize a new instance of the HTMLDocument class using one of HTMLDocument() constructors.
  3. Create a Save Options object that specifies the desired output format and conversion options.
  4. Call one of the convertHTML() methods and pass the necessary parameters to it.

Please take a look over the following Java code snippet that converts HTML to PNG using the convertHTML(document, options, savePath) method.

1// Initialize an HTML document from the file
2HTMLDocument document = new HTMLDocument("nature.html");
3
4// Initialize ImageSaveOptions
5ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Png);
6
7// Convert HTML to PNG
8Converter.convertHTML(document, options, "nature-output.png");

The convertHTML() method takes as parameters document, options, and savePath and performs the operation. Note: You need to specify the path to the output file in your local file system (savePath). Also, for HTMLDocument(documentPath) constructor, you need to provide the path to a source HTML file on your local file system.

Aspose.HTML offers a free online HTML Converter for converting HTML files to a variety of popular formats. You can easily convert HTML to PDF, HTML to XPS, HTML to Image, HTML to MD and HTML to MHTML. Just select the file, choose the format to convert, and you’re done. It’s fast and completely free!

You can download the complete Java examples and data files from GitHub.

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.