Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
To save an HTML document in Java, create or load an HTMLDocument, edit the DOM if needed, and call
HTMLDocument.save(). Use HTMLSaveOptions to control linked resources or HTMLSaveFormat to save HTML as MHTML or Markdown. Use SVGDocument.save() for standalone SVG content.
After creating or editing an HTMLDocument, you can save its current DOM state to an HTML-like format. The com.aspose.html.saving package provides options for controlling the output and associated resources.
Saving and conversion solve different tasks:
save() method and save options.| API | Use it to |
|---|---|
| HTMLDocument.save() | Save the current HTML document by path, local URL, format, options, or resource handler. |
| HTMLSaveOptions | Configure HTML output and access resource handling settings. |
| ResourceHandlingOptions | Control linked resource handling, URL restrictions, and page handling depth. |
| HTMLSaveFormat | Select HTML, MHTML, or Markdown output for a format-based save overload. |
| SVGDocument | Load, edit, and save a standalone SVG document. |
The simplest workflow creates an HTML document, adds content to its DOM, and saves the result as a local .html file.
To save an HTML document to a file in Java:
HTMLDocument.document.save(path) with the target HTML file path.The following example creates a text node, adds it to the document body, and saves save-to-file.html:
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");HTML documents can reference stylesheets, images, scripts, fonts, and other pages. Use HTMLSaveOptions.getResourceHandlingOptions() to control how these resources are processed.
To save HTML and process a directly linked page:
HTMLDocument.HTMLSaveOptions.MaxHandlingDepth to 1 through getResourceHandlingOptions().document.save(path, options).The following example creates an HTML file that links to linked.html. It sets the maximum page handling depth to 1, so directly referenced pages can be processed during saving:
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// Set the maximum page handling depth to 1 so that directly referenced pages can be processed during saving
19options.getResourceHandlingOptions().setMaxHandlingDepth(1);
20
21// Save the document with the save options
22document.save("save-with-linked-file_out.html", options);The default maximum page handling depth is 0, which limits processing to the current document. A value of 1 includes directly referenced pages; -1 enables processing at any depth. Resource and page URL restrictions can still limit which referenced items are handled.
The
HTMLSaveFormat enumeration selects an HTML-like output format when calling a compatible save() overload.
MHTML packages an HTML document and handled resources into a web archive. It is useful when you need a portable archive rather than a main HTML file with an adjacent resource folder.
To save HTML as MHTML in Java:
HTMLDocument..mht or .mhtml extension.document.save(path, HTMLSaveFormat.MHTML).MHTMLSaveOptions instead when page depth or resource handling requires customization.The following example loads an HTML file and saves it in MHTML format:
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-MHTML.mht", HTMLSaveFormat.MHTML);For more control, pass
MHTMLSaveOptions to a compatible save() overload and configure its resource handling options. To convert MHTML input to PDF or images, see the
MHTML Converter.
Markdown is a plain-text markup format suited to editable documentation and content workflows.
To save HTML as Markdown in Java:
HTMLDocument..md file path.document.save(path, HTMLSaveFormat.Markdown).The following example creates an HTML heading and saves it as save-to-MD.md:
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);For converter-based workflows and additional Markdown settings, see Convert HTML to Markdown in Java.
SVG can be embedded in HTML or processed as a standalone XML-based vector document. Use SVGDocument for a standalone SVG DOM. Its loading, DOM editing, and saving workflows are similar to those of HTMLDocument because both APIs follow DOM concepts.
To save an SVG document in Java:
SVGDocument.document.save(path) with a target .svg file.The following example creates SVG markup containing three paths and saves it as save-to-SVG.svg:
1// Create and save an SVG document 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");| Issue | Cause and fix |
|---|---|
| Linked pages are not included | The default MaxHandlingDepth is 0. Set it to 1 for directly referenced pages or select another appropriate depth. |
| Images, CSS, scripts, or fonts are missing | Check the resource paths, base URL, resource handling mode, and URL restrictions used by HTMLSaveOptions. |
A PDF or image was expected from save() | HTMLDocument.save() is for HTML-like formats. Use the HTML conversion APIs for PDF, XPS, DOCX, or image output. |
| MHTML or Markdown output was not created | Use the save(path, HTMLSaveFormat) overload and select MHTML or Markdown. |
| SVG changes were not saved | Apply edits to an SVGDocument and call its save() method with the target SVG path. |
Create or load an HTMLDocument, modify its DOM if needed, and call document.save(path) to write the current document to an HTML file.
The save API can process linked resources. Use HTMLSaveOptions and ResourceHandlingOptions when you need to control whether resources are saved, embedded, or ignored and which URLs may be processed.
Use document.save(path, HTMLSaveFormat.MHTML) to create an MHTML web archive. If linked pages or resource rules require customization, use MHTMLSaveOptions.
Saving writes the document to an HTML-like format such as HTML, MHTML, or Markdown. Converting creates another representation such as PDF, XPS, DOCX, or an image.
You can download the complete examples and data files from GitHub.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.