HTML Conversion API for Java

Convert HTML and related web formats to PDF, XPS, DOCX, and raster images in Java applications.

Convert HTML and Web Formats in Java

Aspose.HTML for Java converts HTML, XHTML, MHTML, EPUB, Markdown, and SVG in Java applications. Source-specific methods in the Converter class convert web content to PDF, XPS, DOCX, MHTML, Markdown, and raster image formats such as PNG, JPG, TIFF, BMP, and GIF.

Most workflows use a source document or stream, an output-specific options class, and a destination path. Classes such as PdfSaveOptions and ImageSaveOptions control page layout, resolution, background, CSS processing, and other settings for the selected result.


Explore Java Conversion Examples

Choose the source format and open a focused page with Java code, API links, output settings, an interactive converter, and format-specific FAQ.


Choose the Java Conversion Method

Use the method that matches the source format. HTML and XHTML share the HTML conversion workflow, while archive, publication, vector, and Markdown sources have dedicated entry points.

Source formatJava APITypical workflow
HTML or XHTMLConverter.convertHTML()Load an HTMLDocument or pass supported HTML input directly
MHTML or MHTConverter.convertMHTML()Pass an archive path, URL, or stream with target save options
EPUBConverter.convertEPUB()Pass an EPUB path, URL, or stream and select PDF, XPS, DOCX, or image options
SVGConverter.convertSVG()Load an SVGDocument and configure the target format
MarkdownConverter.convertMarkdown()Create HTML directly or return an HTMLDocument for PDF and image rendering

Convert HTML to PDF in Java

The example loads input.html, creates the default PDF save options, and writes output.pdf. Try-with-resources closes the HTML document automatically.

HTML to PDF Java Example

import com.aspose.html.HTMLDocument;
import com.aspose.html.converters.Converter;
import com.aspose.html.saving.PdfSaveOptions;

// Load the HTML document
try (HTMLDocument document = new HTMLDocument("input.html")) {
    // Create the PDF save options
    PdfSaveOptions options = new PdfSaveOptions();

    // Convert HTML to PDF
    Converter.convertHTML(document, options, "output.pdf");
}


Configure Conversion Output in Java

The converter method is selected by the source format, while the save-options class is selected by the required output. Each options class exposes settings that apply to its target format.

Output formatJava options classUse it to configure
PDFPdfSaveOptionsPage size, margins, resolution, background, CSS processing, and encryption
Raster imagesImageSaveOptionsImage format, resolution, page setup, background, and text rendering
XPSXpsSaveOptionsFixed-page layout, page setup, resolution, and rendering settings
DOCXDocSaveOptionsPage layout, resolution, background, and font embedding
MHTMLMHTMLSaveOptionsArchive serialization and resource handling
MarkdownMarkdownSaveOptionsMarkdown formatting rules and included content

Relative CSS, images, fonts, and links are resolved from the document URL or the base URL supplied with in-memory content. Provide a valid base URL when converting an HTML string or stream so the renderer can locate referenced resources.

See the HTML Converter guide for source-specific workflows, Fine-Tuning Converters for page setup and resource handling, and the Converter API Reference for available overloads.


Java HTML Conversion FAQ

1. Can I convert multiple HTML files into one PDF in Java?

Use the Java HTML Merger workflow when several source documents must be rendered into one ordered output. A standard Converter.convertHTML() call processes one HTML source.

2. Can I convert HTML to PDF in Java without Chrome or Selenium?

Yes. Aspose.HTML for Java uses its own rendering and conversion API. Load the source with HTMLDocument and call Converter.convertHTML() with PdfSaveOptions; Chrome, Selenium, and other browser automation tools are not required.

3. Can I convert an HTML file or webpage URL in Java?

Yes. Load a local file or webpage URL with HTMLDocument, or use a Converter.convertHTML() overload that accepts a source path, URL, inline HTML with a base URI, or an HTMLDocument object.

4. Is Aspose.HTML for Java free?

Aspose.HTML for Java is a commercial library. You can download an evaluation version or request a temporary license for testing without evaluation limitations. For manual conversions, use the free online HTML converters .


Install Aspose.HTML for Java

Add Aspose.HTML for Java from the official Aspose repository. Replace ASPOSE_HTML_VERSION with an exact version from the Aspose.HTML for Java releases page, then reload or rebuild the project so the dependency is resolved.

Add the repository and dependency to the Maven project:

Maven – pom.xml

<repositories>
    <repository>
        <id>AsposeJavaAPI</id>
        <url>https://releases.aspose.com/java/repo/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>com.aspose</groupId>
        <artifactId>aspose-html</artifactId>
        <version>ASPOSE_HTML_VERSION</version>
    </dependency>
</dependencies>

Add the repository and dependency to the Gradle project:

Gradle – build.gradle

repositories {
    maven {
        url = uri('https://releases.aspose.com/java/repo/')
    }
}
dependencies {
    implementation 'com.aspose:aspose-html:ASPOSE_HTML_VERSION'
}

Use a JDK compatible with the selected release. For manual JAR installation, Kotlin DSL, updates, and dependency troubleshooting, see Install Aspose.HTML for Java .

Explore More Aspose.HTML for Java Guides

Continue with document creation, merging, extraction, and the complete Java API overview.