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// Initialize an empty HTML document
2HTMLDocument document = new HTMLDocument();
3
4// Create a text node and add it to the document
5Text text = document.createTextNode("Hello World!");
6document.getBody().appendChild(text);
7
8// Save the HTML document to the file on a disk
9document.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// Prepare a simple HTML file with a linked document
2java.nio.file.Files.write(
3 java.nio.file.Paths.get("save-with-linked-file.html"),
4 "<p>Hello World!</p><a href='linked.html'>linked file</a>".getBytes());
5
6// Prepare a simple linked HTML file
7java.nio.file.Files.write(java.nio.file.Paths.get(
8 "linked.html"),
9 "<p>Hello linked file!</p>".getBytes());
10
11// Load the "save-with-linked-file.html" into memory
12HTMLDocument document = new HTMLDocument("save-with-linked-file.html");
13
14// Create a save options instance
15HTMLSaveOptions options = new HTMLSaveOptions();
16
17// The following line with value '0' cuts off all other linked HTML-files while saving this instance
18// If you remove this line or change value to the '1', the 'linked.html' file will be saved as well to the output folder
19options.getResourceHandlingOptions().setMaxHandlingDepth(1);
20
21// Save the document with the save options
22document.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// Prepare a simple HTML file with a linked document
2java.nio.file.Files.write(
3 java.nio.file.Paths.get("document.html"),
4 "<p>Hello World!</p><a href='linked-file.html'>linked file</a>".getBytes());
5
6// Prepare a simple linked HTML file
7java.nio.file.Files.write(
8 java.nio.file.Paths.get("linked-file.html"),
9 "<p>Hello linked file!</p>".getBytes());
10
11// Load the "document.html" into memory
12HTMLDocument document = new HTMLDocument("document.html");
13
14// Save the document to MHTML format
15document.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// Prepare HTML code
2String html_code = "<H2>Hello World!</H2>";
3
4// Initialize a document from the string variable
5HTMLDocument document = new HTMLDocument(html_code);
6
7// Save the document as a Markdown file
8document.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// Prepare SVG code
2String code = "<svg xmlns='http://www.w3.org/2000/svg' height='200' width='300'>" +
3 "<g fill='none' stroke-width= '10' stroke-dasharray='30 10'>" +
4 "<path stroke='red' d='M 25 40 l 215 0' />" +
5 "<path stroke='black' d='M 35 80 l 215 0' />" +
6 "<path stroke='blue' d='M 45 120 l 215 0' />" +
7 "</g>" +
8 "</svg>";
9
10// Initialize an SVG instance from the content string
11SVGDocument document = new SVGDocument(code, ".");
12
13// Save the SVG file to a disk
14document.save($o("save-to-SVG.svg"));
Conclusion
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
andResourceHandlingOptions
. 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.Aspose.HTML for Java allows you to save HTML documents in various formats, including HTML, MHTML, and Markdown, providing precise control and simplicity.
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.