Save HTML Document in Java

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:

APIUse it to
HTMLDocument.save()Save the current HTML document by path, local URL, format, options, or resource handler.
HTMLSaveOptionsConfigure HTML output and access resource handling settings.
ResourceHandlingOptionsControl linked resource handling, URL restrictions, and page handling depth.
HTMLSaveFormatSelect HTML, MHTML, or Markdown output for a format-based save overload.
SVGDocumentLoad, edit, and save a standalone SVG document.

Save HTML to a File

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:

  1. Create or load an HTMLDocument.
  2. Add or modify document content.
  3. Call document.save(path) with the target HTML file path.
  4. Open or process the saved HTML file as required.

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");

Save HTML with Linked Resources

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:

  1. Prepare the main HTML document and the page it references.
  2. Load the main file into an HTMLDocument.
  3. Create HTMLSaveOptions.
  4. Set MaxHandlingDepth to 1 through getResourceHandlingOptions().
  5. Call 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.

Save HTML as MHTML or Markdown

The HTMLSaveFormat enumeration selects an HTML-like output format when calling a compatible save() overload.

Save HTML to MHTML

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:

  1. Create or load an HTMLDocument.
  2. Choose a target path with the .mht or .mhtml extension.
  3. Call document.save(path, HTMLSaveFormat.MHTML).
  4. Use 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.

Save HTML to Markdown

Markdown is a plain-text markup format suited to editable documentation and content workflows.

To save HTML as Markdown in Java:

  1. Create or load an HTMLDocument.
  2. Choose the target .md file path.
  3. Call document.save(path, HTMLSaveFormat.Markdown).
  4. Use the saved Markdown in a text-based publishing workflow.

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.

Save an SVG Document

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:

  1. Create or load an SVGDocument.
  2. Modify the SVG DOM if needed.
  3. Call document.save(path) with a target .svg file.
  4. Use the saved file as a standalone SVG document.

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");

Common HTML Saving Issues

IssueCause and fix
Linked pages are not includedThe default MaxHandlingDepth is 0. Set it to 1 for directly referenced pages or select another appropriate depth.
Images, CSS, scripts, or fonts are missingCheck 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 createdUse the save(path, HTMLSaveFormat) overload and select MHTML or Markdown.
SVG changes were not savedApply edits to an SVGDocument and call its save() method with the target SVG path.

FAQ

How do I save an HTML document in Java?

Create or load an HTMLDocument, modify its DOM if needed, and call document.save(path) to write the current document to an HTML file.

Does saving HTML include images and CSS?

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.

How do I save HTML as a single file?

Use document.save(path, HTMLSaveFormat.MHTML) to create an MHTML web archive. If linked pages or resource rules require customization, use MHTMLSaveOptions.

What is the difference between saving and converting HTML?

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.

Other Platforms

Related Articles

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