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, which is a shared facade for most often conversion scenarios and offers a wide range of HTML conversions to popular formats like 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 perform HTML conversion, you should take a few steps:

  1. Install Aspose.HTML for Java library to convert HTML.
  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 shows the HTML to PNG conversion using the convertHTML(document, options, savePath) method.

 1	// Prepare a path to a source HTML file
 2    String documentPath = Path.combine(getDataDir(), "nature.html");
 3
 4    // Prepare a path for converted file saving 
 5    String savePath = Path.combine(getOutputDir(), "nature-output.png");
 6    
 7	// Initialize an HTML Document from the html file
 8    HTMLDocument   document = new HTMLDocument(documentPath);
 9    try {        }
10    finally { if (document != null) document.dispose(); }
11
12    // Initialize ImageSaveOptions 
13    ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Png);
14
15    // Convert HTML to PNG
16    com.aspose.html.converters.Converter.convertHTML(document, options, savePath);

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.