Save HTML Document – Java

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 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.

SaveOptions

The SaveOptions is a base options class for saving operations. It could help you to manage the linked resources. The following table demonstrates a list of available options:

OptionDescription
UrlRestrictionApplies restrictions to the host or folders where resources are located.
MaxHandlingDepthIf you need to save not the only specified HTML document, but also the linked HTML pages, this option gives you the ability to control the depth of the linked pages that should be saved.
JavaScriptThis option specifies how do we need to treat the JavaScript files: it could be saved as a separated linked file, embed into HTML file or even be ignored.
DefaultThis options specifies behavior for other than JavaScript files.

Save HTML

Once you have finished your changes as it is described here, you may want to save the document. The following example is the easiest way to save HTML file:

 1// Prepare an output path for a document saving
 2String documentPath = "save-to-file.html";
 3
 4// Initialize an empty HTML document
 5com.aspose.html.HTMLDocument document = new com.aspose.html.HTMLDocument();
 6
 7// Create a text node and add it to the document
 8Text text = document.createTextNode("Hello World!");
 9document.getBody().appendChild(text);
10
11// Save the HTML document to a file
12document.save(documentPath);

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// Prepare an output path for a document
 2String documentPath = "save-with-linked-file.html";
 3
 4// Prepare a simple HTML file with a linked document
 5File.writeAllText(documentPath, "<p>Hello World!</p>" +
 6        "<a href='linked.html'>linked file</a>");
 7// Prepare a simple linked HTML file
 8File.writeAllText("linked.html", "<p>Hello linked file!</p>");
 9
10// Load the "save-with-linked-file.html" into memory
11com.aspose.html.HTMLDocument document = new com.aspose.html.HTMLDocument(documentPath);
12
13// Create a save options instance
14com.aspose.html.saving.HTMLSaveOptions options = new com.aspose.html.saving.HTMLSaveOptions();
15
16// The following line with value '0' cuts off all other linked HTML files while saving this instance
17// If you remove this line or change value to the '1', the 'linked.html' file will be saved as well to the output folder
18options.getResourceHandlingOptions().setMaxHandlingDepth(1);
19
20// Save the document with the save options
21document.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 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// Prepare an output path for a document saving
 2String documentPath = "save-to-MTHML.mht";
 3
 4// Prepare a simple HTML file with a linked document
 5File.writeAllText("document.html", "<p>Hello World!</p>" +
 6        "<a href='linked-file.html'>linked file</a>");
 7// Prepare a simple linked HTML file
 8File.writeAllText("linked-file.html", "<p>Hello linked file!</p>");
 9
10// Load the "document.html" into memory
11com.aspose.html.HTMLDocument document = new com.aspose.html.HTMLDocument("document.html");
12
13// Save the document to MHTML format
14document.save(documentPath, com.aspose.html.saving.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// Prepare an output path for a document saving
 2String documentPath = "save-to-MD.md";
 3
 4// Prepare HTML code
 5String html_code = "<H2>Hello World!</H2>";
 6
 7// Initialize a document from the string variable
 8com.aspose.html.HTMLDocument document = new com.aspose.html.HTMLDocument(html_code, ".");
 9
10// Save the document as a Markdown file
11document.save(documentPath, com.aspose.html.saving.HTMLSaveFormat.Markdown);

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// Prepare an output path for an SVG document saving
 2String documentPath = "save-to-SVG.svg";
 3
 4// Prepare SVG code
 5String code = "<svg xmlns='http://www.w3.org/2000/svg' height='300' width='300'>" +
 6    "<g fill='none' stroke-width= '10' stroke-dasharray='30 10'>" +
 7        "<path stroke='red' d='M 25 40 l 215 0' />" +
 8        "<path stroke='black' d='M 35 80 l 215 0' />" +
 9        "<path stroke='blue' d='M 45 120 l 215 0' />" +
10    "</g>" +
11"</svg>";
12
13// Initialize an SVG instance from the content string
14com.aspose.html.dom.svg.SVGDocument document = new com.aspose.html.dom.svg.SVGDocument(code, ".");
15
16// Save the SVG file to a disk
17document.save(documentPath);

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.