Save HTML Document in Java

The aspose.html.Saving package is presented by data classes for a description of specific save options at the conversion and saving process. After working with an HTML file or creating an HTML document from scratch, you can save changes using one of the HTMLDocument.save() methods. The Java library provides the [SaveOptions]https://reference.aspose.com/html/java/com.aspose.html.saving/saveoptions/ and ResourceHandlingOptions classes that allow you to set options for saving operations.

Aspose.HTML for Java library has two different concepts for creating the output files:

  • The first conception is based on creating HTML-like files as output – HTML, MHTML, Markdown, and SVG files. The SaveOptions is a base class for this approach that helps to handle the saving process of related resources such as scripts, styles, images, etc.
  • The second concept could be used to represent HTML as a result visually. The base class for this conception is RenderingOptions; it has specialized methods to specify the page size, page margins, resolution, user styles, etc.

This article only describes how to use SaveOptions class and save() methods for saving HTML documents. To read more about the rendering mechanism, please follow the Fine-Tuning Converters article.

Save HTML

Once you have finished your changes in HTML, you may want to save the document using one of the methods HTMLDocument.save(). The following example is the easiest way to save HTML file:

 1// Save HTML to a file using Java
 2
 3// Initialize an empty HTML document
 4HTMLDocument document = new HTMLDocument();
 5
 6// Create a text node and add it to the document
 7Text text = document.createTextNode("Hello, World!");
 8document.getBody().appendChild(text);
 9
10// Save the HTML document to a file
11document.save("save-to-file.html");

The sample above is quite simple. However, in real-life applications you often need additional control over the saving process. The next few sections describe how to use resource handling options or save you document to the different formats.

Save HTML to File

The following code snippet shows how to use ResourceHandlingOptions to manage linked documents. We create a simple HTML file from scratch with a linked document named “linked.html” in the example.

To apply save options, we use the HTMLSaveOptions() constructor to create an instance of the HTMLSaveOptions class. Then, using the getResourceHandlingOptions() method, we set the maximum handling depth for linked resources in the save options to 1. This means that saving the HTML document will include the main document and any directly linked resources up to a depth of 1. And finally, we call the save(path, options) method of the document object, along with the specified save options.

 1// Save HTML with a linked resources using Java
 2
 3// Prepare a simple HTML file with a linked document
 4java.nio.file.Files.write(
 5        java.nio.file.Paths.get("save-with-linked-file.html"),
 6        "<p>Hello, World!</p><a href='linked.html'>linked file</a>".getBytes());
 7
 8// Prepare a simple linked HTML file
 9java.nio.file.Files.write(java.nio.file.Paths.get("linked.html"),
10        "<p>Hello, linked file!</p>".getBytes());
11
12// Load the "save-with-linked-file.html" into memory
13HTMLDocument document = new HTMLDocument("save-with-linked-file.html");
14
15// Create an instance of the HTMLSaveOptions class
16HTMLSaveOptions options = new HTMLSaveOptions();
17
18// The following line with the value "0" cuts off all other linked HTML-files while saving this instance
19// If you remove this line or change the value to "1", the "linked.html" file will be saved as well to the output folder
20options.getResourceHandlingOptions().setMaxHandlingDepth(1);
21
22// Save the document with the save options
23document.save("save-with-linked-file_out.html", options);

Save HTML to MHTML

In some cases you need to save your HTML document as a single MHTML file. MHTML document could be very useful for this purpose since it is a web-page archive and it stores everything inside itself. The HTMLSaveFormat specifies the format in which the document is saved, it can be HTML, MHTML and MD formats. The example below shows how to use the save(path, saveFormat) method for HTML to MHTML saving.

 1// Save HTML as MHTML using Java
 2
 3// Prepare a simple HTML file with a linked document
 4java.nio.file.Files.write(
 5        java.nio.file.Paths.get("document.html"),
 6        "<p>Hello, World!</p><a href='linked-file.html'>linked file</a>".getBytes());
 7
 8// Prepare a simple linked HTML file
 9java.nio.file.Files.write(
10        java.nio.file.Paths.get("linked-file.html"),
11        "<p>Hello, linked file!</p>".getBytes());
12
13// Load the "document.html" into memory
14HTMLDocument document = new HTMLDocument("document.html");
15
16// Save the document to MHTML format
17document.save("save-to-MTHML.mht", HTMLSaveFormat.MHTML);

As well as for HTML to File example, you can use MHTMLSaveOptions and customize the required handling options.

Save HTML to Markdown

Markdown is a markup language with plain-text syntax. In order to create Markdown files by using Aspose.HTML, please take a look at following example:

 1// Save HTML as a Markdown file using Java
 2
 3// Prepare HTML code
 4String html_code = "<H2>Hello, World!</H2>";
 5
 6// Initialize a document from a string variable
 7HTMLDocument document = new HTMLDocument(html_code, ".");
 8
 9// Save the document as a Markdown file
10document.save("save-to-MD.md", HTMLSaveFormat.Markdown);

Aspose.HTML for Java, in addition to the HTML to Markdown save feature, also offers an HTML to Markdown converter. For the more information how to use Markdown Converter, please visit HTML to Markdown article.

Save SVG

Usually, you can see the SVG document as a part of an HTML file; it represents the vector data on the page: images, icons, tables, etc. However, SVG could also be extracted from the web page, and you can manipulate it similarly to the HTML document. Since SVGDocument and HTMLDocument are based on the same WHATWG DOM standard, all operations, such as loading, reading, editing, converting, and saving, are similar for both documents. So, all the examples where you can see manipulation with HTMLDocument are applicable to SVGDocument as well.

To save your changes in SVG, please use as the follows:

 1// Save HTML as SVG using Java
 2
 3// Prepare SVG code
 4String code = "<svg xmlns='http://www.w3.org/2000/svg' height='200' width='300'>" +
 5        "<g fill='none' stroke-width= '10' stroke-dasharray='30 10'>" +
 6        "<path stroke='red' d='M 25 40 l 215 0' />" +
 7        "<path stroke='black' d='M 35 80 l 215 0' />" +
 8        "<path stroke='blue' d='M 45 120 l 215 0' />" +
 9        "</g>" +
10        "</svg>";
11
12// Initialize an SVG instance from the content string
13SVGDocument document = new SVGDocument(code, ".");
14
15// Save the SVG file to disk
16document.save("save-to-SVG.svg");

Conclusion

  1. The aspose.html.Saving package allows developers to customize and optimize the process of saving HTML and SVG documents using advanced options such as SaveOptions and ResourceHandlingOptions. Using these classes, you can efficiently manage linked resources, select specific output formats, and customize the saving process to suit the needs of your application.

  2. Aspose.HTML for Java allows you to save HTML documents in various formats, including HTML, MHTML, and Markdown, providing precise control and simplicity.

  3. The examples in this article demonstrate the simplicity and power of saving operations. Check out the Fine-Tuning Converters article to further explore rendering options and other advanced features.

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

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.